poj 1797 Heavy Transportation Dijkstra&&Kruskal

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
Sample Output
Scenario #1:
4

题意概括:有n路口,m条路,每条路都有其能承载的最大值,意思就是说如果超过这个最大值就算超重就不能在这条路上走,需要找出一条路径,这条路径在所有路径中拥有最大的承载量。一条路径的最大承载量是这条路径里面所有路的最小承载量,因为肯定要经过这个最小承载量所在的路,如果大于他就是超重了。如样例,可以找出两条路径 1—2—3 里面的承载量分别是3,5,而这条路径的最大承载量就是3,大于3,1到2就无法行走;另一条是1—3承载量是4,所以最后找出的最大承载量是4。

用kruskal算法解决比较简单一点,就是找最短路径中最大的承载量

#include<iostream>
#include<algorithm>
using namespace std;
#define M 1100000
#define N 110000
struct edge 
{
	int start,end,power;
}x[M];
int i,j,m,n;
int per[N];
void init()
{
	for(int i=1;i<=n;i++)  
        per[i]=i; 
}
int find(int x)
{
	return per[x]==x?x:find(per[x]);  
}  
int merge (int x,int y)
{
	int fx=find(x);
	int fy=find(y);
	if(fx!=fy)
	{
		per[fy]=fx;
		return 1;
	}
	return 0;
}
 bool cmp(edge a,edge b)
{
	return a.power>b.power;
}
void Kruskal()
{
	int num=0,sum=99999999;
	init();
	for(i=0;i<m;i++)
	{
		if(merge(x[i].start,x[i].end))
		{
			sum=min(sum,x[i].power);
			if(find(1)==find(n))
			{
				break;
			}		
		}	
	}
 
	printf("%d\n\n",sum);
}
int main()
{
	int t=0,T;
	scanf("%d",&T);
	while(T--)
	{
		t++;
		scanf("%d%d",&n,&m);
		for(i=0;i<m;i++)
		{
			scanf("%d%d%d",&x[i].start,&x[i].end,&x[i].power);
		}
		sort(x,x+m,cmp);
		printf("Scenario #%d:\n",t);
		Kruskal();
		
	}	
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_43965640/article/details/87529070