HDU - 2962 Trucking SPFA+二分
Trucking
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 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.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 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
题意:每条路都有最大限重和长度,有一些货物,求卡车在保证能到达且不超载的前提下最多能拉多少货物,和通过的最短路径。
思路:货物最多的基础上路径最短。枚举货物的重量,求在限重内通过的最短路。普通枚举O(n)*SPFA O(kE)可能会超时,这里用到二分枚举O(logn)来优化时间,然后SPFA求最短路。
#include<stdio.h> #include<string.h> #include<deque> #include<vector> #define MAX 1005 #define INF 0x3f3f3f3f using namespace std; struct Node{ int v,h,w; }node; vector<Node> edge[MAX]; int dis[MAX],b[MAX]; int n,mid; void spfa(int k) { int i; deque<int> q; for(i=1;i<=n;i++){ dis[i]=INF; } memset(b,0,sizeof(b)); b[k]=1; dis[k]=0; q.push_back(k); while(q.size()){ int u=q.front(); for(i=0;i<edge[u].size();i++){ int v=edge[u][i].v; int h=edge[u][i].h; int w=edge[u][i].w; if(dis[v]>dis[u]+w&&(h>=mid||h==-1)){ dis[v]=dis[u]+w; if(b[v]==0){ b[v]=1; if(dis[v]>dis[u]) q.push_back(v); else q.push_front(v); } } } b[u]=0; q.pop_front(); } } int main() { int m,u,v,h,w,bg,ed,hi,l,r,f,i,j; f=0; while(scanf("%d%d",&n,&m)&&!(n==0&&m==0)){ f++; for(i=1;i<=n;i++){ edge[i].clear(); } for(i=1;i<=m;i++){ scanf("%d%d%d%d",&u,&v,&h,&w); node.v=v; node.h=h; node.w=w; edge[u].push_back(node); node.v=u; edge[v].push_back(node); } scanf("%d%d%d",&bg,&ed,&hi); l=0;r=hi;mid=0; int ans1=0,ans2=-1; while(l<=r){ mid=(l+r)/2; //二分 spfa(bg); if(dis[ed]==INF) r=mid-1; else{ ans1=mid; ans2=dis[ed]; l=mid+1; } } if(f!=1) printf("\n"); if(ans2==-1) printf("Case %d:\ncannot reach destination\n",f); else printf("Case %d:\nmaximum height = %d\nlength of shortest route = %d\n",f,ans1,ans2); } return 0; }