#K64302. Shortest Path with Tolls

    ID: 31945 Type: Default 1000ms 256MiB

Shortest Path with Tolls

Shortest Path with Tolls

You are given a network of villages connected by roads. Each road has an associated distance and toll cost. The task is to find the shortest path from a starting village \( s \) to a destination village \( t \). In the case where more than one path has the same total distance \( D = \sum l \), the path with the minimum total toll cost \( P = \sum p \) should be selected. If no valid path exists from \( s \) to \( t \), output Impossible.

The problem requires you to consider both the distance and the toll, and to use a prioritized approach (first minimizing distance, then toll) to determine the optimal path.

inputFormat

The input consists of multiple datasets. For each dataset:

  • The first line contains two integers \( v \) and \( e \), representing the number of villages and roads, respectively.
  • The second line contains two integers \( s \) and \( t \), the starting and destination village numbers.
  • The following \( e \) lines each contain four integers: \( u \), \( v \), \( l \), and \( p \), where \( u \) and \( v \) denote two villages connected by a road, \( l \) is the distance, and \( p \) is the toll cost for that road.
  • The input ends with a line containing "0 0".

All input is provided via stdin.

outputFormat

For each dataset in the input, output a single line:

  • If a path exists from \( s \) to \( t \), output two space-separated integers: the minimum total distance and the corresponding total toll cost.
  • If no such path exists, output the string Impossible.

All output should be written to stdout.

## sample
5 6
1 5
1 2 3 10
1 3 4 5
2 4 2 4
3 4 5 8
4 5 3 2
2 5 6 15
0 0
8 16

</p>