当前位置:编程学习 > C/C++ >>

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


题目:
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.
注意,能够求出最短路的同时,还要保存下答案。


代码:
[cpp]
#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){ 
  &n

补充:软件开发 , C++ ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,