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 ...
随机推荐
- vue-scroller的使用
一 安装 使用npm 安装 npm install vue-scroller -S 二 引入 https://www.jianshu.com/p/a39f5276ff0b https://www.np ...
- [C++] 2017联发科技杯编程挑战赛 复赛题 “杰克船长的烦恼”
题目如下. 规则 杰克船长这次运气不错,抢到了一大堆金币.但他马上又开始发愁了, 因为如何给大家分金币,一直都是件不容易的事,每次杰克船长都要头疼好几天. 关于分金币,海盗的行规是这样的: 每次行动, ...
- 转换数据库连接池为hikaricp
hikaricp号称是最快的,今天转换过来试试 1.修改配置文件 <!-- Hikari Datasource --> <bean id="dataSource&quo ...
- 安装配置资产管理软件GLPI
GLPI是法语Gestionnaire libre de parc informatique的缩写,是一款历史悠久的资产管理软件: GLPI提供功能全面的IT资源管理接口,可以用来建立数据库全面管理I ...
- Linux系统下wget命令的使用教程
一.Linux wget简介 wget是linux上的命令行的下载工具.这是一个GPL许可证下的自由软件.Linux wget支持HTTP和FTP协议,支持代理服务器和断点续传功能,能够自动递归远程主 ...
- springboot学习(三)——使用HttpMessageConverter进行http序列化和反序列化
以下内容,如有问题,烦请指出,谢谢! 对象的序列化/反序列化大家应该都比较熟悉:序列化就是将object转化为可以传输的二进制,反序列化就是将二进制转化为程序内部的对象.序列化/反序列化主要体现在程序 ...
- VC++6.0调试简单快捷键
编译——F7 重新编译——Ctrl+F7 设置断点 ——F9 取消断点——F9 删除所有断点——Ctrl+Shift+F9 开始调试——F5 进行下一次调试——F5 停止调试——Shift+F5 逐过 ...
- 并发-CountDownLatch、CyclicBarrier和Semaphore
CountDownLatch.CyclicBarrier和Semaphore 参考: http://www.cnblogs.com/dolphin0520/p/3920397.html https:/ ...
- 基于XML配置的AOP实现日志打印
Spring中可以使用注解或XML文件配置的方式实现AOP.1.导入jar包 com.springsource.net.sf.cglib -2.2.0.jar com.springsource.org ...
- DataStage 服务启动
说明:①如果发现was.datastage已经自启动,但db2没有自启动,应先将它们停止,在按顺序启动;②如果都自启动了,就不用再启动,关闭防火墙即可. --0.关闭防火墙service iptabl ...