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 ...
随机推荐
- BZOJ 1951 Lucas定理+CRT
思路: 枚举约数 套个裸的Lucas+CRT就完了... //By SiriusRen #include <cmath> #include <cstdio> using nam ...
- 错误信息:getOutputStream() has already been called for this response
原因(转): getOutputStream()和getWriter()这两个方法不能在一个请求内同时使用, 如果使用forward,这时将要跳转到的页面是要用getWriter()方法获得输出流把页 ...
- redis启动加载过程、数据持久化
背景 公司一年的部分业务数据放在redis服务器上,但数据量比较大,单纯的string类型数据一年就将近32G,而且是经过压缩后的. 所以我在想能否通过获取string数据的时间改为保存list数据类 ...
- Struts2框架学习(二)——访问流程及架构
1.Struts2的执行流程 从客户端发送请求过来,先经过前端控制器(核心过滤器StrutsPrepareAndExecuteFilter)过滤器中执行一组拦截器(一组拦截器就会完成部分功能代码),拦 ...
- 【PYTHON】编码是个细致活
python逆向获取DNS时用到一个函数socket.gethostbyaddr,传入本地主机地址后报错如下: UnicodeDecodeError: 'utf-8' codec can't deco ...
- C# HttpWebRequest post 请求传参数
Dictionary<string, string> parameters = new Dictionary<string, string>(); //参数列表 paramet ...
- nodeJs的一些常识知识
在项目目录中打开命令窗口 1. npm init 生成一个 package.json.(npm inii -y 直接生成,不用确定). 2.npm i . 下载 package.json devDep ...
- jquery选择器的一些处理
本文不讨论用jquery选择器具体怎么选择页面元素,而讨论选择元素后后的一些处理 jquery的选择器选择元素的时候,即使没有选择到指定的对象,页面并不会报错,例子: <!doctype htm ...
- 设置Pycharm在创建py文件时自动添加文件头注释(类似于钩子特性)
在每次新建一个py文件的时候 1 如何自动添加 #!/usr/bin/env python2 自动添加 #-*- coding: utf-8 -*- 操作方法: File -> se ...
- String、Stringbuffer、StringBuffer回顾
前言: 久了没用到,一下子就忘了..,参考文章:https://www.cnblogs.com/su-feng/p/6659064.html.https://baijiahao.baidu.com/s ...