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 ...
随机推荐
- svn / git SourceTree
开发使用SourceTree 忽略文件这块老弄错,这次专门博客一下,使用CocoaPods 开发项目, 忽略步骤如下: 忽略文件内容如下 *.xcworkspace xcuserdata *.loc ...
- Mysql5.7 用户与授权
mysql -uroot -proot MySQL5.7 mysql.user表没有password字段改 authentication_string: 一. 创建用户: 命令:CREATE USER ...
- Android 电池关机充电
android 电池(一):锂电池基本原理篇 android 电池(二):android关机充电流程.充电画面显示 android 电池(三):android电池系统 android电池(四):电池 ...
- 20162326 齐力锋 2017-2018学期 Bag类的补写博客
要求: 代码运行在命令行中,路径要体现学号信息,IDEA中,伪代码要体现个人学号信息 参见Bag的UML图,用Java继承BagInterface实现泛型类Bag,并对方法进行单元测试(JUnit), ...
- spring集成spring mvc 和hibernate详解
1.配置IOC容器 <!-- 配置IOC容器 --> <context-param> <param-name>contextConfigLocation</p ...
- 制作基于Buildbot的自动化测试系统Docker镜像
Buildbot in Docker 前言 最近使用Buildbot做了一个自动测试的框架,为了部署方便,可以把测试框架做成Docker镜像,方便部署.这里记录下过程,供大家参考. 项目介绍 项目是一 ...
- Java Swing窗体小工具实例 - 原创
Java Swing窗体小工具实例 1.本地webserice发布,代码如下: 1.1 JdkWebService.java package server; import java.net.InetA ...
- 2017 湘潭邀请赛&JSCPC G&J
训练的时候对G想了一个假算法..也有很大可能是写错了.. 下来一看别人的G 看起来很奇妙.. 开始把所有的左括号翻成右括号,然后cost*=-1 这样在优先队列中就是最优的 然后for每一段 如果前缀 ...
- javascript页面打印
打印本身比较简单,但要考虑到具体的需求.比如 1. 多浏览器: if (isIE()) { //打印预览 WebBrowser1.execWB(7, 1); } else { window.print ...
- php将科学计算法得出的结果转换成原始数据
由于php最大只支持显示 15位因的数据运算,大于15位的2数加减乘除的数据的结果,会直接用科学计数法显示, 但在现实生活中,科学计数法不利于普通人识别,所以,本函数将:科学计数法的出的结果转换成原始 ...