首页 > 其他 > 详细

hdu2962 Trucking (最短路+二分查找)

时间:2014-08-21 17:03:24      阅读:327      评论: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
 
 
这个题花了我和我老婆一下午的时间 本来以为很简单 后来越想越复杂 改了好多遍 最后还PE了一遍T^T,唉 。。。。结果还被我老婆说了
 
题目大意是有n个点m条路 每条路都有一个限高,最后给出一个起点 一个终点和车所能装货物的最大高度,求在车装货物高度尽量高的情况下的最短路
 
先用二分列举货物的高度 再用SPFA找最短路就OK0.0,,,噢噢噢噢 最后要注意下输出格式 是在测试数据之间有空行
 
#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
using namespace std;

struct node
{
    int h,len;
} map[1010][1010];

int start,end,height,c;
int node[1010];
const int inf=9999999;

int Spfa(int high)
{
    for (int i=1;i<=c;i++)
    node[i]=inf;
    queue<int>q;
    int inq[1010]= {0};
    int tm=start;
    node[tm]=0;
    inq[tm]=1;
    q.push(tm);
    while (!q.empty())
    {
        int s=q.front();
        q.pop();
        for (int i=1; i<=c; i++)
        {
            //cout<<s<<i<<" "<<node[i]<<" "<<map[s][i].len<<endl;
            if (map[s][i].h>=high&&node[i]>map[s][i].len+node[s])
            {
                node[i]=map[s][i].len+node[s];
                //cout<<"   "<<i<<" "<<node[i]<<endl;
                if (!inq[i])
                {
                    q.push(i);
                    inq[i]=1;
                }
            }
        }
        inq[s]=0;

    }
    if (node[end]!=inf)
        return node[end];
    else
        return -1;
}

int main ()
{
    int r,maxx,minn,h,k=1;
    while (cin>>c>>r&&(c||r))
    {
        int ans=-1,cmp=-1;
        for(int i=1;i<=c;i++)
        {
            for(int j=1;j<=c;j++)
            {
                map[i][j].len=inf;
                map[i][j].h=0;
            }
        }
        maxx=0,minn=inf;
        for (int i=1; i<=r; i++)
        {
            int a,b,len;

            cin>>a>>b>>h>>len;
            if(h==-1) h=inf;
            if (minn>h) minn=h;
            if (maxx<h) maxx=h;
            //cout<<minn<<" "<<maxx<<endl;
            if (map[a][b].len>len)
                map[a][b].len=map[b][a].len=len;
            if (map[a][b].h<h)
                map[a][b].h=map[b][a].h=h;
        }
        cin>>start>>end>>height;
        maxx=height>maxx?maxx:height;
        int l=minn,r=maxx;
        while (l<=r)
        {
            int mid=(r+l)>>1;
            //cout<<l<<" "<<r<<" "<<mid<<endl;
            int flag=Spfa(mid);
            if (flag!=-1)
            {
                l=mid+1;
                ans=mid;
                cmp=flag;
            }
            else
                r=mid-1;
        }
        if(k>1)
        printf("\n");
        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;
}

  

hdu2962 Trucking (最短路+二分查找),布布扣,bubuko.com

hdu2962 Trucking (最短路+二分查找)

原文:http://www.cnblogs.com/mis-xiao/p/3927449.html

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