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

hdu2962之二分+最短

編輯:C++入門知識

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<iostream>   
#include<cstdio>   
#include<cstdlib>   
#include<cstring>   
#include<string>   
#include<queue>   
#include<algorithm>   
#include<map>   
#include<iomanip>   
#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]<dist[i] && height[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<<endl;  
        cout<<"Case "<<++num<<":\n";  
        if(maxh == -INF)cout<<"cannot reach destination"<<endl;  
        else printf("maximum height = %d\nlength of shortest route = %d\n",right,minl);  
    }  
    return 0;  
}  

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<queue>
#include<algorithm>
#include<map>
#include<iomanip>
#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]<dist[i] && height[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<<endl;
		cout<<"Case "<<++num<<":\n";
		if(maxh == -INF)cout<<"cannot reach destination"<<endl;
		else printf("maximum height = %d\nlength of shortest route = %d\n",right,minl);
	}
	return 0;
}

 

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