This page is still under construction.

Parts of this page are still being built. What you see may change.

Courier Services

Time limit1sMemory limit128 MB

Summary
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 nn offices numbered from 11 to nn. 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 nAn_A class-A offices, nBn_B class-B offices, and nCn_C class-C offices, where nA+nB+nC=nn_A + n_B + n_C = n, nA>0n_A > 0, and nC>0n_C > 0.

  • 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 (costcost, timetime). 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 ss to a given destination office tt (both ss and tt 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 ss and tt; 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 s=C1s = C_1 and t=C2t = C_2. To choose the best transport routes between these offices we consider the routes C1→B4→A3→B4→C2C_1 \to B_4 \to A_3 \to B_4 \to C_2, C1→B4→A6→B4→C2C_1 \to B_4 \to A_6 \to B_4 \to C_2, C1→B4→A3→A6→B4→C2C_1 \to B_4 \to A_3 \to A_6 \to B_4 \to C_2, and C1→B4→A6→A3→B4→C2C_1 \to B_4 \to A_6 \to A_3 \to B_4 \to C_2, with (cost, time) pairs (55,66)(55, 66), (60,65)(60, 65), (84,95)(84, 95), and (84,95)(84, 95) respectively. The first two routes are better than the third and the fourth. So there are two routes for which no better route exists: C1→B4→A3→B4→C2C_1 \to B_4 \to A_3 \to B_4 \to C_2 and C1→B4→A6→B4→C2C_1 \to B_4 \to A_6 \to B_4 \to C_2.

Write a program that:

  • reads the description of the office network together with the start office ss and the destination office tt,
  • determines all (cost, time) pairs characterizing the transport routes from ss to tt 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 nn and the number of connections mm, where 2≤n≤10002 \le n \le 1000 and 1≤m≤50001 \le m \le 5000. Offices are numbered from 11 to nn.

Each of the next nn lines describes one office. The description consists of a letter A, B, or C, and two positive integers kk and cc, separated by single spaces. The value kk is the cost and cc is the time of sorting parcels at the office, where 1≤k≤201 \le k \le 20 and 1≤c≤201 \le c \le 20.

Each of the next mm lines describes one connection between offices. The description consists of four integers aa, bb, kk, and cc, separated by single spaces. The numbers aa and bb are the numbers of the connected offices, 1≤a,b≤n1 \le a, b \le n. The value kk is the cost and cc is the time of transporting a parcel over this connection, 1≤k≤1001 \le k \le 100 and 1≤c≤1001 \le c \le 100. All connections are bidirectional. There is at most one (direct) connection between any two offices.

The last line contains two positive integers ss and tt separated by a single space, 1≤s,t≤n1 \le s, t \le n. These are the numbers of the start and destination offices.

Output

The first line should contain one positive integer rr: the number of distinct (cost, time) pairs characterizing all transport routes from office ss to office tt for which no better route exists. The next rr 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.

Examples1

  1. Example 1

    Input
    9 9
    C 1 2
    C 2 1
    A 20 30
    B 12 13
    C 1 1
    A 23 27
    B 10 15
    C 1 3
    C 3 2
    1 4 1 1
    2 4 1 2
    3 4 3 2
    5 3 2 7
    3 6 5 1
    6 4 4 3
    6 7 4 4
    7 8 3 3
    7 9 2 2
    1 2
    
    Expected output
    2
    55 66
    60 65