POJ - 1797 Heavy Transportation【dijkstra】

Heavy Transportation

Time Limit: 3000MS Memory Limit: 30000K
Total Submissions: 52657 Accepted: 13458
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
Source

TUD Programming Contest 2004, Darmstadt, Germany

链接:http://poj.org/problem?id=1797

简述:一共有n条路线,每条路线有对应的承重量,求从1到n所有路线中的最大承重量。

分析:将vis数组置零,输入数据,建立无向图,接着是dijkstra算法,这里求从1到n所有路线中的最大值,更新部分变一下形。

说明:先是用prim写了一次,显示超时。再用dijkstra写了一次,还是显示超时。搞了很久,后来发现是printf跟cout的一个输入输出的问题,接着过了。后来回去改prim的代码,发现直接WAemmmm……【prim跟dijkstra的区别:https://zhidao.baidu.com/question/557814268866629492.html】
(在初始化数组这里栽了好多个跟头,三个数组都要初始化)

AC代码如下:

#include <iostream>
#include <string.h>
#include <algorithm>
#include <cmath>
#include <stdio.h>
using namespace std;
int map[1010][1010], vis[1010], dis[1010];
int n;
void dijkstra()
{
	int i, j;
	for (i = 1; i <= n; i++) dis[i] = map[1][i];			
	vis[1] = 1;
	int k;	//	k的位置放错了
	for (i = 1; i <= n; i++) {
		int minn = -1;
		for (j = 1; j <= n; j++) {
			if (!vis[j] && minn < dis[j]) {
				k = j;
				minn = dis[j];
			}
		}
		vis[k] = 1;
		for (j = 1; j <= n; j++)
			if (!vis[j] && dis[j] < min(map[k][j], minn))
				dis[j] = min(map[k][j], minn);
	}
}
int main()
{
	
	int t, o=0;
	scanf("%d", &t);
	while (t--) {
		int m, a, b, c, i;
		scanf("%d %d", &n, &m);
		memset(dis, 0, sizeof(dis));
		memset(vis, 0, sizeof(vis));
		memset(map, 0, sizeof(map));
		for(i = 1;i <= m;i++) {
			scanf("%d %d %d", &a, &b, &c);
			map[a][b] = map[b][a] = c;	//建立无向图
		}
		dijkstra();
		printf("Scenario #%d:\n%d\n\n", ++o, dis[n]);
	}
}

猜你喜欢

转载自blog.csdn.net/qq_43966202/article/details/86739283