HDU 2962 Trucking(二分+带限制最短路)

本文讨论了一个涉及卡车运输优化的问题,目标是在货物装载高度有限制的情况下,找到从起点到终点的最短路径。通过采用二分法策略,该文提供了一种有效的方法来确定最大允许装载高度与最短路径之间的关系。详细解释了输入格式、输出要求,并附带了一个示例输入和输出,帮助读者理解如何应用此算法解决实际问题。

链接:

http://acm.hdu.edu.cn/showproblem.php?pid=2962


题目:

Trucking

Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1211    Accepted Submission(s): 428


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
 

题目大意:

在一个地图上,每条路径有长度,以及它的限制高度。 一辆卡车要从A地到B地, 卡车的最大能装h高度的货物。 求这辆卡车从A到B所能装下的最高货物的时候, 最短路径是多少。



分析与总结:

HDU 1839 相类似。 卡车载的货物高度h从小到大依次枚举时,满足条件的A到B的路径数量是依次递减的,直到没有路径为止。因此满足单调性,可以用二分法对卡车的载货量进行二分,在求最短路。 如果能够求出最短路,那么left=mid+1, 否则right=mid.

注意,能够求出最短路的同时,还要保存下答案。



代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;

const int INF = 0x7fffffff;
const int VN  = 1005;
const int EN  = VN*VN/2;

struct Edge{
    int v,next;
    int h, len;
}E[EN];

int n;
int m;
int size;
int head[VN];
int d[VN];
int limit;
bool inq[VN];

void init(){
    size=0;
    memset(head, -1, sizeof(head));
}
void addEdge(int u,int v,int h,int l){
    E[size].v=v;
    if(h!=-1)E[size].h=h;
    else E[size].h=INF;
    E[size].len=l;
    E[size].next=head[u];
    head[u]=size++;
}

int SPFA(int src, int end){
    for(int i=1; i<=n; ++i)d[i]=INF;
    memset(inq, 0, sizeof(inq));
    queue<int>q;
    d[src] = 0;
    q.push(src);
    while(!q.empty()){
        int u=q.front(); q.pop();
        inq[u]=false;
        for(int e=head[u]; e!=-1; e=E[e].next)if(E[e].h>=limit){
            int tmp = d[u]+E[e].len;
            if(d[E[e].v] > tmp){
                d[E[e].v] = tmp;
                if(!inq[E[e].v]){
                    inq[E[e].v]=true;
                    q.push(E[e].v);
                }
            }
        }
    }
    return d[end];
}

int main(){
    int cas=1,u,v,h,l,start,end,hh;
    while(~scanf("%d%d",&n,&m) && n+m){
        if(cas!=1)puts("");
        init();
        for(int i=0; i<m; ++i){
            scanf("%d%d%d%d",&u,&v,&h,&l);
            addEdge(u,v,h,l);
            addEdge(v,u,h,l);
        }
        scanf("%d%d%d",&start,&end,&hh);
        
        int ans,ans_h=0,ans_len=INF,left=0, right=hh+1;

        while(left<right){
            limit = (left+right)>>1;
            ans=SPFA(start, end);
            if(ans!=INF){
                left=limit+1;
                if(limit>ans_h){
                    ans_h=limit;
                    ans_len=ans;
                }
                else if(limit==ans_h&&ans<ans_len){
                    ans_len=ans;
                }
            }
            else{
                right=limit;    
            }
        }
        printf("Case %d:\n",cas++);
        if(ans_len==INF){
            puts("cannot reach destination");
        }
        else{
            printf("maximum height = %d\n", ans_h);
            printf("length of shortest route = %d\n", ans_len);
        }
    }
    return 0;
}



 

——  生命的意义,在于赋予它意义。

          
     原创 http://blog.youkuaiyun.com/shuangde800 , By   D_Double  (转载请标明)






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值