【dijkstra】poj1797

Heavy Transportation

Description

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到n城市最大承载量,而最大承载量就是从城市1到城市n所有通路上的最大承载量

表示这是高中做过的一道题,但是在想出办法之前我都没有意识到,确实是感觉太久远了……

dijkstra稍微改动一下更新临时最短路的条件就可以了——如果 d[ j ] < min(a[ k ][ j ],  d[ k ]) 就更优一些可以更新


#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
long a[1010][1010];
long d[1010];
bool b[1010];
long t, n, m;
void init()
{
	memset(a, 0, sizeof(a));
	cin >> n >> m;
	for(int i = 0;i != m; i++)
	{
		int x,y,z;
		cin >> x >> y >> z;
		a[x][y] = z;
		a[y][x] = z;
	}
}
void deal()
{
	memset(d, 0, sizeof(d));
	memset(b, 0, sizeof(b));
	b[1] = true;
    for(int i = 1; i <= n; i++)
	{
		d[i] = a[1][i];
	}

	for (int i = 1; i <= n - 1; i++)
	{
		int maxn = 0;
		int k = 0;
		for (int j = 2; j <= n; j++)
		{
			if ((b[j] == 0) && (d[j] > maxn))
			{
				maxn = d[j];
				k = j;
				//cout<<k<<"  :";
			}
		}

		b[k] = true;

		for (int j = 1; j <= n; j++)
		{
			if((d[k] != 0) && (a[k][j] != 0) && (~b[j]) && d[j] < min(a[k][j], d[k]))
				d[j] = min(a[k][j],d[k]);
			//cout<<d[j]<<endl;
		}
	}
	cout << d[n] << endl;
}
int main()
{
	//freopen("poj1797.in","r",stdin);
	//freopen("poj1797.out","w",stdout);
    cin >> t;
    for(int i = 1; i <= t;i++)
	{
		init();
		cout << "Scenario #" << i << ":" << endl;
        deal();
        cout << endl;
	}
	return 0;
}


全部评论

相关推荐

最近和朋友聊天,她说了句让我震惊的话:"我发现我连周末点外卖都开始'最优解'了,一定要赶在高峰期前下单,不然就觉得自己亏了。"这不就是典型的"班味入侵"吗?工作思维已经渗透到生活的方方面面。
小型域名服务器:啊?我一直都这样啊?我还以为是我爱贪小便宜呢?每次去实验室都得接一杯免费的开水回去,出门都得规划一下最短路径,在宿舍就吃南边的食堂,在实验室就吃北边的食堂,快递只有顺路的时候才取。
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务