hdoj--2122--Ice_cream’s world III(克鲁斯卡尔)
Ice_cream’s world III
city to the capital. The project’s cost should be as less as better.
2 1
0 1 10 4 0
10 impossible
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<queue>
#include<algorithm>
using namespace std;
int pre[10010];
struct node
{
int u,v,val;
}p[10010];
int m,n;
void init()
{
for(int i=0;i<10010;i++)
pre[i]=i;
}
int cmp(node s1,node s2)
{
return s1.val<s2.val;
}
int find(int x)
{
int r=x;
while(r!=pre[r])
r=pre[r];
while(x!=r)
{
int t=pre[x];
pre[x]=r;
x=t;
}
return r;
}
int join(int x,int y)
{
int fx=find(x);
int fy=find(y);
if(fx!=fy)
{
pre[fx]=fy;
return 1;
}
return 0;
}
int main()
{
while(cin>>n>>m)
{
init();
for(int i=0;i<m;i++)
cin>>p[i].u>>p[i].v>>p[i].val;
sort(p,p+m,cmp);
int sum=0;
for(int i=0;i<m;i++)
{
if(join(p[i].u,p[i].v))
sum+=p[i].val;
}
int flog=0;
int gen=0;
for(int i=0;i<n;i++)
{
if(pre[i]==i)
gen++;
if(gen>1)
flog=1;
}
if(flog)
printf("impossible\n\n");
else
printf("%d\n\n",sum); }
return 0;
}
hdoj--2122--Ice_cream’s world III(克鲁斯卡尔)的更多相关文章
- hdoj 2122 Ice_cream’s world III【最小生成树】
Ice_cream's world III Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- hdoj 2122 Ice_cream’s world III
并查集+最小生成树 Ice_cream’s world III Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 ...
- HDU 2122 Ice_cream’s world III【最小生成树】
解题思路:基础的最小生成树反思:不明白为什么i从1开始取,就一直WA,难道是因为村庄的编号是从0开始的吗 Ice_cream’s world III Time Limit: 3000/1000 MS ...
- 图的生成树(森林)(克鲁斯卡尔Kruskal算法和普里姆Prim算法)、以及并查集的使用
图的连通性问题:无向图的连通分量和生成树,所有顶点均由边连接在一起,但不存在回路的图. 设图 G=(V, E) 是个连通图,当从图任一顶点出发遍历图G 时,将边集 E(G) 分成两个集合 T(G) 和 ...
- 贪心算法(Greedy Algorithm)之最小生成树 克鲁斯卡尔算法(Kruskal's algorithm)
克鲁斯卡尔算法(Kruskal's algorithm)是两个经典的最小生成树算法的较为简单理解的一个.这里面充分体现了贪心算法的精髓.大致的流程能够用一个图来表示.这里的图的选择借用了Wikiped ...
- Eddy's picture(prime+克鲁斯卡尔)
Eddy's picture Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Tota ...
- Ice_cream’s world III(prime)
Ice_cream’s world III Time Limit : 3000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Othe ...
- 【类克鲁斯卡尔做法+枚举最小边】【HDU1598】【find the most comfortable road】
题意: 给你一个图,有边权,K个询问:u到v 的路径中 边权最大值-边权最小值的最小值是多少 http://acm.hdu.edu.cn/showproblem.php?pid=1598 题解( ...
- 克鲁斯卡尔(Kruskal)算法
# include <stdio.h> # define MAX_VERTEXES //最大顶点数 # define MAXEDGE //边集数组最大值 # define INFINITY ...
随机推荐
- 深入ES6 模块系统
深入ES6 模块系统 本文转载自:众成翻译 译者:neck 链接:http://www.zcfy.cc/article/4436 原文:https://ponyfoo.com/articles/es6 ...
- 生成式模型:LDA
原文链接:http://blog.sina.com.cn/s/blog_5033f3b40101flbj.html 文章图文并茂,我就不转载了!!! LSI-Laten ...
- strusts2_json
引用别人的 Struts.xml <package name="default" extends ="json-default" > <act ...
- 团体程序设计天梯赛-练习集-L1-025. 正整数A+B
L1-025. 正整数A+B 本题的目标很简单,就是求两个正整数A和B的和,其中A和B都在区间[1,1000].稍微有点麻烦的是,输入并不保证是两个正整数. 输入格式: 输入在一行给出A和B,其间以空 ...
- 【转】Oracle基础结构认知—oracle物理结构 礼记八目 2017-12-13 20:31:06
原文地址:https://www.toutiao.com/i6499008214980362765/ oracle数据库启动:oracle服务启动,通过参数文件查找控制文件,启动控制文件,则控制文件调 ...
- qqbot 出现请求接口失败的问题
解决方法: 找到python安装目录下“Lib\site-packages\qqbot\qcontactdb\fetch.py”文件下的“http://s.web2.qq.com” 替换成 “http ...
- Linux安装pycharm
1.pycharm包地址:链接:https://pan.baidu.com/s/1uZJDvP0DW-l7FE2J4tB-Pw 密码:5ebi rz上传到服务器 tar -zxvf pycharm-c ...
- Problem 13
Problem 13 # Problem_13 """ Work out the first ten digits of the sum of the following ...
- Ubuntu14.04 Anaconda
我虚拟机Ubuntu14.04上的Python已经存在了两个版本,一个是python 2.7,一个是Python 3.4.想在它上面安装Anaconda,但又有所顾虑.我先想到的是,先卸载Ubuntu ...
- 【Codeforces Round #502 (in memory of Leopoldo Taravilse, Div. 1 + Div. 2) D】The Wu
[链接] 我是链接,点我呀:) [题意] 给你n个字符串放在multiset中. 这些字符串都是长度为m的01串. 然后给你q个询问 s,k 问你set中存在多少个字符串t 使得∑(t[i]==s[i ...