This page is still under construction.

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

Hyperspace Routes

Time limit5sMemory limit64 MB

Summary
For each query, find every possible value of the shortest A-to-B path length as the shared hyperspace edge weight x ranges over the positive integers, then report the count and sum, or inf when unbounded.
Level

Hard9 of 10

Topics
Shortest path, Graph, Math, Implementation
Solved
No attempts yet

Problem

In the distant future, food is transported between planets along one-way trade routes. Each route directly connects two planets and has a known transit time.

The traders' guild plans to add new routes using a recently discovered technology: hyperspace travel. Hyperspace travel is also one-directional. Because it is still experimental, the hyperspace travel time is not yet known; it is known, however, that it does not depend on the distance between planets, so every hyperspace route takes the same amount of time to traverse. Let this common time be xx.

The figure below shows a case with three interconnected planets and their transit times. Planets are labelled with positive integers, and the hyperspace travel time is denoted by xx (the figure depicts the graph of the second test case).

Transit time is measured in days and is always a positive integer; the hyperspace time xx is likewise a positive integer.

For two planets AA and BB, the guild wants to know all possible values of the shortest-path total transit time from AA to BB, taken over every possible value of xx. For example, in the situation above, the shortest trip from planet 2 to planet 1 could take 55 days (when x≥5x \ge 5), or 44, 33, 22, or 11 day (when x<5x < 5).

Input

The first line contains two integers PP and RR: the number of planets and the number of routes (1≤P≤5001 \le P \le 500, 0≤R≤100000 \le R \le 10000).

Each of the next RR lines contains two planet labels CC and DD (1≤C,D≤P1 \le C, D \le P, C≠DC \ne D) and the travel time TT. For a conventional route, TT is an integer (1≤T≤1061 \le T \le 10^6); for a hyperspace route, TT is the character x. Several routes may exist between the same pair of planets.

The next line contains an integer QQ, the number of queries (1≤Q≤101 \le Q \le 10).

Each of the next QQ lines contains two planet labels AA and BB (A≠BA \ne B): the guild asks “what are the possible values of the shortest-path transit time from AA to BB?”.

Output

Print QQ lines, one per query.

For each query print two integers: the number of distinct possible values and their sum. If the number of distinct values is unbounded, print only inf on that line instead. If there is no path from AA to BB, both the number of distinct values and their sum are 00 (print 0 0).

Notes

Explanation of the first sample:

  1. There is no path from planet 2 to planet 1, so the answer is 0 0.
  2. For every positive integer xx, the shortest path from 1 to 3 takes 2x2x days, so the number of values is unbounded and the answer is inf.
  3. The shortest path from 1 to 4 can take 33 days (when x=1x = 1), 66 days (when x=2x = 2), or 88 days (when x≥3x \ge 3). There are 33 distinct values and their sum is 3+6+8=173 + 6 + 8 = 17.

Examples2

  1. Example 1

    Input
    4 4
    1 2 x
    2 3 x
    3 4 x
    1 4 8
    3
    2 1
    1 3
    1 4
    
    Expected output
    0 0
    inf
    3 17
    
  2. Example 2

    Input
    3 5
    3 2 x
    2 1 x
    2 1 5
    1 3 10
    3 1 20
    6
    1 2
    2 3
    3 1
    2 1
    3 2
    1 3 
    
    Expected output
    inf
    5 65
    15 185
    5 15
    inf
    1 10