Bridges and Tunnels

Time limit1sMemory limit128 MB

Summary
Given an undirected weighted graph whose edges are marked indoor or outdoor, answer p queries, each asking for the minimum outdoor time from one building to another, breaking ties by total time.
Level

Medium7 of 10

Topics
Graph, Shortest path, Dynamic programming, Greedy
Solved
No attempts yet

Problem

It may feel warm now, but in a few months campus will be full of snow. Luckily, many of the buildings on campus are connected by bridges and tunnels, so you do not need to go outside very much. The network of buildings can be confusing, and it is hard to know the best way to get from one building to another. A computer program could help.

Input

The first line of input contains three integers nn, mm, and pp (0<n≤40000 < n \le 4000, 0<m≤400000 < m \le 40000, 0<p≤300 < p \le 30): the number of buildings on campus, the number of (indoor or outdoor) paths between the buildings, and the number of trips you would like to make. Buildings are numbered sequentially from 00 to n−1n - 1.

Each of the next mm lines describes a path between buildings with three integers and a letter. The first two integers specify the two buildings connected by the path (the path can be taken in either direction). The third integer specifies the number of seconds required to take the path from one building to the other; this number is at least 00 and at most one million. The letter is I if the path is indoors, or O if the path is outdoors.

Each of the next pp lines describes a trip from one building to another using two integers, the numbers of the two buildings.

Output

For each trip, find the optimal route between the two specified buildings. The optimal route minimizes the amount of time spent outside; among routes that spend the same amount of time outside, the optimal route minimizes the total time spent.

For each trip, output a single line containing two integers: the time spent outside and the total time spent on the optimal route. If there is no route connecting the two specified buildings, output instead a line containing the word IMPOSSIBLE.

Examples1

  1. Example 1

    Input
    2 1 1
    0 1 30 I
    0 1
    
    Expected output
    0 30