#hokudaihitachi20191a. [hokudai_hitachi2019_1_a]Problem A
[hokudai_hitachi2019_1_a]Problem A
Problem Setting
Overview
- Concept: In this programming contest, you will run a delivery service. Customers will place orders with your shop. Each order has a unique and should be delivered to the corresponding customer. Your delivery service has one car. The car will fetch the ordered item from the shop and deliver it to the customer.
- Score: Your goal is to deliver as many items as possible, as quickly as possible in a given amount of time . (Orders are expected until ).
- Constraint: In this contest there is no constraint on the number of items you can place in the car. However, an item can only be loaded in the car, by fetching it from the shop, after the order has been placed.
- Problem A/B: In problem A all order positions and times are given to the contestant in advance and the contestant algorithm shall optimize the moves of the car to make as many deliveries as possible as fast as possible. On the other hand, in problem B orders appear online, that is new orders appear, while you move your car to make as many deliveries as possible as fast as possible.
Specification of Time and Space:
- Time: In this contest we model the progress of time by integer values .
- Map: In this contest we model a map by a simple, undirected, and connected graph , consisting of a set of vertices and a set of edges
- Shop and customer locations: The vertices are labeled from to and the vertex denotes the location of your shop, while vertices denote locations of potential customers. Here, denotes the number of elements of the set .
- Streets: Each edge represents a street connecting the vertices . The corresponding length is given by an integer edge weight .
- Graph creation: The algorithm for generating the map graph based on a random seed is specified in the following pseudo-code. For further details, please see the sample code below.
Pseudo code: Map graph generator
- Input:, ,
- 2d vertex grid:
- First, find the largest integer such that , with being the smallest possible non-negative integer.
- Then we plot points on the 2d vertex grid .
- For each point add a uniform random offset dx, dy \\in \[0, 1\] , giving the final vertex position (x + dx, y + dy)\\in \[0,R\] \\times \[0,R\].
- Finally, add the remaining vertices at a uniform random position with .
- Vertex labels are assigned by random shuffling. The shop is the vertex .
- How we create Highways:
- To generate a highway network, we create a complete graph on the vertex set , assigning each vertex pair the Euclidean distance as an edge weight.
- Next, we construct a minimum spanning tree of . The edges of the minimum spanning tree are the highway network of the graph . We assign each of those edges an edge weight $d_{u,v} \\leftarrow \\lceil 2 \\times W_{u, v} \\rceil$ .
- How we add side roads:
- To create a network of side roads, we successively add edges to the graph as follows:
- Update .
- Among the vertex pairs , not yet connected by an edge, select a pair with minimal .
- Assign the edge weight $d_{u,v} \\leftarrow \\lceil 4 \\times W_{u, v} \\rceil$ .
- Here, is essentially based on the Euclidean distance of vertices, giving preference to connecting nearby vertices with low degree. In addition, preference is given to side roads along the rectangular grid, to avoid too many bridges. The detailed definitions are as follows:
- Define , the degree of vertex as the number of incident edges.
- Define for each vertex according to its original position on the vertex grid as:
- If is even :
- If is odd :
- For the remaining vertices : Assign a color at random.
- Define a factor as follows:
- If and are the same : Set
- If and are different : Set
- Define a factor as follows:
- If : Set
- If : Set
- Finally, the cost is defined as follows:
- $\\mathrm{cost}(u,v) = W_{u,v}\\times \\mathrm{degree}(u) \\times \\mathrm{degree}(v) \\times f(u,v) \\times g(u) \\times g(v)$.
- To create a network of side roads, we successively add edges to the graph as follows:
- How we assign order frequencies:
- Assign each vertex an order frequency .
- Init the order frequency of the shop vertex: .
- Init the order frequency of the other vertices:
- Now determine vertices with order frequency 2. For this draw a uniform random center point c=(c_x,c_y)\\in \[R/4,3R/4\]\\times\[R/4,3R/4\] and then for all vertices do:
- If $\\mathrm{EuclideanDistance}(c,u)\\le R/8 + \\mathrm{uniformRandom}\[0,R/8\]$:
Specification of Car Locations and Moves:
In order to make deliveries you will operate a delivery car, which can take positions and make moves as specified below.
-
Car position: A car can generally take two types of position:
- on a vertex .
- on an edge . More specifically, it is located at a distance from to .
-
Car move: At each step you have to choose one of the following actions in order to control your delivery car.
stay
: stay at the current position.move w
: Take one step towards vertex .
In case of choosing
move w
, must obey the following constraints. A failure to obey these constraints results in a wrong answerWA
.- must be a vertex, i.e., .
- If the car is on vertex , there must be an edge connecting and , i.e., .
- If the car is on the edge , must either be or .
Orders, Deliveries, and Constraints:
- Orders: Throughout the contest each order is characterized by three quantities: A unique order ID, a vertex indicating the order destination, and the order time at which the order appeared. For the detailed format see below.
- Order generation: At each time $0 \\le t \\le T_{\\text{last}} = 0.95 \\times T_{\\text{max}}$ up to one new order can appear with probability . In case there is an order, the order destination is chosen from the vertex set with probability proportional to the order frequency . For details, see the pseudo-code below or the sample code further below.
Pseudo code: Order generation
-
Input: Last order time and average order probability .
-
Init: .
-
For each step do:
- Generate a uniform random number r \\in \[0,1\] .
- If :
- Select a vertex position at random with probability proportional to the order frequency of the vertex.
- place order (new order ID, order time , vertex position )
- Else: place no order
-
Note: The average order probability is defined as follows:
-
$p_{\\text{order}}(t) = \\begin{cases} t / T_{\\text{peak}}, & \\text{if } 0\\le t \\lt T_{\\text{peak}}, \\\\ (T_{\\text{last}} - t) / (T_{\\text{last}}- T_{\\text{peak}}), & \\text{if } T_{\\text{peak}} \\le t \\lt T_{\\text{last}}, \\\\ 0, & \\text{if } T_{\\text{last}} \\le t, \\end{cases}$
-
where and is drawn randomly uniform from the interval \[0, T_{\\text{last}}\].
-
Note: The value of will not be given as an input.
- Delivery: To deliver an order, the contestant must do the following steps after the order has been placed:
- (1st) Move the car onto the shop: Note: When moving a car onto the shop, all orders with order time less than or equal to the current time, will be transfered into the car. On the other hand, orders which have not appeared yet, cannot be placed into the car.
- (2nd) Move the car to the customer position: To finalize a delivery, move the car onto the vertex of the customer position. Note: Orders which have not been transfered into the car yet, will not be delivered, even if you arrive at the customer position.
- Constraints: Throughout the contest, we assume each order has a unique and should be delivered to the corresponding customer. It is further assumed that an unlimited number of orders can be placed in the car.
Scoring
-
During the contest the total score of a submission is determined by summing the score of the submission with respect to 30 input cases.
-
After the contest a system test will be performed. To this end, the contestant's last submission will be scored by summing the score of the submission on 100 previously unseen input cases.
-
For each input case, the score is calculated as follows:
$\\text{Score} = \\sum_{i \\in D} {(T_{\\text{max}})}^{2} - {(\\mathrm{waitingTime}_i)}^{2},$
Here we use the following definitions:
- : the set of orders delivered until
- the waiting time of the th order: $\\mathrm{waitingTime}_i = \\mathrm{deliveredTime}_i - \\mathrm{orderedTime}_i$.
- Note that an input case giving the output
WA
will receive points.
Particulars of Problem A
In problem A we pass all orders which appear at time as an input to the contestant code. The task is to provide an algorithm which optimizes the moves of the car such that the above score becomes maximal.
Input Format
Input is provided in the following form:
: :
- In the first line denotes the number of vertices, while denotes the number of edges.
- The following lines denote the edges of the graph. In particular, in the th line and denote the edge connecting and and the corresponding distance.
- The following line denotes the number of steps .
In the following line, is information about the order from the customer that occurs at time . is given in the form:
- represents the number of new orders which appear at time .
- The next lines give the newly generated order information. The -th order information indicates that the order ID of the new order, while denotes the vertex to which the customer wishes the order to be delivered.
- Note: If , there are no new lines.
Requirements
- All inputs are of non-negative integer value.
- $1 \\leq d_{u_i, v_i} \\leq \\lceil 4\\sqrt{2|V|} \\rceil$
- The given graph has no self-loops / multiple edges and is guaranteed to be connected.
- $1 \\leq \\mathrm{new\\_id}_{i} \\leq T_{\\text{last}}+1$ . Note: If all orders are generated by the order generation rule as explained above, the total number of orders is at most . Therefore, the possible range of should be from to .
- The order IDs are unique.
Output Format
The Output expects integers in the format specified below.
:
In particular, shall specify the movement of the delivery car by using one of the following two options:
stay
, if the car shall not move:
-1
move w
, if the car shall be moved one step towards the neighboring vertex
w
Note that in case of 2) has to satisfy the following conditions:
- If the car is at vertex : .
- If the car is on the edge , must either satisfy or .
Input Example
5 7
1 2 5
5 3 4
2 4 8
1 5 1
2 3 3
4 5 3
4 3 9
4
1
1 2
1
2 5
1
3 4
0
Note that this input is an example of small size and does not meet the constraints of the contest.
Output Example
2
-1
1
5
Explanation
The example operates on a graph with vertices, edges, and time steps. We now describe the orders which have occured and the movement of the car.
Time
At time an order is placed at the shop. This order has ID\= 1 and should be delivered to vertex . Because your car is currently at vertex one, the order will be automatically transfered into your car. In this way, when your car is at the shop, all orders which have been made at present and before, will automatically be loaded into your car.
Time →
You choose to move towards vertex move 2
. You will now move one step towards vertex 2.
Time
A new order has appeared. It has ID and shall be delivered at vertex .
Time →
You decided to stay
. You can now stay on the edge where you are.
Time
A new order has appeared. It has ID and shall be delivered at vertex .
Time →
You decided to move move 1
back to vertex 1. You are approaching one step towards vertex 1. Doing a U-turn in this way is explicitly allowed.
Time
New orders have not occurred. Now that you are at the vertex (store), the orders with order ID are loaded into your car. In a similar way, whenever you return to the store, all the orders that have already been placed are loaded into your car automatically.
Time →
Being at vertex 1 you choose move 5
. You are moving your car one step towards vertex . You arrive at vertex .
Time
Since you arrived at vertex , the order with order ID can be delivered.
Sample Code A
A software toolkit for generation of input samples, scoring and testing on a local contestant environment, and sample codes for beginners is provided through the following link. In addition we provide software for visualizing the contestants results.