HDU 2962 trucking (Binary + shortest spfa)

Source: Internet
Author: User

Question link: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 2962

Trucking

Time Limit: 20000/10000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 1763 accepted submission (s): 618


Problem descriptiona 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.

 

Inputthe 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.

 

Outputfor 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 input5 61 2 7 51 3 4 22 4-1 102 5 2 43 4 10 14 5 5 51 5 105 61 2 7 51 3 4 22 4-1 102 5 2 43 4 10 14 5 8 51 5 43 11 2-1 1001 3 100 0

 

Sample outputcase 1: maximum height = 7 length of Shortest Route = 20 Case 2: maximum height = 4 length of Shortest Route = 8 case 3: cannot reach destination is a's question with a great god. It turns out to be a code written by a person. Well, it's no longer a pleasure to play with him !!! Okay. Let's talk about this question. (1) There are a lot of input data for this question. The first line is two numbers, indicating the city and road. From the second line, there is a limit between the city and the city. The last line is the start and end points, and the maximum height that the truck can afford. (2) Therefore, the question is output from the start to the end of the maximum height of the shortest ~~~ Note that the question is too high. (3) When the limit is-1, this path has no limit on height. (4) There is also a release in the middle. Be careful with PE. (5) By the way, the last note is that we can take two points to find the maximum height that can meet the conditions and save a lot of time ~~ For details, see the code.
# Include <iostream> # include <cstdio> # include <queue> # include <cstring> using namespace STD; struct node {int H, Len ;} map [1010] [1010]; int start, end, height, C; int node [1010]; const int INF = 9999999; int spfa (INT high) {for (INT I = 1; I <= C; I ++) node [I] = inf; queue <int> q; int INQ [1010] = {0 }; int TM = start; node [Tm] = 0; INQ [Tm] = 1; q. push (TM); While (! Q. empty () {int S = Q. front (); q. pop (); For (INT I = 1; I <= C; I ++) {// cout <S <I <"" <node [I] <"" <map [s] [I]. len <Endl; If (Map [s] [I]. h> = high & node [I]> map [s] [I]. len + node [s]) {node [I] = map [s] [I]. len + node [s]; // cout <"" <I <"" <node [I] <Endl; If (! INQ [I]) {q. Push (I); INQ [I] = 1 ;}} INQ [s] = 0 ;}if (node [end]! = Inf) return node [end]; else return-1;} int main () {int R, Maxx, Minn, H, k = 1; while (CIN> C> r & (c | r) {int ans =-1, CMP =-1; for (INT I = 1; I <= C; I ++) {for (Int J = 1; j <= C; j ++) {map [I] [J]. len = inf; Map [I] [J]. H = 0 ;}} Maxx = 0, Minn = inf; For (INT I = 1; I <= r; I ++) {int A, B, Len; cin> A> B> H> Len; If (H =-1) h = inf; If (Minn> H) Minn = h; if (Maxx 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.