poj 1797(并查集)
http://poj.org/problem?id=1797
题意:就是从第一个城市运货到第n个城市,最多可以一次运多少货。
输入的意思分别为从哪个城市到哪个城市,以及这条路最多可以运多少货物。
思路:我觉得可以用floyd来做这道题,结果交上去就TLE了,不过时间复杂度为n3TLE看起来也是比较正常,毕竟数字大。
然后我就看到网上有人用并查集来做,不然以前我都没往这方面想过,然后就用并查集来做
用并查集的思路就是,首先,对每组数据按照重量由大到小进行排序。然后查找合并。当Find(n) == Find( 1 )时,那个数据的重量也就是答案。
因为每组数据都是由大到小进行排序了,当前的重量肯定是最小的,而加入这一组数据后,上面的式子就成立了,也就说明这是第一次连通。
#include <stdio.h>
#include <string.h>
#include <stdlib.h> struct cm{
int x,y,weigh;
}s[]; int cmp(const void *a,const void *b)
{
return (*(cm *)b).weigh-(*(cm *)a).weigh;
} int belg[],m,n; int Find(int x)
{
int _x=x,_b;
while(belg[_x]!=_x)
_x=belg[_x];
while(x!=belg[x])
{
_b=belg[x];
belg[x]=_x;
x=_b;
}
return _x;
} int unio(int x)
{
belg[ Find( s[x].y ) ] = Find( s[x].x );
if( Find( ) == Find ( n )) return ;
return ;
} int main()
{
// freopen("in.txt","r",stdin);
int t,a=,ans;
scanf("%d",&t);
while( t-- )
{
scanf("%d%d",&n,&m);
a++;
for( int i = ; i <= n ; i++ )
belg[i]=i;
for( int i = ; i < m ; i++ )
scanf("%d%d%d",&s[ i ].x,&s[ i ].y,&s[ i ].weigh);
qsort(s,m,sizeof(s[]),cmp);
for( int i = ; i < m ; i++ )
if(unio(i))
{
ans = s[ i ].weigh;
break;
}
printf("Scenario #%d:\n%d\n\n",a,ans);
}
return ;
}
下面的是TLE了的Floyd(仅供参考,不敢确定正确性)
#include <stdio.h>
#include <string.h> int n,m,graph[ ][ ]; int main()
{
// freopen("in.txt","r",stdin);
int t,a = ;
scanf("%d",&t);
while( t-- )
{
int b,c,d;
a++;
scanf("%d%d",&n,&m);
memset( graph , , sizeof( graph ) );
for (int i = ; i <= m ; i++ )
{
scanf("%d%d%d",&b,&c,&d);
graph[ b ][ c ] = d;
// graph[ c ][ b ] = d;
}
for( int k = ; k <= n ; k++ )
for( int i = ; i < n ; i++ )
for(int j = i+ ; j <= n ; j++ )
if( graph [ i ][ j ] > graph [ i ][ k ] && graph[ i ][ j ] > graph [ k ][ j ] )
if( graph[ i ][ k ] > graph [ k ][ j ] )
graph[ i ][ j ] = graph [ k ][ j ];
else
graph[ i ][ j ] = graph [ i ][ k ];
printf("Scenario #%d:\n%d\n\n",a,graph[ ][ n ]);
}
return ;
}
poj 1797(并查集)的更多相关文章
- poj 1984 并查集
题目意思是一个图中,只有上下左右四个方向的边.给出这样的一些边, 求任意指定的2个节点之间的距离. 就是看不懂,怎么破 /* POJ 1984 并查集 */ #include <stdio.h& ...
- POJ 2492 并查集扩展(判断同性恋问题)
G - A Bug's Life Time Limit:10000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u S ...
- POJ 2492 并查集应用的扩展
A Bug's Life Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 28651 Accepted: 9331 Descri ...
- POJ 3228 [并查集]
题目链接:[http://poj.org/problem?id=3228] 题意:给出n个村庄,每个村庄有金矿和仓库,然后给出m条边连接着这个村子.问题是把所有的金矿都移动到仓库里所要经过的路径的最大 ...
- poj 1733 并查集+hashmap
题意:题目:有一个长度 已知的01串,给出多个条件,[l,r]这个区间中1的个数是奇数还是偶数,问前几个是正确的,没有矛盾 链接:点我 解题思路:hash离散化+并查集 首先我们不考虑离散化:s[x] ...
- poj 3310(并查集判环,图的连通性,树上最长直径路径标记)
题目链接:http://poj.org/problem?id=3310 思路:首先是判断图的连通性,以及是否有环存在,这里我们可以用并查集判断,然后就是找2次dfs找树上最长直径了,并且对树上最长直径 ...
- POJ 3657 并查集
题意: 思路: 1.二分+线段树(但是会TLE 本地测没有任何问题,但是POJ上就是会挂--) 2.二分+并查集 我搞了一下午+一晚上才搞出来----..(多半时间是在查错) 首先 如果我们想知道这头 ...
- poj 2236 并查集
并查集水题 #include<cstdio> #include<iostream> #include<algorithm> #include<cstring& ...
- poj 1417(并查集+简单dp)
True Liars Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 2087 Accepted: 640 Descrip ...
随机推荐
- GoLang之网络
GoLang之网络 Go语言标准库里提供的net包,支持基于IP层.TCP/UDP层及更高层面(如HTTP.FTP.SMTP)的网络操作,其中用于IP层的称为Raw Socket. net包的Dial ...
- JavaScript 面向对象程序设计(下)——继承与多态 【转】
JavaScript 面向对象程序设计(下)--继承与多态 前面我们讨论了如何在 JavaScript 语言中实现对私有实例成员.公有实例成员.私有静态成员.公有静态成员和静态类的封装.这次我们来讨论 ...
- json_decode
<?php $json = '{"a":1,"b":2,"c":3,"d":4,"e":5}' ...
- WinSCP 连接 Ubuntu 拒绝的问题
1.打开配置文件 $ sudo vi /etc/ssh/sshd_config 2.修改操作 PermitRootLogin without-password 修改为 PermitRootLogin ...
- pch
#define kWeakSelf(weakSelf) __weak __typeof(self)weakSelf = self; #ifndef __OPTIMIZE__#define NSLog( ...
- git项目开发版本控制实践
linux和bsd: 第一, bsd, berkeley software distribution, 伯克利软件套装, 是最开始的unix是开放的, 然后berkeley对unix进行了修改, 形成 ...
- hdu4951 Multiplication table (乘法表的奥秘)
http://acm.hdu.edu.cn/showproblem.php?pid=4951 2014多校 第八题 1008 2014 Multi-University Training Contes ...
- 字符编码详解及由来(UNICODE,UTF-8,GBK)
一直对字符的各种编码方式懵懵懂懂,什么ANSI.UNICODE.UTF-8.GB2312.GBK.DBCS.UCS--是不是看的很晕,假如您细细的阅读本文你一定可以清晰的理解他们.Let's ...
- 包介绍 - UriTemplates (用于处理格式化Uri模板)
UriTemplates 用于处理格式化Uri模板 PM> Install-Package Tavis.UriTemplates 设置Uri Path Segment [Fact] public ...
- mysql 总结一
mysql 总结一 数据类型(四类): 整型(5种:tinyint,smallint,mediumint, int(或integer),bigint ): 浮点型:(float,double), 日期 ...