POJ1797 Heavy Transportation
解题思路:典型的Kruskal,不能用floyed(会超时),上代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define inf 0x3f3f3f3f
const int maxn = ;
int father[maxn]; struct node{
int x, y, w;
}p[maxn*maxn]; //第一次开maxn,RE了一次 int cmp(node A, node B)
{
return A.w > B.w; //权值从大到小排序
} int Find(int x)
{
return father[x] == x ? x : father[x] = Find(father[x]);
} void Union(int x, int y)
{
int rootx = Find(x);
int rooty = Find(y);
if(rootx != rooty) father[rootx] = rooty;
return ;
} int main()
{
int t, n, m, kase = ;
scanf("%d", &t);
while(t--)
{
scanf("%d %d", &n, &m);
for(int i = ; i <= n; i++) father[i] = i;
for(int i = ; i <= m; i++)
scanf("%d %d %d", &p[i].x, &p[i].y, &p[i].w);
sort(p+, p++m, cmp); //注意这里是p+1开始 int ans = inf; //初始化ans为最大值
for(int i = ; i <= m; i++)
{
int rootx = Find(p[i].x);
int rooty = Find(p[i].y);
if(rootx != rooty)
{
Union(rootx, rooty);
//ans保存路径中最小的权值
if(p[i].w < ans) ans = p[i].w;
//如果1和n已经连接,则直接跳出
if(Find() == Find(n)) break;
}
}
//注意输出格式
printf("Scenario #%d:\n%d\n\n", kase++, ans);
}
return ;
}
POJ1797 Heavy Transportation的更多相关文章
- [poj1797]Heavy Transportation<最大生成树prim&kruskal>
题目链接:http://poj.org/problem?id=1797 题意:给定n个点,m条边,每条边连接两点切有权值.求点1到点n的路径的上的最小边的值最大... 翻别人博客找到的题,方法挺多的, ...
- POJ--1797 Heavy Transportation (最短路)
题目电波: POJ--1797 Heavy Transportation n点m条边, 求1到n最短边最大的路径的最短边长度 改进dijikstra,dist[i]数组保存源点到i点的最短边最大的路径 ...
- POJ1797 Heavy Transportation 【Dijkstra】
Heavy Transportation Time Limit: 3000MS Memory Limit: 30000K Total Submissions: 21037 Accepted: ...
- (Dijkstra) POJ1797 Heavy Transportation
Heavy Transportation Time Limit: 3000MS Memory Limit: 30000K Total Submissions: 53170 Accepted: ...
- POJ1797 Heavy Transportation —— 最短路变形
题目链接:http://poj.org/problem?id=1797 Heavy Transportation Time Limit: 3000MS Memory Limit: 30000K T ...
- [POJ1797] Heavy Transportation(最大生成树 || 最短路变形)
传送门 1.最大生成树 可以求出最大生成树,其中权值最小的边即为答案. 2.最短路 只需改变spfa里面的松弛操作就可以求出答案. ——代码 #include <queue> #inclu ...
- POJ 1797 Heavy Transportation (最大生成树)
题目链接:POJ 1797 Description Background Hugo Heavy is happy. After the breakdown of the Cargolifter pro ...
- (POJ 1797) Heavy Transportation 最大生成树
题目链接:http://poj.org/problem?id=1797 Description Background Hugo Heavy is happy. After the breakdown ...
- POJ1797 Heavy Transportation (堆优化的Dijkstra变形)
Background Hugo Heavy is happy. After the breakdown of the Cargolifter project he can now expand bus ...
随机推荐
- HDOJ_1087_Super Jumping! Jumping! Jumping! 【DP】
HDOJ_1087_Super Jumping! Jumping! Jumping! [DP] Time Limit: 2000/1000 MS (Java/Others) Memory Limit: ...
- linux---(6/27)tr命令和sed命令详解
Tr命令: tr是简单的单个“字符”处理工具,而sed是功能非常强大的“字符串”处理工具. 用于查询,字符串2用于处理各种转换.tr刚执行时,字符串1中的字符被映射到字符串2中的字符,然后转换操作开始 ...
- launch 文件解析
roslaunch工具是ros中python实现的程序启动工具,通过读取launch文件中的参数配置.属性配置等来启动一系列节点: 很多ROS包或源码包中都有launch文件,一般为该程序包能够运行起 ...
- 双camera景深计算
https://sanwen8.cn/p/2e41VC5.html 本文系微信公众号<大话成像>,知乎专栏< all in camera>原创文章,转载请注明出处. 接着上一篇 ...
- 【Head First Servlets and JSP】笔记21:从有脚本到无脚本
可以建立多态的bean引用吗 使用type,但没有class scope属性默认为“page” 从有脚本到无脚本 1.快速搭建一个测试环境:输入用户名,返回“Hello, 用户名” index.htm ...
- Stalstack 安装
Stalstack 介绍 saltsack与shell自动化的区别 shell 自动化脚本 --> 串行 saltstack --> 并行 saltsack 平台管理插件 saltstac ...
- 无缝走A的终极技巧:学会了你也是走A怪
ADC重点之中:改键铸造更强走A! 我们先来欣赏一波来自世界顶尖ADC大师兄Doublelift的教科书般的走A. 他使用的金克丝在空蓝的情况下,凭借娴熟的走A技巧,拿下五杀. 关于走A,其实它有着一 ...
- jz2440存储管理实验【学习笔记】
平台:jz2440 作者:庄泽彬(欢迎转载,请注明作者) 说明:韦东山一期视频学习笔记 简介:先来简单的说明一下这次的实验,看看下图,我们的程序通过烧录器下载到nandflash当中去,之后在启动的时 ...
- mysql官网下载链接——绿色版&安装版
windows64位5.5.60安装版 https://downloads.mysql.com/archives/get/file/mysql-5.5.60-winx64.msi windows64位 ...
- XML基本知识点——思维导图
如图 思维导图图片链接 http://www.edrawsoft.cn/viewer/public/s/5dcd3224563939 有道云笔记图片链接 http://note.youdao.com/ ...