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 ...
随机推荐
- 『NiFi 学习之路』入门 —— 下载、安装与简单使用
一.概述 "光说不练假把式." 官网上的介绍多少让人迷迷糊糊的,各种高大上的词语仿佛让 NiFi 离我们越来越远. 实践是最好的老师.那就让我们试用一下 NiFi 吧! 二.安装 ...
- HandlerSocket ---MySQL与NoSQL ---SQL与NoSQL的融合(转)
项目地址:https://github.com/ahiguti/HandlerSocket-Plugin-for-MySQL 写这一篇内容的原因是MySQL5.6.2突然推出了memcached的功能 ...
- hadoop20---代理另一种方式
package cn.itcast_05_proxy.service; /** * 这是一个业务的接口,这个接口中的业务就是返回衣服的价格 */ public interface IBoss {//接 ...
- sq 如何创建全局的临时表。
全局临时表的生命周期一直持续到创建会话(不是创建级别)才终止.例如,如果你在存储过程中创建全局临时表,当超出存储过程的范围时,该表并不会被销毁.当创建会话终止后,SQL Server才会自动尝试删除该 ...
- 在非小细胞肺癌中,MET基因的14号外显子突变和年龄,依赖于癌症阶段的CNV,C-MET过表达的关系
背景:c-MET是肝细胞生长因子的酪氨酸激酶受体.MET 14号外显子编码部分c-MET的胞内跨膜结构域,包括重要的调节原件,比如酪氨酸1003,一个降解c-MET的相关酶的结合位点. 拥有MET 1 ...
- NOIP 转圈游戏
描述 n 个小伙伴(编号从 0 到 n-1)围坐一圈玩游戏.按照顺时针方向给 n 个位置编号,从0 到 n-1.最初,第 0 号小伙伴在第 0 号位置,第 1 号小伙伴在第 1 号位置,……,依此类推 ...
- springboot--配置文件加载顺序
-file:./config(内部配置) -file:./ (内部配置) -classpath:/config (外部配置) -classpath:/ (外部配置) 运维: spring -jar s ...
- JavaWeb -- Struts2,对比, 简单表单提交,校验,防重复提交, 文件上传
Struts2核心流程图 1. Struts2 和 Struts1 对比 struts1:基于Servlet(ActionServlet),actionForm众多(类的爆炸),action单例(数据 ...
- git常用操作 配置用户信息、拉取项目、提交代码、分支操作、版本回退...
git常用操作 配置用户信息.拉取项目.提交代码.分支操作.版本回退... /********git 配置用户信息************/ git config --global user.name ...
- VMware 虚拟镜像转 Hyper-V(Win10/2016)
VMware 虚拟镜像转 Hyper-V(Win10/2016) 参考:http://www.askme4tech.com/how-convert-vmware-virtual-machine-hyp ...