Courier Services
Time limit1sMemory limit128 MB
Find all Pareto-optimal (cost, time) pairs among routes from a class-C source to a class-C destination in a network with a special office-class structure and cycles.
- Level
Hard8 of 10
- Topics
- Graph, Shortest path, Dynamic programming, Implementation
- Solved
- No attempts yet
Problem
The company SuperCourier delivers parcels to any corner of the world. The company has a network of offices numbered from to . Each office collects parcels from a designated area, sorts them, transfers them, and delivers them to recipients. Fixed connections between offices allow parcels to be transported.
Offices are divided into classes A, B, and C. The network has class-A offices, class-B offices, and class-C offices, where , , and .
- Class-A offices perform primary sorting and transfer. Every parcel, on its way from sender to recipient, must pass through at least one class-A office. A class-A office is connected to every other class-A office, and to some number of class-B and class-C offices.
- Class-B offices perform secondary sorting and transfer. Every class-B office is connected to at least one class-A office and to some number of class-C offices; it has no connection to any other class-B office.
- Class-C offices only collect parcels from their area and deliver parcels to recipients in that area. Every class-C office has exactly one connection, to a class-A or class-B office.
The figure below shows an example network.

Figure: An example network of offices.
Each office and each connection is assigned a pair of attributes (, ). For an office, this pair gives the cost and time of collecting or delivering a parcel in the area it serves, and of sorting and transferring parcels, depending on the office class. For a connection, the pair gives the cost and time of transporting a parcel between the two offices.
The task is to determine, for a given network, the best routes for transporting parcels from a given start office to a given destination office (both and are class C). Each route is characterized by a pair of attributes, its transport cost and time: these are the sum of the costs and the sum of the times of all offices and connections along the route (including and ; an office or connection visited several times is counted each time). The smaller the cost and the shorter the time, the better. We say one route is better than another if it has:
- a smaller cost and a not-longer time, or
- a shorter time and a not-larger cost.
We are interested in the routes for which no better route exists.
For example (see the figure), let and . To choose the best transport routes between these offices we consider the routes , , , and , with (cost, time) pairs , , , and respectively. The first two routes are better than the third and the fourth. So there are two routes for which no better route exists: and .
Write a program that:
- reads the description of the office network together with the start office and the destination office ,
- determines all (cost, time) pairs characterizing the transport routes from to for which no better route exists, where if at least two different routes give the same (cost, time) pair it is reported only once,
- prints the determined pairs.
Input
The first line contains two positive integers separated by a single space: the number of offices and the number of connections , where and . Offices are numbered from to .
Each of the next lines describes one office. The description consists of a letter A, B, or C, and two positive integers and , separated by single spaces. The value is the cost and is the time of sorting parcels at the office, where and .
Each of the next lines describes one connection between offices. The description consists of four integers , , , and , separated by single spaces. The numbers and are the numbers of the connected offices, . The value is the cost and is the time of transporting a parcel over this connection, and . All connections are bidirectional. There is at most one (direct) connection between any two offices.
The last line contains two positive integers and separated by a single space, . These are the numbers of the start and destination offices.
Output
The first line should contain one positive integer : the number of distinct (cost, time) pairs characterizing all transport routes from office to office for which no better route exists. The next lines should contain these pairs, in order of increasing cost. Each pair should be printed on a separate line, with the cost and the time separated by a single space.