Heavy Transportation (迪杰斯拉变形)

Background
Hugo Heavy is happy. After the breakdown of the Cargolifter project he can now expand business. But he needs a clever man who tells him whether there really is a way from the place his customer has build his giant steel crane to the place where it is needed on which all streets can carry the weight.
Fortunately he already has a plan of the city with all streets and bridges and all the allowed weights.Unfortunately he has no idea how to find the the maximum weight capacity in order to tell his customer how heavy the crane may become. But you surely know.

Problem
You are given the plan of the city, described by the streets (with weight limits) between the crossings, which are numbered from 1 to n. Your task is to find the maximum weight that can be transported from crossing 1 (Hugo's place) to crossing n (the customer's place). You may assume that there is at least one path. All streets can be travelled in both directions.

Input

The first line contains the number of scenarios (city plans). For each city the number n of street crossings (1 <= n <= 1000) and number m of streets are given on the first line. The following m lines contain triples of integers specifying start and end crossing of the street and the maximum allowed weight, which is positive and not larger than 1000000. There will be at most one street between each pair of crossings.

Output

The output for every scenario begins with a line containing "Scenario #i:", where i is the number of the scenario starting at 1. Then print a single line containing the maximum allowed weight that Hugo can transport to the customer. Terminate the output for the scenario with a blank line.

Sample Input

1
3 3
1 2 3
1 3 4
2 3 5

题目大意:从起点1到终点n,找出到终点的每条路径中最小的值,在这些最小值中找到最大值输出即可。

解题思路:迪杰斯拉的变形,一开始题意没理解,现在举个例子,

1
4 4
1 2 8
1 3 10
2 4 7
3 4 5
1->2->n(4)这条路的最小值为7
1->3->n(4)这条路的最小值为5
7>5所以最后的值为7.

先找到离起点权值最大的点,先存在dis[v]里,这时候dis[v]存的是直接到点的值,再通过这个点绕行,比如这个点是u,这时候就不是一条路,那么再比较dis[v]与dis[u],e[u][v]的最小值,若dis[v]比最小值小的话,更新dis[v].最后dis[n]就是要求的值。

代码:

#include <stdio.h>
#include <string.h> 
#define inf 99999999		
int book[1010];
int dis[1010];
int e[1010][1010];

int min(int a,int b)	
{
	if(a>b)
		return b;
	return a;
}

int main()
{
	int t,n,m,a,b,x;
	int i,j,minn,u,v,count=1;
	scanf("%d",&t);
	while(t--)
	{
		memset(e,0,sizeof(e));		
		memset(book,0,sizeof(book));
		scanf("%d%d",&n,&m);
		for(i=1;i<=n;i++)		
			for(j=1;j<=n;j++)
			{
				if(i==j)
					e[i][j]=0;
				else
					e[i][j]=-1; 
			}
		for(i=1;i<=m;i++)		
		{
			scanf("%d%d%d",&a,&b,&x);
			if(x>e[a][b])			
			{					
				e[a][b]=x;
				e[b][a]=x;		
			}
		}
		printf("Scenario #%d:\n",count++);
		for(i=1;i<=n;i++)			
			dis[i]=e[1][i];
		book[1]=1;
		for(i=1;i<=n-1;i++)		
		{
			minn=-1;
			for(j=1;j<=n;j++){		//找到离起点权值最大的那条边 
				if(book[j]==0&&dis[j]>minn){ 
					minn=dis[j];
					u=j; 
				} 
			}
			book[u]=1;				
			for(v=1;v<=n;v++)
				if(dis[v]<min(dis[u],e[u][v])) 
					dis[v]=min(dis[u],e[u][v]); 
		}
		//for(i=1;i<=n;i++) 
			printf("%d\n\n",dis[n]);	 
	}
	return 0;
} 

猜你喜欢

转载自blog.csdn.net/hello_cmy/article/details/81223350