码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2015-04-05 09:16:13      阅读:208      评论:0      收藏:0      [点我收藏+]

标签:hdu2962   trucking   最短路   spfa   dijkstra   

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 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
 通过枚举高度,使高度尽可能的高的前提下求最短路。
如果真的一个一个枚举会超时,但是通过二分就可以了。
Dijkstra代码:
#include <stdio.h>
#include <string.h>
#define INF 1000000000
#define MAX 1010

int 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 ;
			}
			else
				j = 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 1010
using 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) ;
					else
						que.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 ;
			}
			else
				j = 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 ;
}

与君共勉

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

标签:hdu2962   trucking   最短路   spfa   dijkstra   

原文地址:http://blog.csdn.net/lionel_d/article/details/44874443

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!