poj 1797 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

题目大意:给出一张图,求从1到n的路径上权值的最大的最小值。

解题思路:最大生成树。用Prim算法,改动一些地方即可了。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdlib>
using namespace std; typedef long long ll;
const int N = 1005;
const int INF = 0x3f3f3f3f;
int n, m;
int D[N][N], dis[N], vis[N], ans; void init() {
ans = INF;
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= n; j++) {
D[i][j] = 0;
}
}
for (int i = 0; i <= n; i++) dis[i] = 0;
for (int i = 0; i <= n; i++) vis[i] = 0;
} void input() {
scanf("%d%d", &n, &m);
int a, b, len;
for (int i = 0; i < m; i++) {
scanf("%d %d %d", &a, &b, &len);
D[a][b] = D[b][a] = len;
}
} void Prim() {
int k;
for (int i = 1; i <= n; i++) {
dis[i] = D[1][i];
}
vis[1] = 1;
for (int i = 0; i < n - 1; i++) {
int L = -2;
for (int j = 1; j <= n; j++) {
if (!vis[j] && dis[j] > L) {
L = dis[j];
k = j;
}
}
if (ans > L) ans = L;
if (k == n) break;
vis[k] = 1;
for (int j = 1; j <= n; j++) {
if (!vis[j] && D[j][k] > dis[j]) {
dis[j] = D[j][k];
}
}
}
printf("%d\n\n", ans);
} int main() {
int T, Case = 1;
scanf("%d", &T);
while (T--) {
printf("Scenario #%d:\n", Case++);
init();
input();
Prim();
}
return 0;
}

poj 1797 Heavy Transportation(最大生成树)的更多相关文章

  1. POJ 1797 Heavy Transportation (最大生成树)

    题目链接:POJ 1797 Description Background Hugo Heavy is happy. After the breakdown of the Cargolifter pro ...

  2. POJ 1797 Heavy Transportation / SCU 1819 Heavy Transportation (图论,最短路径)

    POJ 1797 Heavy Transportation / SCU 1819 Heavy Transportation (图论,最短路径) Description Background Hugo ...

  3. POJ.1797 Heavy Transportation (Dijkstra变形)

    POJ.1797 Heavy Transportation (Dijkstra变形) 题意分析 给出n个点,m条边的城市网络,其中 x y d 代表由x到y(或由y到x)的公路所能承受的最大重量为d, ...

  4. POJ 1797 Heavy Transportation

    题目链接:http://poj.org/problem?id=1797 Heavy Transportation Time Limit: 3000MS   Memory Limit: 30000K T ...

  5. POJ 1797 Heavy Transportation SPFA变形

    原题链接:http://poj.org/problem?id=1797 Heavy Transportation Time Limit: 3000MS   Memory Limit: 30000K T ...

  6. POJ 1797 Heavy Transportation(最大生成树/最短路变形)

    传送门 Heavy Transportation Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 31882   Accept ...

  7. POJ 1797 Heavy Transportation (Dijkstra变形)

    F - Heavy Transportation Time Limit:3000MS     Memory Limit:30000KB     64bit IO Format:%I64d & ...

  8. POJ 1797 ——Heavy Transportation——————【最短路、Dijkstra、最短边最大化】

    Heavy Transportation Time Limit:3000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64 ...

  9. POJ 1797 Heavy Transportation (Dijkstra)

    题目链接:POJ 1797 Description Background Hugo Heavy is happy. After the breakdown of the Cargolifter pro ...

随机推荐

  1. 五种js判断是否为整数(转)

    五种js判断是否为整数类型方式 作者:snandy 这篇文章主要介绍了五种JavaScript判断是否为整数类型方式,需要的朋友可以参考下   这篇看看如何判断为整数类型(Integer),JavaS ...

  2. java注解(Annotation)

    本文转载自http://www.cnblogs.com/xdp-gacl/p/3622275.html 一.认识注解 注解(Annotation)很重要,未来的开发模式都是基于注解的,JPA是基于注解 ...

  3. JavaScript系列-----Object之toString()和valueOf()方法 (2)

    深入理解toString()和valueOf()函数 1.我们为什么要了解这两种方法 众所周知,toString()函数和valueOf函数,这两个函数是Object类的对象生来就拥有的,而且他们还可 ...

  4. seajs笔记

    Amd和Cmd的区别有哪些? 1. 对于依赖的模块,AMD 是提前执行,CMD 是延迟执行.不过 RequireJS 从 2.0 开始,也改成可以延迟执行(根据写法不同,处理方式不同).CMD 推崇 ...

  5. C#的Random到底该怎么使用

    先看代码: 在循环中,有的只NEW一个Random,有的每次都NEW 一个Random. Console.WriteLine("1.多个Random,默认随机种子,"); ; i ...

  6. Maven2的配置文件settings.xml

    简介: 概览 当Maven运行过程中的各种配置,例如pom.xml,不想绑定到一个固定的project或者要分配给用户时,我们使用settings.xml中的settings元素来确定这些配置.这包含 ...

  7. js基础01

    常见的五大浏览器:chrome firfox ie opera safari 浏览器的解析器会把代码解析成用户所能看到的东西 www.2cto.com/kf/201202/118111.html浏览器 ...

  8. python学习总结笔记(一)

    1.raw_input("请输入:")提示录入信息,读取录入的字符串返回你录入的字符串2.os.environ 获取所有系统的环境变量,返回一个字典.3.str与repr区别str ...

  9. javascript第十章--Ajax与Comet

    ① XMLHttpRequest对象 ② XMLHttpRequest2级 ③ 进度事件 ④ 跨域源资源共享 ⑤ 其他跨域技术

  10. linux下大于2T硬盘格式化方法

    现在的硬盘很多都大于2T,但是linux自带的fdisk 工具无法格式化大于2T的磁盘,需要使用第三方工具parted,我们来看如何使用parted格式硬盘 1,可以先使用fdisk -l查看系统当前 ...