设为首页 加入收藏

TOP

hdu2962之二分+最短(一)
2014-11-23 20:10:28 来源: 作者: 【 】 浏览:9
Tags:hdu2962 二分 最短

Trucking
Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1473 Accepted Submission(s): 509


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

Source
2008 Rocky Mountain Regional


题目给定了m条路,从a到b限制通过的高度为h,距离为l

现在要求从s到t,车能载的高度最高为h,求车能从s到t载的最高高度为多少,最短路距离是多少

分析:对车能载的高度进行二分,然后求最短路

#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#define INF 99999999   
using namespace std;  
  
const int MAX=1000+10;  
int dist[MAX],height[MAX][MAX],road[MAX][MAX];  
int n,m,maxh,minl;  
bool mark[MAX];  
  
bool dijkstra(int s,int t,int h){  
    for(int i=1;i<=n;++i){  
        if(height[s][i]>=h)dist[i]=road[s][i];  
        else dist[i]=INF;  
        mark[i]=false;  
    }  
    dist[s]=INF,mark[s]=true;  
    for(int k=1;k<=n;++k){  
        int point=s;  
        for(int i=1;i<=n;++i){  
            if(dist[point]>dist[i] && !mark[i])point=i;  
        }  
        if(point == s)return false;  
        if(point == t){minl=dist[t];maxh=h;return true;}  
        mark[point]=true;  
        for(int i=1;i<=n;++i){  
            if(!mark[i] && dist[point]+road[point][i]=h){  
                dist[i]=road[point][i]+dist[point];  
            }  
        }  
    }  
}   
  
void Init(int n){  
    for(int i=1;i<=n;++i){   
        for(int j=i+1;j<=n;++j){  
            road[i][j]=road[j][i]=INF;  
            height[i][j]=height[j][i]=-INF;  
        }  
    }  
}  
  
int main(){  
    int a,b,l,h,left,right,mid,num=0;  
    while(scanf("%d%d",&n,&m),n+m){  
        Init(n);  
        for(int i=1;i<=m;++i){  
            scanf("%d%d%d%d",&a,&b,&h,&l);  
            road[a][b]=l,height[a][b]=h;  
            road[b][a]=l,height[b][a]=h;  
            if(h == -1)height[a][b]=height[b][a]=INF;  
        }  
        scanf("%d%d%d",&a,&b,&h);  
        left=0,right=h,maxh=-INF;  
        while(left<=right){  
            mid=left+right>>1;  
            if(dijkstra(a,b,mid))left=mid+1;  
            else right=mid-1;  
        }  
        if(h == -1)dijkstra(a,b,-1);  
        if(num)cout<
#include
#include
#include
#include
#include
#include
#include
#include
#define INF 99999999
using namespace std;

cons
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇UVA 507 Jill Rides Again (连续.. 下一篇UVA 108 Maximum Sum(子矩阵最大..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·C 内存管理 | 菜鸟教 (2025-12-26 20:20:37)
·如何在 C 语言函数中 (2025-12-26 20:20:34)
·国际音标 [ç] (2025-12-26 20:20:31)
·微服务 Spring Boot (2025-12-26 18:20:10)
·如何调整 Redis 内存 (2025-12-26 18:20:07)