hdu 2962 Trucking 最短路+二分。。Dijkstra+SPFA两种算法实现。

来源:互联网 发布:flash编程pdf 编辑:程序博客网 时间:2024/05/08 23:00

Trucking

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


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 61 2 7 51 3 4 22 4 -1 102 5 2 43 4 10 14 5 8 51 5 105 61 2 7 51 3 4 22 4 -1 102 5 2 43 4 10 14 5 8 51 5 43 11 2 -1 1001 3 100 0
 

Sample Output
Case 1:maximum height = 7length of shortest route = 20Case 2:maximum height = 4length of shortest route = 8Case 3:cannot reach destination
 通过枚举高度,使高度尽可能的高的前提下求最短路。
如果真的一个一个枚举会超时,但是通过二分就可以了。
Dijkstra代码:
#include <stdio.h>#include <string.h>#define INF 1000000000#define MAX 1010int dis[MAX] ;struct Graph{int len , height ;}graph[MAX][MAX];bool visited[MAX] ;int s , d ;int dijkstra(int n , int limit){for(int i = 1 ; i <= n ; ++i){if(graph[s][i].height<limit){dis[i] = INF ;}else{dis[i] = graph[s][i].len ;}visited[i] = false ;}dis[s] = 0 ;visited[s] = true ;for(int i = 1 ; i < n ; ++i){int index = -1 , min = INF ;for(int j = 1 ; j <= n ; ++j){if(!visited[j] && min>dis[j]){min = dis[j] ; index = j ;}}if(index == -1){return dis[d];}visited[index] = true ;for(int j = 1 ; j <= n ;  ++j){if(graph[index][j].height<limit){continue ;}if(!visited[j] && dis[j]>dis[index]+graph[index][j].len){dis[j] = dis[index]+graph[index][j].len ;}}}return dis[d] ;}int main(){int c , r , cs = 1;while(~scanf("%d%d",&c,&r)&&(c+r)){if(cs != 1)puts("") ;for(int i = 0 ; i <= c ; ++i){for(int j = 0 ; j <= i ; ++j){graph[i][j].len = graph[j][i].len = INF ;}}for(int i = 0 ; i < r ; ++i){int x , y , h , l ;scanf("%d%d%d%d",&x,&y,&h,&l) ;if( h == -1)h = INF ;graph[x][y].len = graph[y][x].len = l ;graph[x][y].height = graph[y][x].height = h ;}int limit ;scanf("%d%d%d",&s,&d,&limit) ;int i = 1 , mid=1 , j =limit , ans = INF ;while(i<=j){mid = (i+j)/2 ;int temp = dijkstra(c,mid) ;if(temp != INF){ans = temp ;i = mid + 1 ;}elsej = mid - 1 ;}printf("Case %d:\n",cs++);  if(ans != INF){printf("maximum height = %d\n",j);              printf("length of shortest route = %d\n",ans);  }else              printf("cannot reach destination\n"); }return 0 ;}


SPFA代码:
#include <cstdio>#include <cstring>#include <deque>#define INF 1000000000#define MAX 1010using namespace std ;int dis[MAX] ;struct Graph{int len , height ;}graph[MAX][MAX];bool visited[MAX] ;int s , d ;int spfa(int n , int limit){int c[MAX] ;for(int i = 1 ; i <= n ; ++i){dis[i] = INF ;c[i] = 0 ;visited[i] = false ;}dis[s] = 0 ;visited[s] = true ;deque<int> que ;que.push_back(s) ;while(!que.empty()){int k = que.front() ;visited[k] = false ;que.pop_front() ;c[k]++ ;if(c[k]>n){break ;}for(int i = 1 ; i <= n ; ++i){if(graph[k][i].height<limit)continue ;if(dis[i]>dis[k]+graph[k][i].len){dis[i] = dis[k]+graph[k][i].len ;if(que.empty())que.push_back(i) ;else{if(dis[i]>dis[que.front()])que.push_back(i) ;elseque.push_front(i) ; }visited[i] = true ;}}}return dis[d] ;}int main(){int c , r , cs = 1;while(~scanf("%d%d",&c,&r)&&(c+r)){if(cs != 1)puts("") ;for(int i = 0 ; i <= c ; ++i){for(int j = 0 ; j <= i ; ++j){graph[i][j].len = graph[j][i].len = INF ;}}for(int i = 0 ; i < r ; ++i){int x , y , h , l ;scanf("%d%d%d%d",&x,&y,&h,&l) ;if( h == -1)h = INF ;graph[x][y].len = graph[y][x].len = l ;graph[x][y].height = graph[y][x].height = h ;}int limit ;scanf("%d%d%d",&s,&d,&limit) ;int i = 1 , mid=1 , j =limit , ans = INF ;while(i<=j){mid = (i+j)/2 ;int temp = spfa(c,mid) ;if(temp != INF){ans = temp ;i = mid + 1 ;}elsej = mid - 1 ;}printf("Case %d:\n",cs++);  if(ans != INF){printf("maximum height = %d\n",j);              printf("length of shortest route = %d\n",ans);  }else              printf("cannot reach destination\n"); }return 0 ;}

与君共勉
0 0
原创粉丝点击