UVALive 4223 Trucking 二分+spfa

Trucking

题目连接:

https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2224

Descriptionww.co

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

Hint

题意

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

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

题解:

二分+spfa,裸题

直接跑就好了,注意输出,两个直接必须有空行,最后一个答案不能有空行

其他就没什么坑点了

代码

#include<bits/stdc++.h>
using namespace std;

const int maxn = 1e3+5;
struct node
{
    int x,y,z;
};
vector<node> E[maxn];
int d[maxn];
queue<int> Q;
int inq[maxn];
int n,m;
int spfa(int a,int b,int c)
{
    memset(inq,0,sizeof(inq));
    while(!Q.empty())Q.pop();
    for(int i=1;i<=n;i++)
        d[i]=1e9;
    d[a]=0;
    Q.push(a);
    inq[a]=1;
    while(!Q.empty())
    {
        int now = Q.front();
        Q.pop();
        inq[now]=0;
        for(int i=0;i<E[now].size();i++)
        {
            if(E[now][i].y<c)continue;
            if(d[E[now][i].x]>d[now]+E[now][i].z)
            {
                d[E[now][i].x] = d[now]+E[now][i].z;
                if(!inq[E[now][i].x])
                {
                    inq[E[now][i].x]=1;
                    Q.push(E[now][i].x);
                }
            }
        }
    }
    if(d[b]==1e9)return 0;
    return 1;
}
int main()
{
    int cas = 0;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        cas++;
        if(n==0&&m==0)break;
        for(int i=1;i<=n;i++)
            E[i].clear();
        for(int i=1;i<=m;i++)
        {
            int a,b,c,d;
            scanf("%d%d%d%d",&a,&b,&c,&d);
            if(c==-1)c=1e9;
            node p;
            p.x=b,p.y=c,p.z=d;
            E[a].push_back(p);
            p.x=a;
            E[b].push_back(p);
        }
        int a,b,c;scanf("%d%d%d",&a,&b,&c);
        int l = 0,r = c;
        while(l<=r)
        {
            int mid = (l+r)/2;
            if(spfa(a,b,mid))l=mid+1;
            else r=mid-1;
        }
        l--;
        spfa(a,b,l);
        if(d[b]==1e9)
        {
            if(cas!=1)printf("\n");
            printf("Case %d:\n",cas);
            printf("cannot reach destination\n");

        }
        else
        {
            if(cas!=1)printf("\n");
            printf("Case %d:\n",cas);
            printf("maximum height = %d\n",l);
            printf("length of shortest route = %d\n",d[b]);
        }
    }
}
posted @ 2016-01-22 21:23  qscqesze  阅读(221)  评论(1编辑  收藏  举报