The Moon of Valencia

Time limit1sMemory limit128 MB

Summary
Given a map of places with satisfaction values and walking edges, decide for each query whether a simple path between two nodes exists that fits a time budget and yields a satisfaction sum within 0.1 of a target.
Level

Hard9 of 10

Topics
Backtracking, DFS, Graph
Solved
No attempts yet

Problem

Everybody knows the Moon of Valencia is magical, and everybody talks about the mystery that happens under it at night. People remember what time they entered the first bar, what time they reached the hotel and how happy they were when they arrived, but nobody remembers the bars and pubs in between.

The Valencia hotels have hired you to write the program that helps their customers remember. A customer gives the departure time and place, the arrival time and place, and the degree of satisfaction on arrival. The program decides whether a night that matches the story is possible at all.

The program works on a map with the location of every bar and pub. Entering a place adds its own degree of satisfaction. Walking from one place to another makes people angry, so it lowers the degree of satisfaction instead: one point for every minute spent walking, and seconds that do not fill a whole minute count as their fraction of a minute, so 30 seconds cost 0.5 points. Everybody walks at 4 km/h. A customer may stay in a bar or pub as long as they like, but only a stay of at least 15 minutes earns its satisfaction.

A customer does not have to enter every place on the way. Any subset of the places along the route may be entered, and entering the departure place is optional too. The satisfaction is counted up to the door of the target place, so the grade of the target place itself is never added. A route may not pass through the same place twice.

The whole night has to fit between the departure time and the arrival time. The walking time plus 15 minutes for every place entered may not exceed that interval. Time left over is not a problem, because the customer can sit longer in the places they entered.

Input

The input consists of several test cases. Each case begins with the description of a map, followed by the list of arrivals the program has to check.

The description of a map begins with the word MAP in capital letters followed by two integers PP and MM, where PP is the number of places and MM is the number of paths connecting two places (1≤P≤641 \le P \le 64). The next PP lines describe one place each, with two coordinates (real numbers in kilometres), its grade of satisfaction (a real number), its ID and its name. The next MM lines describe one path each, with the IDs of the two places it connects. Each pair of places is connected by at most one path, and no two paths cross.

After the map comes a line with the word ARRIVALS in capital letters, and then one line per arrival with the departure time, the departure place, the arrival time, the arrival place and the degree of satisfaction on arrival, a real number. Times are written as hh:mm on a 24-hour clock, and an arrival time earlier than the departure time means the customer got in after midnight.

Output

For each case print a line with the word MAP in capital letters followed by the number of the case. The first case is MAP 1, the second is MAP 2, and so on.

Then print one line per arrival, in the order the arrivals are given. Print Possible! when at least one route matches the customer's story, and Impossible! when none does.

A route matches the story when it follows paths of the map, never passes through the same place twice, fits between the departure time and the arrival time, and reaches a degree of satisfaction whose absolute difference from the required one is less than 0.1.

Write the search so that it runs as fast as possible. The example case is a good measure: whatever handles it comfortably handles the rest.

Hint

Figure 1: the first map of the example input.

Examples1

  1. Example 1

    Input
    MAP 19 40
      0     0     0  UPV  Universitat Politecnica de Valencia
      5     5     0  SPV  Contest hotel
      0     1    35  B01  The Object
      1.1   1    42  B02  Opera
      0.6   1.7  33  B03  New York
      1.3   2    55  B04  Blue Note
      1.5   2.5  23  B05  The Popes
      2.5   2    13  B06  Petrol
      4     3.5  12  B07  King of Kings
      1.1   4    14  B08  O Salati
      1.2   4.5  13  B09  The Snails
      2.5   3.5  34  B10  The Earth
      1.5   1.5  55  B11  Cafe Coffee
      3     4.5  31  B12  Vermouth house
      4.5   2.5  45  B13  Jamon Session
      1.3   3.6  24  B14  Let's go to eat
      1.5   4    34  B15  I'm hungry
      0.6   2.5  53  B16  The Gecko
      3.5   2.5  43  B17  The Black Sheep
    UPV B01
    B01 B02
    B01 B03
    B01 B16
    B02 B03
    B02 B11
    B16 B08
    B16 B14
    B16 B03
    B03 B04
    B03 B11
    B04 B11
    B04 B16
    B04 B05
    B05 B14
    B08 B09
    B08 B15
    B08 B14
    B11 B06
    B14 B15
    B05 B06
    B05 B16
    B05 B10
    B15 B09
    B15 B10
    B09 B12
    B06 B10
    B06 B17
    B10 B07
    B10 B17
    B10 B12
    B10 B14
    B12 B15
    B12 B07
    B12 SPV
    B17 B07
    B17 B13
    B07 B13
    B07 SPV
    B13 SPV
    ARRIVALS
    23:00  UPV 03:00  SPV   9.0
    23:00  UPV 03:00  SPV   8.0
    23:00  UPV 03:00  SPV   7.0
    23:00  UPV 03:00  SPV   6.0
    23:00  UPV 03:00  SPV   5.0
    23:00  UPV 03:00  SPV   4.0
    23:00  UPV 03:00  SPV   3.0
    23:00  UPV 03:00  SPV   2.0
    23:00  UPV 03:00  SPV   1.0
    23:00  UPV 03:00  SPV   0.0
    23:00  UPV 03:00  SPV  -1.0
    23:00  UPV 03:00  SPV  -2.0
    23:00  UPV 03:00  SPV  -30.0
    23:00  UPV 03:00  SPV  -40.0
    23:00  B05 03:00  B10   40.0
    23:00  B05 03:00  B10   30.0
    23:00  B05 03:00  B10   20.0
    23:00  B05 03:00  B10   10.0
    23:00  B05 03:00  B10    0.0
    23:00  B05 03:00  B10  -10.0
    23:00  B05 03:00  B10  -20.0
    23:00  B05 03:00  B10  -30.0
    23:00  B05 03:00  B10  -40.0
    MAP 2 1
     0  0 0 UPV Universitat Politecnica de Valencia
    10 10 0 SPV Hotel Silken Puerta de Valencia
    UPV SPV
    ARRIVALS
    23:00  UPV  1:00  SPV   9.0
    23:00  UPV  1:00  SPV   8.0
    
    Expected output
    MAP 1
    Possible!
    Possible!
    Possible!
    Possible!
    Possible!
    Possible!
    Possible!
    Possible!
    Possible!
    Possible!
    Possible!
    Possible!
    Possible!
    Possible!
    Possible!
    Possible!
    Possible!
    Possible!
    Possible!
    Possible!
    Possible!
    Possible!
    Possible!
    MAP 2
    Impossible!
    Impossible!