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 ...
随机推荐
- 404. Sum of Left Leaves
Find the sum of all left leaves in a given binary tree. 左树的值(9+15=24) /** * Definition for a binary ...
- MYTOP安装和使用
安装 mytop 1. 在 /etc/yum.repo.d 新建一个文件 21andy.com.repo [21Andy.com] name=21Andy.com Packages for Enter ...
- 二、oracle pctfree和pctused详解
一.建立表时候,注意PCTFREE参数的作用 PCTFREE:为一个块保留的空间百分比,表示数据块在什么情况下可以被insert,默认是10,表示当数据块的可用空间低于10%后,就不可以被insert ...
- [asp.net core]project.json(1)
摘要 前面介绍了使用vs2015新建asp.net core web的内容,这篇文章学习下project.json文件的内容. project.json 原文:https://docs.microso ...
- 将Ubuntu 15.10升级到Ubuntu 16.04
Ubuntu 16.04 LTS 代号为 Xenial Xerus,其最终版将于 2016 年 4 月 21 日正式发布,Ubuntu16.04 将是非常受欢迎的开源操作系统 Ubuntu 的第 6 ...
- php 关于时区 date gmdate date_default_timezone_set/get 终极答疑
关于GMT和UTC时间? GMT+0800 Greenwich: [gri:nitf], 格林威治/格林尼治.是英国伦敦泰晤士河附近的一个小镇 Mean: adj. 自私的; n. 平均.. he i ...
- Jquery控制滚动显示欢迎字幕v2
Jquery控制滚动显示欢迎字幕v2: 之前做的那个比较适合测试环境,但要套入到网站中,有两个按钮在那摆着,还是不太好看.后面对代码进行了修改,如下: 参考代码: <html> <h ...
- HDU 1048
#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <string.h> int main() { char ...
- button 按钮
<!DOCTYPE html> <html> <body> <h1>我的第一段 JavaScript</h1> <p> Java ...
- 关于学习session的一二
session作为一种服务器端技术,在网页中与cookie相配合可谓相得益彰,今天学习了进一步session的相关知识,在这里记录一二,以便于以后查阅 /*问题:当我们浏览网页的时候,张三和李四可以购 ...