Trucking(HDU 2962 最短路+二分搜索)
Trucking
Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2216 Accepted Submission(s): 757
Problem Description
A 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 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.
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 cases. 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, except 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 travelled in both directions, 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
0 0
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
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
题意:给定一个无向图,每条边有长度,通过最大高度两个权值,求解从起点到终点的能通过的最大高度以及在此高度上的最短路径长度
思路:二分搜索,每次进行一次最短路算法Dijkstra,路径更新公式需要添加 map[i][v].h>=p||map[i][v].h==-1
需要注意最后一行不能输出\n PE了两次
1 #include <cstring> 2 #include <iostream> 3 #include <algorithm> 4 #include <cstdio> 5 using namespace std; 6 #define Max 1005 7 #define INF 9999999 8 struct node 9 { 10 int len,h; 11 }map[Max][Max]; 12 bool vis[Max]; 13 int dis[Max]; 14 int c,r; 15 int s,e,height; 16 17 bool Dijkstra(int p) 18 { 19 int i,j; 20 memset(vis,0,sizeof(vis)); 21 for(i=0;i<=c;i++) 22 dis[i]=INF; 23 dis[s]=0; 24 while(true) 25 { 26 int v=0; 27 for(i=1;i<=c;i++) 28 { 29 if(!vis[i]&&(v==0||dis[i]<dis[v])) 30 v=i; 31 } 32 if(v==0) 33 break; 34 vis[v]=1; 35 //cout<<v<<endl; 36 // cout<<"3 "<<dis[3]<<endl; 37 for(i=1;i<=c;i++) 38 { 39 if(vis[i]==0&&(map[i][v].h>=p||map[i][v].h==-1)&&(dis[v]+map[i][v].len<dis[i])) 40 dis[i]=dis[v]+map[i][v].len; 41 } 42 } 43 return dis[e]!=INF; 44 } 45 46 int main() 47 { 48 int i,j; 49 int a,b; 50 int t=1; 51 freopen("in.txt","r",stdin); 52 while(scanf("%d%d",&c,&r)) 53 { 54 if(c==0&&r==0) 55 break; 56 for(i=0;i<=c;i++) 57 for(j=0;j<=c;j++) 58 { 59 map[i][j].h=0; 60 map[i][j].len=INF; 61 } 62 for(i=0;i<r;i++) 63 { 64 scanf("%d%d",&a,&b); 65 scanf("%d%d",&map[a][b].h,&map[a][b].len); 66 map[b][a].h=map[a][b].h; 67 map[b][a].len=map[a][b].len; 68 } 69 scanf("%d%d%d",&s,&e,&height); 70 int lb=0,rb=height+1,mid,ans,length=INF; 71 while(rb-lb>1) 72 { 73 mid=(rb+lb)/2; 74 if(Dijkstra(mid)) 75 { 76 length=dis[e]; 77 ans=mid; 78 lb=mid; 79 } 80 else 81 rb=mid; 82 } 83 if(t!=1) 84 printf("\n"); 85 if(length==INF) 86 printf("Case %d:\ncannot reach destination\n",t++); 87 else 88 { 89 printf("Case %d:\n",t++); 90 printf("maximum height = %d\n",ans); 91 printf("length of shortest route = %d\n",length); 92 } 93 } 94 }