Question:
Trucking
Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission (s): 1211 Accepted Submission (s): 428
Problem Description
A certain local trucking company wowould like to transport some goods on a cargo truck from one place to another. it is desirable to transport as much goods as possible each trip. unfortunately, one cannot always use the roads in the shortest route: some roads may have obstacles (e.g. bridge overpass, tunnels) which limit heights of the goods transported. therefore, the company wowould like to transport as much as possible each trip, and then choose the shortest route that can be used to transport that amount.
For the given cargo truck, maximizing the height of the goods transported is equivalent to maximizing the amount of goods transported. for safety reasons, there is a certain height limit for the cargo truck which cannot be exceeded.
Input
The input consists of a number of instances. each case starts with two integers, separated by a space, on a line. these two integers are the number of cities (C) and the number of roads (R ). there are at most 1000 cities, numbered from 1. this is followed by R lines each containing the city numbers of the cities connected by that road, the maximum height allowed on that road, and the length of that road. the maximum height for each road is a positive integer, cannot t that a height of-1 indicates that there is no height limit on that road. the length of each road is a positive integer at most 1000. every road can be traveled in both directions ctions, and there is at most one road connecting each distinct pair of cities. finally, the last line of each case consists of the start and end city numbers, as well as the height limit (a positive integer) of the cargo truck. the input terminates when C = R = 0.
Output
For each case, print the case number followed by the maximum height of the cargo truck allowed and the length of the shortest route. use the format as shown in the sample output. if it is not possible to reach the end city from the start city, print "cannot reach destination" after the case number. print a blank line between the output of the cases.
Sample Input
5 6
1 2 7 5
1 3 4 2
2 4-1 10
2 5 2 4
3 4 10 1
4 5 8 5
1 5 10
5 6
1 2 7 5
1 3 4 2
2 4-1 10
2 5 2 4
3 4 10 1
4 5 8 5
1 5 4
3 1
1 2-1 100
1 3 10
0 0
Sample Output
Case 1:
Maximum height = 7
Length of shortest route = 20
Case 2:
Maximum height = 4
Length of shortest route = 8
Case 3:
Cannot reach destination
Question:
On a map, each path has a length and its restricted height. A truck needs to be loaded from location A to location B, and the maximum h height of the truck can be loaded. What is the shortest path when this truck can hold the highest cargo from A to B.
Analysis and Summary:
Similar to HDU 1839. The number of paths from A to B that meet the condition is gradually decreased until there is no path. Therefore, to satisfy the monotonicity, we can use the binary method to divide the truck's carrying capacity into two parts and find the shortest path. If you can find the shortest path, left = mid + 1; otherwise right = mid.
Note: You must save the answer while finding the shortest path.
Code:
[Cpp]
# Include <iostream>
# Include <cstdio>
# Include <cstring>
# Include <queue>
Using namespace std;
Const int INF = 0x7fffffff;
Const int VN = 1005;
Const int EN = VN * VN/2;
Struct Edge {
Int v, next;
Int h, len;
} E [EN];
Int n;
Int m;
Int size;
Int head [VN];
Int d [VN];
Int limit;
Bool inq [VN];
Void init (){
Size = 0;
Memset (head,-1, sizeof (head ));
}
Void addEdge (int u, int v, int h, int l ){
E [size]. v = v;
If (h! =-1) E [size]. h = h;
Else E [size]. h = INF;
E [size]. len = l;
E [size]. next = head [u];
Head [u] = size ++;
}
Int SPFA (int src, int end ){
For (int I = 1; I <= n; ++ I) d [I] = INF;
Memset (inq, 0, sizeof (inq ));
Queue <int> q;
D [src] = 0;
Q. push (src );
While (! Q. empty ()){
Int u = q. front (); q. pop ();
Inq [u] = false;
For (int e = head [u]; e! =-1; e = E [e]. next) if (E [e]. h> = limit ){
Int tmp = d [u] + E [e]. len;
If (d [E [e]. v]> tmp ){
D [E [e]. v] = tmp;
If (! Inq [E [e]. v]) {
Inq [E [e]. v] = true;
Q. push (E [e]. v );
}
}
}
}
Return d [end];
}
Int main (){
Int cas = 1, u, v, h, l, start, end, hh;
While (~ Scanf ("% d", & n, & m) & n + m ){
If (cas! = 1) puts ("");
Init ();
For (int I = 0; I <m; ++ I ){
Scanf ("% d", & u, & v, & h, & l );
AddEdge (u, v, h, l );
AddEdge (v, u, h, l );
}
Scanf ("% d", & start, & end, & hh );
Int ans, ans_h = 0, ans_len = INF, left = 0, right = hh + 1;
While (left <right ){
Limit = (left + right)> 1;
Ans = SPFA (start, end );
If (ans! = INF ){
Left = limit + 1;
If (limit> ans_h ){
Ans_h = limit;
Ans_len = ans;
}
Else if (limit = ans_h & ans <ans_len ){
Ans_len = ans;
}
}
Else {
Right = limit;
}
}
Printf ("Case % d: \ n", cas ++ );
If (ans_len = INF ){
Puts ("cannot reach destination ");
}
Else {
Printf ("maximum height = % d \ n", ans_h );
Printf ("length of shortest route = % d \ n", ans_len );
}
}
Return 0;
}