hdu2296||UVALive 4223 Trucking (二分+spfa)

来源:互联网 发布:数组大小可以任意改变 编辑:程序博客网 时间:2024/05/01 17:13
题意

有n个城市,m条双向边,每条边有两个限制,1是高度限制,2是花费

然后让你从s到t,高度不超过c的情况下,最高为多少,保证最高的情况下,花费最小为多少

思路:二分高度,然后跑spfa


#include <cstdio>#include <queue>#include <cstring>#include <iostream>#include <cstdlib>#include <algorithm>#include <vector>#include <map>#include <string>#include <set>#include <ctime>#include <cmath>#include <cctype>using namespace std;#define maxn 1000+10#define maxm 1000+10#define LL long longint cas=1,T;const int INF = 1<<28;int n,m;struct Edge{int to,heigh,dist;    Edge(int v,int h,int d):to(v),heigh(h),dist(d){}};vector<Edge>G[maxn];int d[maxn];int inq[maxn];void init(){for (int i = 0;i<=n;i++)G[i].clear();}bool spfa(int s,int e,int h){   queue<int>q;   memset(inq,0,sizeof(inq));   for (int i = 0;i<=n;i++)   d[i]=INF;   d[s]=0;   inq[s]=1;   q.push(s);   while (!q.empty())   {   int u = q.front();   q.pop();   inq[u]=0;   for (int i = 0;i<G[u].size();i++)   {   Edge &e = G[u][i];   if (e.heigh < h)   continue;   if (d[u]<INF && d[e.to]>d[u]+e.dist)   {               d[e.to]=d[u]+e.dist;   if (!inq[e.to])   {   q.push(e.to);   inq[e.to]=1;   }   }   }   }   if (d[e]==INF)   return 0;   else   return 1;}int main(){//freopen("in","r",stdin);//scanf("%d",&T);while (scanf("%d%d",&n,&m) && n){if (cas>1)printf("\n");printf("Case %d:\n",cas++);init();for (int i = 0;i<m;i++){int a,b,c,d;scanf("%d%d%d%d",&a,&b,&c,&d);if (c==-1)c=INF;G[a].push_back(Edge(b,c,d));G[b].push_back(Edge(a,c,d));}int s,t,h;scanf("%d%d%d",&s,&t,&h);int mid;int l = 0,r = h;while (l<=r){            mid = (l+r)/2;if (spfa(s,t,mid)){//h=l;l=mid+1;}elser=mid-1;}l--;        spfa(s,t,l);if (d[t]!=INF)        {printf("maximum height = %d\n",l);printf("length of shortest route = %d\n",d[t]);}elseputs("cannot reach destination");}//printf("time=%.3lf",(double)clock()/CLOCKS_PER_SEC);return 0;}

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



0 0
原创粉丝点击