UVA4223 两遍spfa

      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

题意:最短路问题,对于每条路有一个高度限制和一个长度,货车本身有一个高度限制,当货车经过某条路时货车的高度不能超过这条路的高度限制和货车本身的高度限制,优先考虑高度,当两条路高度限制一样时选择更短的那条,按格式输出货车的最大高度和此时通过的路径的长度。

一开始我是按poj1797这题的思路来写的,对路径高度松弛的时候顺便改变路径长度,当高度一样时去松弛路径长度,但wa了7,8次后我再绝望的时候大佬发现了这样一组数据,如下:

poj1797:https://blog.csdn.net/ecjtu_17_TY/article/details/81272562

假设A为起点E为终点,对与这组数据按一开始的思路跑出来答案是( 4,11 ),而正确答案显而易见应该是( 4,5 ),当跑到D的时候都是对的,此时D的状态应该是( 5,10 ),再从D到E就出现问题了,此时E的状态是( 4 , 11 ),而显然( 4 ,5 ),才是符合题意的。

然后就是现在这个思路了:我们先跑一遍,这一遍只跑高度,不要去管长度,跑完之后可以得到一个A到E的最大高度,然后把这个高度当成松弛条件再跑一遍去求最短路,如果这个高度为0不存在就不跑,这样就做完了,emmmmmmmm,没什么想说的,以后抱紧大佬大腿就好了  嘤嘤嘤~~~~~~~~~

这里我用的spfa,至于dijkstra,bellman-ford和floyd没试,嘤嘤嘤~~~~~~~~~

AC代码:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<string>
#include<deque>
#include<queue>
#include<list>
const int inf=0x3f3f3f3f;
const int MOD=1e9+7;
#define LL long long
#define ME0(x) memset(x,0,sizeof(x))
#define MEI(x) memset(x,inf,sizeof(x))
#define MEF(x) memset(x,-1,sizeof(x))
using namespace std;
int n,m,s,e,h,d,t=0,cnt;
int first[1005],nextt[2000000+10],hig[1005],dis[1005],flag[1005];
queue<int> q;
struct LX
{
    int st,ed,hi,di;
}lx[2000000+10];
void add(int st,int ed,int hi,int di)
{
    cnt++;
    lx[cnt].st=st;
    lx[cnt].ed=ed;
    lx[cnt].hi=hi;
    lx[cnt].di=di;
    nextt[cnt]=first[st];
    first[st]=cnt;
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        t++;
        if(n==0&&m==0)
        {
            break;
        }
        ME0(first);
        cnt=0;
        for(int m1=1;m1<=m;m1++)
        {
            scanf("%d%d%d%d",&s,&e,&h,&d);
            add(s,e,h,d);
            add(e,s,h,d);
        }
        scanf("%d%d%d",&s,&e,&h);
        for(int cnt1=1;cnt1<=cnt;cnt1++)
        {
            if(lx[cnt1].hi==-1||lx[cnt1].hi>h)
            {
                lx[cnt1].hi=h;
            }
        }
        ME0(flag);
        ME0(hig);
        q.push(s);
        flag[s]=1;
        hig[s]=inf;
        while(!q.empty())
        {
            int a=q.front();
            q.pop();
            flag[a]=0;
            for(int i=first[a];i!=0;i=nextt[i])
            {
                int b=lx[i].ed;
                if(min(hig[a],lx[i].hi)>hig[b])
                {
                    hig[b]=min(hig[a],lx[i].hi);
                    if(!flag[b])
                    {
                        q.push(b);
                        flag[b]=1;
                    }
                }
            }
        }
        if(hig[e]>=1)
        {
            ME0(flag);
            MEI(dis);
            q.push(s);
            flag[s]=1;
            dis[s]=0;
            while(!q.empty())
            {
                int a=q.front();
                q.pop();
                flag[a]=0;
                for(int i=first[a];i!=0;i=nextt[i])
                {
                    int b=lx[i].ed;
                    if(dis[b]>dis[a]+lx[i].di&&lx[i].hi>=hig[e]&&hig[a]>=hig[e])
                    {
                        dis[b]=dis[a]+lx[i].di;
                        if(!flag[b])
                        {
                            q.push(b);
                            flag[b]=1;
                        }
                    }
                }
            }
        }
        if(t>1)
        {
            printf("\n");
        }
        printf("Case %d:\n",t);
        if(hig[e]>=1)
        {
            printf("maximum height = %d\n",hig[e]);
            printf("length of shortest route = %d\n",dis[e]);
        }
        else
        {
            printf("cannot reach destination\n");
        }
    }
}

emmmmmmmm,就这样~~~~~~~~~~~·

猜你喜欢

转载自blog.csdn.net/ecjtu_17_TY/article/details/81458789