poj 1797 Heavy Transportation(最大生成树)
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(最大生成树)的更多相关文章
- POJ 1797 Heavy Transportation (最大生成树)
题目链接:POJ 1797 Description Background Hugo Heavy is happy. After the breakdown of the Cargolifter pro ...
- POJ 1797 Heavy Transportation / SCU 1819 Heavy Transportation (图论,最短路径)
POJ 1797 Heavy Transportation / SCU 1819 Heavy Transportation (图论,最短路径) Description Background Hugo ...
- POJ.1797 Heavy Transportation (Dijkstra变形)
POJ.1797 Heavy Transportation (Dijkstra变形) 题意分析 给出n个点,m条边的城市网络,其中 x y d 代表由x到y(或由y到x)的公路所能承受的最大重量为d, ...
- POJ 1797 Heavy Transportation
题目链接:http://poj.org/problem?id=1797 Heavy Transportation Time Limit: 3000MS Memory Limit: 30000K T ...
- POJ 1797 Heavy Transportation SPFA变形
原题链接:http://poj.org/problem?id=1797 Heavy Transportation Time Limit: 3000MS Memory Limit: 30000K T ...
- POJ 1797 Heavy Transportation(最大生成树/最短路变形)
传送门 Heavy Transportation Time Limit: 3000MS Memory Limit: 30000K Total Submissions: 31882 Accept ...
- POJ 1797 Heavy Transportation (Dijkstra变形)
F - Heavy Transportation Time Limit:3000MS Memory Limit:30000KB 64bit IO Format:%I64d & ...
- POJ 1797 ——Heavy Transportation——————【最短路、Dijkstra、最短边最大化】
Heavy Transportation Time Limit:3000MS Memory Limit:30000KB 64bit IO Format:%I64d & %I64 ...
- POJ 1797 Heavy Transportation (Dijkstra)
题目链接:POJ 1797 Description Background Hugo Heavy is happy. After the breakdown of the Cargolifter pro ...
随机推荐
- seajs源码
/*** Sea.js 3.0.0 | seajs.org/LICENSE.md 中文注释由 李祥威 添加,为个人对细节的理解,官方解释很详细的地方就不说了 难免有错漏,联系我: chuangweil ...
- 利用Python循环(包括while&for)各种打印九九乘法表
一.for循环打印九九乘法表 #注意:由于缩进在浏览器不好控制,请大家见谅,后续会有图片传入. 1.1 左下角 for i in range(1,10): for j in range(1,i+1): ...
- 初识React-Redux之粗暴理解入门
权当暂记 日后再行补充完善,若有阅读者,请翻到下文黄色标题'从这里开始'起阅读. Rudex在我看来最本质做的事情就是将所有的State属性统一存储(一个属性就是一个注册到store的Reducer) ...
- python模块导入的方法与区别
import .. #导入整个模块 from .. import .. #导入模块中的类.函数或者变量 from .. import * #导入模块中的所有公开成员 from .. import ...
- 学Java的前景与就业,资深程序员教你怎么开始学Java!
IT行业一直是就业的热门岗位,程序员这个职业稳定性和收入比都有着不错的前景,那么学Java的前景和就业是什么样的呢?随着入行Java的准程序员越来越多,各种学习Java的流派也层出不穷!其实在编程的世 ...
- Windows命令行command的Shell命令详细解析和语法
CMD命令大全及详细解释和语法 Microsoft Windows XP [版本 5.1.2600] 有关某个命令的详细信息,请键入 HELP 命令名 ASSOC 显示或修改文件扩展名关联. A ...
- 如何基于 eolinker 的进行接口管理
由于工作的原因,经常要接触到很多API接口,而API接口在设计时往往需要编写大量的文档,而且编写完成后往往需要根据实际情况,经常改动文档,这使得文档编写维护工作量相对较大,这让我也包括很多的开发者都很 ...
- Oracle单行函数基础运用
单行函数 整个SQL的精髓:select语句+单行函数(背) 字符串函数 常用的处理字符串的函数有如下: No. 函数名 含义 1 UPPER(c1) upper 将字符串全部转为大写 2 LOWE ...
- 秒表计时器以及Stopwatch
Stopwatch:秒表计时器,用来记录程序的运行时间,通常用来测试代码在时间上的执行效率.(需要引用:System.Diagnostics.) Stopwatch sw=new Stopwatch( ...
- 关于HTML5新手应该知道的几点知识
随着移动互联网的快速发展,HTML5迅速崛起,我们的生活的方方面面都被HTML5渗透着.HTML5在PC端.移动端上均应用广泛,被称为Web的未来.而随着Google正式停止支持Swiffy,HTML ...