首页 > 代码库 > hdu 2962 Trucking (二分+最短路Spfa)
hdu 2962 Trucking (二分+最短路Spfa)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=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 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 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 = 7
length of shortest route = 20
Case 2:
maximum height = 4
length of shortest route = 8
Case 3:
cannot reach destination
这一题是和某大神一起a的,结果被他说成了是一个人写的代码,哼,不再和他愉快的玩耍了!!!
好,不多说了,我们讨论这一题。
题目大意:
(1)这一题的输入数据有很多,第一行是两个数,表示城市和路,从第二行开始就是城市与城市之间的限高和路长。最后一行输入的是起始和终止点,还有一个就是卡车所能承受的最大高度。
(2)所以这题想输出的是从起始到结束最大高度下的最短路~~~注意题目所给的限高哦。
(3)还有一个就是当限高为-1时,那么这条路就是没有高度的限制。
(4)还有在中间有一个放行,小心PE。
(5)对了,还有最最后一个注意的就是这里采取二分来找能满足条件的最大高度,能够节省很大的时间~~
详见代码。
#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<h) maxx=h; //cout<<minn<<" "<<maxx<<endl; if (map[a][b].len>len) map[a][b].len=map[b][a].len=len; if (map[a][b].h<h) map[a][b].h=map[b][a].h=h; } cin>>start>>end>>height; maxx=height>maxx?maxx:height; int l=minn,r=maxx; while (l<=r) { int mid=(r+l)>>1; //cout<<l<<" "<<r<<" "<<mid<<endl; int flag=Spfa(mid); if (flag!=-1) { l=mid+1; ans=mid; cmp=flag; } else r=mid-1; } /*for (int i=minn;i<=maxx;i++)//这就是所谓的超时的方法。。。 { int flag=Spfa(i); if(flag!=-1) { ans=i; cmp=flag; } }*/ if (k>1) printf ("\n");//注意这里的格式问题,小心PE哦~ printf("Case %d:\n",k++); if(ans==-1) printf("cannot reach destination\n"); else { printf ("maximum height = %d\n",ans); printf ("length of shortest route = %d\n",cmp); } //printf("\n"); } return 0;}
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。