首页 > 其他 > 详细

HDU-2962-Trucking(最短路)

时间:2014-04-19 01:34:20      阅读:509      评论:0      收藏:0      [点我收藏+]
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


思路:用Dijkstra的思想来求最大高度,还要再用Dijkstra求最短路。


#include <cstdio>
#include <algorithm>
#define INF 99999999
using namespace std;

int h[1001],d[1001],maph[1001][1001],mapd[1001][1001];
bool vis[1001];

int main()
{
    int n,m,i,j,s,e,lim,t,a,b,c,mm,mi,casenum=1;

    while(~scanf("%d%d",&n,&m) && n)
    {
        for(i=1;i<=n;i++) vis[i]=0,h[i]=-1,d[i]=INF;
        for(i=1;i<=n;i++) for(j=1;j<=n;j++) maph[i][j]=0,mapd[i][j]=INF;

        while(m--)
        {
            scanf("%d%d%d%d",&a,&b,&c,&t);

            mapd[a][b]=mapd[b][a]=t;

            if(c!=-1) maph[a][b]=maph[b][a]=c;
            else maph[a][b]=maph[b][a]=INF;
        }

        scanf("%d%d%d",&s,&e,&lim);

        t=n-1;
        h[s]=lim;
        d[s]=0;
        while(t--)//用Dijkstra的思想来求最大高度
        {
            mm=0;
            for(i=1;i<=n;i++)
            {
                if(!vis[i] && h[i]>mm)
                {
                    mm=h[i];
                    mi=i;
                }
            }

            vis[mi]=1;
            if(mi==e) break;

            for(i=1;i<=n;i++)
            {
                if(!vis[i] && mapd[mi][i]<INF)
                {
                    if(h[i]<min(h[mi],maph[mi][i])) h[i]=min(h[mi],maph[mi][i]);
                }
            }
        }

        for(i=1;i<=n;i++) vis[i]=0;//求最短距离

        t=n-1;
        d[s]=0;
        while(t--)
        {
            mm=INF;
            for(i=1;i<=n;i++)
            {
                if(!vis[i] && d[i]<mm)
                {
                    mm=d[i];
                    mi=i;
                }
            }

            vis[mi]=1;
            if(mi==e) break;

            for(i=1;i<=n;i++)
            {
                if(!vis[i] && mapd[mi][i]<INF && maph[mi][i]>=h[e])
                {
                    if(d[i]>d[mi]+mapd[mi][i])
                    {
                        d[i]=d[mi]+mapd[mi][i];
                    }
                }
            }
        }

        if(casenum>1) printf("\n");
        printf("Case %d:\n",casenum++);
        if(d[e]<INF) printf("maximum height = %d\nlength of shortest route = %d\n",h[e],d[e]);
        else printf("cannot reach destination\n");
    }
}


HDU-2962-Trucking(最短路),布布扣,bubuko.com

HDU-2962-Trucking(最短路)

原文:http://blog.csdn.net/faithdmc/article/details/24033157

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!