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 其实就是最小生成树,但是这其中有值得注意的地方:就是重边.题目没有告诉你两个城市之间只有一条路可走, ...
随机推荐
- jQuery在多个div中,删除指定项
之前工作中有一个需求,就是在一堆图片列表中,点击具体的图片,并从界面移除:点击具体的图片,下载:这是一个思路 <style type="text/css" media=&qu ...
- pgpool中定义的数据库节点及pgpool支持的复制模式
/* * The first DB node id appears in pgpool.conf or the first "live" DB * node otherwise. ...
- [THUWC2017]在美妙的数学王国中畅游 LCT+泰勒展开+求导
p.s. 复合函数求导时千万不能先带值,再求导. 一定要先将符合函数按照求导的规则展开,再带值. 设 $f(x)=g(h(x))$,则对 $f(x)$ 求导: $f'(x)=h'(x)g'(h(x)) ...
- Codeforces Round #493 (Div. 2) B. Cutting 前缀和优化_动归水题
不解释,题目过水 Code: #include<cstdio> #include<cmath> #include<algorithm> using namespac ...
- 如何降低死循环的 CPU 占用
有的时候程序中需要使用死循环,比如消息监听就要用一个死循环,直到受到消息请求关闭才可能跳出循环. 一个 while(true){} 的循环中即便循环体是空的,也会占用几乎一整个 CPU 核心.为了降低 ...
- easyUI datagrid的合并的js封装
$.extend($.fn.datagrid.methods, { autoMergeCells : function (jq, fields) { return jq.each(function ( ...
- postgressql sql查询拼接多个字段为一个字段查询出来
表年份 月份 数据1 数据22000 1 1 12000 2 2 12001 2 2 2 2001 5 5 4 希望的查询结果如下所示: 时间 数据1 数据22000年1月 1 12000年2月 2 ...
- Module build failed: Module failed because of a eslint warning
eslint 设置 warning 级别,在 开发编译失败的原因,报错如下: F:\vue-mobile-skeleton>npm run dev > byhealth@1.0.0 dev ...
- ASP.NET Menu控件点击区域太小解决方法
ASP.NET自带的Menu控件点击区域比较小,基本就是文本范围和图片范围,在区域外虽然选择的项有颜色变化,但是这个时候点击是没有用的,体验不是很好 检查前台生成的HTML,是用td嵌套a标签,a标签 ...
- [读书笔记] R语言实战 (六) 基本图形方法
1. 条形图 barplot() #载入vcd包 library(vcd) #table函数提取各个维度计数 counts <- table(Arthritis$Improved) count ...