POJ_1797 Heavy Transportation 【最大生成树的最小边】
一、题目
二、分析
题意就是让你找到从1到n的一条路,由于边的最大称重限制,你需要确定限制的最小值,也就是能运输的最大值。
可以结合最小生成树想,利用并查集,然后不断更新答案即可,需要注意的是题意是1到n走到就可以了,并不需要走到每个点,所以不是完整的最大生成树,所以当1与n共父节点时直接返回结果即可。
三、AC代码
1 #include <cstdio>
2 #include <iostream>
3 #include <algorithm>
4 #include <cstring>
5
6 using namespace std;
7 const int MAXN = 1e3 + 14;
8
9 struct edge
10 {
11 int from, to, cost;
12 bool operator < (const edge &e)
13 {
14 return cost > e.cost;
15 }
16 }E[MAXN*MAXN];
17 int m, n;
18 int par[MAXN];
19
20 int Find(int x)
21 {
22 return par[x] == x ? x : par[x] = Find(par[x]);
23 }
24
25 int Kruskal()
26 {
27 int Ans = 1e7;
28 for(int i = 1; i <= n; i++)
29 par[i] = i;
30 for(int i = 0; i < m; i++)
31 {
32 int a = E[i].from, b = E[i].to;
33 int fa = Find(a), fb = Find(b);
34 if(fa == fb)
35 {
36 continue;
37 }
38 else
39 {
40 Ans = min(Ans, E[i].cost);
41 par[fa] = fb;
42 }
43 if(Find(1) == Find(n))
44 return Ans;
45
46 }
47 return Ans;
48 }
49
50 int main()
51 {
52 //freopen("in.txt", "r", stdin);
53 int T, Case = 0;
54 scanf("%d", &T);
55 while(T--)
56 {
57 if(Case)
58 puts("");
59 scanf("%d%d", &n, &m);
60 for(int i = 0; i < m; i++)
61 scanf("%d%d%d", &E[i].from, &E[i].to, &E[i].cost);
62 sort(E, E + m);
63 printf("Scenario #%d:\n%d\n", ++Case, Kruskal());
64 }
65 return 0;
66 }
POJ_1797 Heavy Transportation 【最大生成树的最小边】的更多相关文章
- POJ 1797 Heavy Transportation (最大生成树)
题目链接:POJ 1797 Description Background Hugo Heavy is happy. After the breakdown of the Cargolifter pro ...
- [poj1797]Heavy Transportation<最大生成树prim&kruskal>
题目链接:http://poj.org/problem?id=1797 题意:给定n个点,m条边,每条边连接两点切有权值.求点1到点n的路径的上的最小边的值最大... 翻别人博客找到的题,方法挺多的, ...
- (POJ 1797) Heavy Transportation 最大生成树
题目链接:http://poj.org/problem?id=1797 Description Background Hugo Heavy is happy. After the breakdown ...
- POJ 1797 Heavy Transportation (dijkstra 最小边最大)
Heavy Transportation 题目链接: http://acm.hust.edu.cn/vjudge/contest/66569#problem/A Description Backgro ...
- poj 1797 Heavy Transportation(最大生成树)
poj 1797 Heavy Transportation Description Background Hugo Heavy is happy. After the breakdown of the ...
- POJ 1797 Heavy Transportation(最大生成树/最短路变形)
传送门 Heavy Transportation Time Limit: 3000MS Memory Limit: 30000K Total Submissions: 31882 Accept ...
- #图# #最大生成树# #kruskal# ----- OpenJudge 799:Heavy Transportation
OpenJudge 799:Heavy Transportation 总时间限制: 3000ms 内存限制: 65536kB 描述BackgroundHugo Heavy is happy. Afte ...
- Heavy Transportation(最短路 + dp)
Heavy Transportation Time Limit:3000MS Memory Limit:30000KB 64bit IO Format:%I64d & %I64 ...
- poj 1797 Heavy Transportation(最短路径Dijkdtra)
Heavy Transportation Time Limit: 3000MS Memory Limit: 30000K Total Submissions: 26968 Accepted: ...
随机推荐
- 左神算法第一节课:复杂度、排序(冒泡、选择、插入、归并)、小和问题和逆序对问题、对数器和递归(Master公式)
第一节课 复杂度 排序(冒泡.选择.插入.归并) 小和问题和逆序对问题 对数器 递归 1. 复杂度 认识时间复杂度常数时间的操作:一个操作如果和数据量没有关系,每次都是固定时间内完成的操作,叫做常数 ...
- 微软大楼设计方案(中等) 推公式+RMQ问题
近日,微软新大楼的设计方案正在广泛征集中,其中一种方案格外引人注目.在这个方案中,大楼由 nn 栋楼组成,这些楼从左至右连成一排,编号依次为 11 到 nn,其中第 ii 栋楼有 h_ihi层. ...
- ACM-ICPC国际大学生程序设计竞赛北京赛区(2015)网络赛
#1235 : New Teaching Buildings 时间限制:2000ms 单点时限:2000ms 内存限制:256MB 描述 Thanks to the generous finance ...
- CSS3 动态生成内容(在Web中插入内容)====CSS的伪类或者伪元素
# css3 .类:伪类::伪元素 /* CSS3伪元素/伪类 :https://www.w3.org/TR/css3-selectors/#selectors ::selection 伪元素(F12 ...
- CSS ? Layout Module : CSS 布局模型
1 1 1 https://www.w3.org/TR/css-grid-1/ CSS Grid Layout Module Level 1 W3C Working Draft, 19 May 201 ...
- swiper & swiper slider
swiper & swiper slider mobile swiper https://idangero.us/swiper/ https://idangero.us/swiper/get- ...
- macOS 录屏 gif
macOS 录屏 gif LICEcap bug 授权问题? 如何在 Mac 上录制屏幕 https://support.apple.com/zh-cn/HT208721 Command + Shif ...
- Nodejs 使用 TypeScript
安装依赖 λ yarn add typescript types/node concurrently nodemon wait-on -D 初始化一个 tsconfig.json λ ./node_m ...
- 如何通过NGK数字增益平台实现兑换算力
最近币圈里有一个新项目NGK非常火热,很多朋友在经过了了解以后纷纷表示很看好这个项目,那么除了二级市场之外,是否还能有其他的方式可以更低成本地获得NGK代币呢? 答案是肯定的,NGK数字增益平台便是低 ...
- NGK数字钱包的特点是什么?NGK钱包的优点和缺点是什么?
说起区块链数字资产,那就离不开谈到数字钱包.数字钱包不仅有资产管理的功能,还可以进行资产理财.资产交易,甚至能为公链DAPP导流. 对于NGK公链而言,其数字钱包已然成为了解NGK公链的基础条件.NG ...