HDU 2122 Ice_cream’s world III【最小生成树】
解题思路:基础的最小生成树
反思:不明白为什么i从1开始取,就一直WA,难道是因为村庄的编号是从0开始的吗
Ice_cream’s world III
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1032 Accepted Submission(s): 335
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int pre[10010];
struct Edge
{ int u,v,w;
} edge[10010];
bool cmp(Edge n1,Edge n2)
{
return n1.w<n2.w;
}
int find(int root)
{
return root == pre[root] ? root : pre[root] = find(pre[root]);
}
int unionroot(int x,int y)
{
int root1=find(x);
int root2=find(y);
if(root1==root2)
return 0;
pre[root1]=root2;
return 1;
}
int main()
{
int n,m,i,j,tmp,x,y,ans;
while(scanf("%d %d",&n,&m)!=EOF)
{
tmp=0;
ans=0;
for(i=0;i<=10010;i++)
pre[i]=i;
for(i=0;i<m;i++)
scanf("%d %d %d",&edge[i].u,&edge[i].v,&edge[i].w); sort(edge,edge+m,cmp);
for(i=0;i<m;i++)
{
x=find(edge[i].u);
y=find(edge[i].v);
if(unionroot(x,y))
{
ans+=edge[i].w;
tmp++;
}
}
if(tmp!=n-1)
printf("impossible\n");
else
printf("%d\n",ans);
printf("\n");
}
}
HDU 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 ...
- A - Ice_cream’s world III
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Status Pract ...
- Ice_cream’s world III
Ice_cream's world III Time Limit : 3000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Othe ...
- Ice_cream’s world III(prime)
Ice_cream’s world III Time Limit : 3000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Othe ...
- hdu 5266 pog loves szh III(lca + 线段树)
I - pog loves szh III Time Limit:6000MS Memory Limit:131072KB 64bit IO Format:%I64d & %I ...
- hdu 2121 Ice_cream’s world II
Ice_cream’s world II http://acm.hdu.edu.cn/showproblem.php?pid=2121 Time Limit: 3000/1000 MS (Java/O ...
- hdu 2120 Ice_cream's world I
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2120 Ice_cream's world I Description ice_cream's worl ...
- Hdu 3371 Connect the Cities(最小生成树)
地址:http://acm.hdu.edu.cn/showproblem.php?pid=3371 其实就是最小生成树,但是这其中有值得注意的地方:就是重边.题目没有告诉你两个城市之间只有一条路可走, ...
随机推荐
- Powerpivot PowerBI相关组件下载安装(附操作截图)
加载方式:com加载项加载方法: 点击Excel界面[文件]→[选项]→[加载项]→[COM加载项]→[转到] Excel2013加载PowerView Excel216PowerQuery不需要加载 ...
- Unity 导入的模型检测不到碰撞
解决方案 添加Mesh Collider和rigidbody,并且Collider里边2个勾要勾上. 这是我碰到的问题.如果没解决你的问题,别打我,逃~
- ZBrush与同类数字雕刻软件的比较
随着数字雕刻软件的迅猛发展,不但在软件的数量和功能上有突飞猛进的提高,行业应用上也有很大的拓展.那么,与同类数字雕刻软件比较下,用户应该如何选择呢?下面我们来对这些软件做一个简单的罗列分析. 目前数字 ...
- Django框架详解之template
模板简介 将页面的设计和python的代码分离开会更干净简洁更容易维护.我们可以使用Django的模板系统来实现这种模式 python的模板:HTML代码+模板语法 模板包括在使用时会被值替换掉的变量 ...
- day13 基本的文件操作(好东西)
目录 基本的文件处理 什么是文件 如何使用文件 使用Python写一个小程序控制文件 open(打开文件) read: readline:一次性读取一行 del:删除 close:关闭 write(写 ...
- ubantu上搭建hive环境
上次我们在ubantu上安装了hadoop,那我们现在再进一步,开始我们的学习之旅--hive板块吧! 第一步:准备! 软件: I.一个搭建好的hadoop环境 II.hive的bin文件(前往apa ...
- arttemplate模板引擎里面多层循环
要使用支持原生js的插件: 代码截图: json数据 { "list": [ { "name": "学历层次", "item&qu ...
- luoguP2742 【模板】二维凸包 / [USACO5.1]圈奶牛 二维凸包
我们知道,纵坐标最小的点一定在凸包上(如果有多个,那它们都会被取到) 随便找一个纵坐标最小的点,将其他所有点按照这个点为原点极角排序,我们发现极角大的会在极角小的后面加入(感性认知一下) 考虑新(加入 ...
- echarts的下载为excel小工具
echarts自带有下载为图片的功能,但是没有下载为excel表格的功能,个人觉得下载为表格更为需要,所以写了个工具,此工具应用有局限性,只能用于柱形图和折线图,option的series要是如下形式 ...
- HDU-2955 Robberies 浮点数01背包 自变量和因变量位置互换
题目链接:https://cn.vjudge.net/problem/HDU-2955 题意 突然想找几个银行抢钱. 给出各银行的钱数和被抓的概率,以及能容忍的最大被抓概率. 问他最多能抢到多少钱? ...