Trucking |
Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) |
Total Submission(s): 135 Accepted Submission(s): 56 |
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. |
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 |
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 |
Source
|
(1)这一题的输入数据有很多,第一行是两个数,表示城市和路,从第二行开始就是城市与城市之间的限高和路长。最后一行输入的是起始和终止点,还有一个就是卡车所能承受的最大高度。
(2)所以这题想输出的是从起始到结束最大高度下的最短路~~~注意题目所给的限高哦。
(3)还有一个就是当限高为-1时,那么这条路就是没有高度的限制。
思路:
用二分枚举 h 最短路多跑几次就行了。这个是双向图。
一眼没看到又 wa 了十多分钟
mdzz
AC代码:
#include<iostream>
#include<string.h>
#include<stdio.h>
#include<vector>
#include<queue>
using namespace std;
struct road
{
int end;
int cost;
int h;
friend bool operator < (const road &a,const road &b)
{
return a.cost>b.cost;
}
};
priority_queue<road> que;
vector<road> v[1005];
bool vis[1005];
int main()
{
int n,m,i,j;
int a,b,h,c;
int s,e;
road p;
int E;
int k=1;
int ans,cmp;
int minn,maxx;
while(scanf("%d%d",&n,&m))
{
if(n==0&&m==0)
break;
ans=cmp=-1;
minn=9999999;
maxx=-1;
for(i=0;i<1005;i++)
v[i].clear();
for(i=0;i<m;i++)
{
scanf("%d%d%d%d",&a,&b,&h,&c);
if(h==-1)
h=9999999;
if(minn>h)
minn=h;
if(maxx<h)
maxx=h;
p.h=h;
p.end=b;
p.cost=c;
v[a].push_back(p);
p.end=a;
v[b].push_back(p);
}
scanf("%d%d%d",&s,&e,&h);
maxx=h>maxx?maxx:h;
int l=minn;
int r=maxx;
int mid;
int falg;
while(l<=r)
{
mid=(l+r)/2;
p.end=s;
p.cost=0;
que.push(p);
road now,next;
falg=0;
memset(vis,0,sizeof(vis));
while(!que.empty())
{
//cout<<"now "<<now.end<<endl;
now=que.top();
que.pop();
if(vis[now.end])
continue;
vis[now.end]=true;
if(now.end==e)
{
falg=1;
break;
}
int d=v[now.end].size();
for(i=0;i<d;i++)
{
next.end=v[now.end][i].end;
if(vis[next.end])
continue;
if(v[now.end][i].h<mid)
continue;
next.cost=v[now.end][i].cost+now.cost;
que.push(next);
}
}
while(!que.empty())
{
que.pop();
}
if(falg)
{
l=mid+1;
ans=mid;
cmp=now.cost;
}
else
{
r=mid-1;
}
}
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);
}
}
return 0;
}