poj1797(最短路,Dijkstra算法)

题目:
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

题解:(1)运用一下Dijkstra算法并把条件从最短路径改为最大承重量即可
(2)用一个二维数组储§存某路到某路的承重量。用一个一维数组(q)存从路口一到路口i的最大承重量(最大承载量为路过的路口中承载量最小的)。
(3)一开始找出路口一能直接到的路口,找出其中最大的承载量,比如为1–n,那么q[n]的最大承载量就是p[1][n],因为路口[n]为路口一能直接到的路口中最大承载量了,不可能存在从先经过其他路口再到路口n而最大承载量大于p[1][n]的。
(4)把q分为已经确定了的和暂时存放的两组,则此刻q[n]是确定的,接着从路口n出发,找到路口n能到的路口,进行更新暂时存在的q,更新完再从暂时存放的q中找到最大的q变成确定的,道理如(4)所说。
(5)重复(4)过程直到所有q都确定即可

ac代码

#include<iostream>       
#include<cstdlib>      
#include<cstdio> 
#include<cstring>      
#include<cmath>           
#include<string>      
#include<cstdlib>      
#include<iomanip>      
#include<vector>      
#include<list>      
#include<map>      
#include<queue>    
#include<algorithm>
using namespace std;
int q[1005], p[1005][1005];
int main()
{
	int x, y, z, t, n, m, k, kkk = 1, v[1005];
	cin >> t;
	while (t--)
	{
		memset(p, 0, sizeof(p));
		memset(v, 0, sizeof(v));
		cin >> n >> m;
		for (int i = 0; i < m; i++)
		{
			scanf("%d%d%d", &x, &y, &z);
			p[x][y] = p[y][x] = z;
		}int MAX; v[1] = 1; q[1] = 0;
		for (int i = 2; i <= n; i++)
			q[i] = p[1][i];
		for (int j = 1; j <=n; j++)
		{
			k = 0; MAX = 0;
			for (int i = 2; i <= n; i++)//找到q中最大的。
			{
				if (!v[i] && MAX < q[i])
				{
					MAX = q[i];
					k = i;
				}
			}if (k == 0)break;//已经没有未确定的q了,结束
			v[k] = 1;//k为当前q中最大的,把q【k】变成确定的。
			for (int i = 2; i <= n; i++)//如(4)所说,从路口K出发更新q。
				if (p[k][i] && !v[i])
					q[i] = max(q[i], min(p[k][i], q[k]));
		}
		cout << "Scenario #" << kkk << ":" << endl;
		cout << q[n] << endl<<endl;
		kkk++;
	}
}

猜你喜欢

转载自blog.csdn.net/weixin_43965698/article/details/87858422