程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> HDU 2962 Trucking(二分+帶限制最短路)

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

編輯:C++入門知識


題目:
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){ 
                    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; 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved