HDU 2962 Trucking Shortest circuit + two points. DIJKSTRA+SPFA two kinds of algorithms are implemented.

Source: Internet
Author: User

TruckingTime limit:20000/10000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 1966 Accepted Submission (s): 680


Problem Descriptiona certain local trucking company would 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 would such as 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 are equivalent to maximizing the amount of goods Transported. For safety reasons, there are a certain height limit for the cargo truck which cannot be exceeded.
Inputthe input consists of a number of cases. Each case is starts with the integers, separated by a space, and on a line. These integers is the number of cities (C) and the number of roads (R). There is at most of cities, numbered from 1. This was followed by R lines each containing the city numbers of the cities connected by that road, the maximum height allo Wed on this road, and the length of that road. The maximum height for each road are a positive integer, except that a height of-1 indicates this there is no height limit On that road. The length of each road was a positive integer at most 1000. Every road can be travelled in both directions, and there are at the most one road connecting each distinct pair of cities. Finally, the last line of all case consists of the start and end city numbers, as well as the height limit (a positive in Teger) of the cargo truck. The input terminates when C = R = 0.
Outputfor, 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 isn't 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 61 2 7 51 3 4 22 4-1 102 5 2 43 4 10 14 5 8 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 Output
Case 1:maximum height = 7length of Shortest route = 20Case 2:maximum height = 4length of Shortest route = 8Case 3:cannot R Each destination
The shortest path is obtained by enumerating the heights so that the heights are as high as possible. If a single enumeration is really timed out, it can be done by two points. Dijkstra Code:
#include <stdio.h> #include <string.h> #define INF 1000000000#define MAX 1010int Dis[max]; struct graph{int Len, height;} Graph[max][max];bool Visited[max]; int s, D; int Dijkstra (int n, int limit) {for (int i = 1; I <= n; ++i) {if (Graph[s] [I].height<limit) {dis[i] = INF;} Else{dis[i] = Graph[s][i].len;} Visited[i] = false;} Dis[s] = 0; Visited[s] = true; for (int i = 1; i < n; ++i) {int index =-1, min = INF; for (int j = 1; j <= N; ++j {if (!visited[j] && min>dis[j]) {min = dis[j]; index = j;}} if (index = =-1) {return dis[d];}  Visited[index] = true; for (int j = 1; j <= N; ++J) {if (graph[index][j].height<limit) {continue;} if (!visited[j] && Dis[j]>dis[index]+graph[index][j].len) {dis[j] = Dis[index]+graph[index][j].len;}}} return dis[d];} int main () {int C, r, cs = 1;while (~scanf ("%d%d", &c,&r) && (c+r)) {if (cs! = 1) puts (""); for (int i = 0; I <= C; ++i) {for (int j = 0; J <= i; ++j) {Graph[i][j].len = Graph[j][i].len = INF;}} for (int i = 0; i < R; ++i) {int x, y, H, l; scanf ("%d%d%d%d", &x,&y,&h,&l), if (h = =-1) h = INF; gra Ph[x][y].len = Graph[y][x].len = l; graph[x][y].height = Graph[y][x].height = h;} int limit; scanf ("%d%d%d", &s,&d,&limit); int i = 1, mid=1, J =limit, ans = INF; while (i<=j) {mid = (i+j)/ 2; int temp = Dijkstra (C,mid); if (temp! = INF) {ans = temp; i = mid + 1;} Elsej = mid-1;}  printf ("Case%d:\n", cs++);              if (ans! = INF) {printf ("Maximum height =%d\n", j);  printf ("Length of shortest route =%d\n", ans); }else printf ("Cannot reach destination\n"); }return 0;}


SPFA Code:
#include <cstdio> #include <cstring> #include <deque> #define INF 1000000000#define MAX 1010using namespace std; int Dis[max]; struct graph{int len, height;} Graph[max][max];bool Visited[max]; int s, D; int spfa (int n, int limit) {int C[max]; for (int i = 1; I <= n; ++i) {dis [I] = INF; C[i] = 0; Visited[i] = false;}  Dis[s] = 0; Visited[s] = True;d eque<int> que; Que.push_back (s); while (!que.empty ()) {int k = Que.front (); Visited[k] = false; Que.pop_front (); c[k]++; if (c[k]>n) {break;} for (int i = 1; I <= n; ++i) {if (graph[k][i].height<limit) continue; if (Dis[i]>dis[k]+graph[k][i].len) {dis[i] = di S[k]+graph[k][i].len; if (Que.empty ()) que.push_back (i); Else{if (Dis[i]>dis[que.front ()]) que.push_back (i); Elseque.push_front (i); }visited[i] = true;}}} return dis[d];} int main () {int C, r, cs = 1;while (~scanf ("%d%d", &c,&r) && (c+r)) {if (cs! = 1) puts (""); for (int i = 0; I <= C; ++i) {for (int j = 0; J <= i; ++j) {Graph[i][j].len = GrapH[j][i].len = INF;}} for (int i = 0; i < R; ++i) {int x, y, H, l; scanf ("%d%d%d%d", &x,&y,&h,&l), if (h = =-1) h = INF; gra Ph[x][y].len = Graph[y][x].len = l; graph[x][y].height = Graph[y][x].height = h;} int limit; scanf ("%d%d%d", &s,&d,&limit); int i = 1, mid=1, J =limit, ans = INF; while (i<=j) {mid = (i+j)/ 2; int temp = SPFA (C,mid); if (temp! = INF) {ans = temp; i = mid + 1;} Elsej = mid-1;}  printf ("Case%d:\n", cs++);              if (ans! = INF) {printf ("Maximum height =%d\n", j);  printf ("Length of shortest route =%d\n", ans); }else printf ("Cannot reach destination\n"); }return 0;}

with June

HDU 2962 Trucking Shortest circuit + two points. DIJKSTRA+SPFA two kinds of algorithms are implemented.

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.