解题思路:基础的最小生成树
反思:不明白为什么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

Problem Description
ice_cream’s world becomes stronger and stronger; every road is built as undirected. The queen enjoys traveling around her world; the queen’s requirement is like II problem, beautifies the roads, by which there are some ways from every city to the capital. The project’s cost should be as less as better.
 
Input
Every case have two integers N and M (N<=1000, M<=10000) meaning N cities and M roads, the cities numbered 0…N-1, following N lines, each line contain three integers S, T and C, meaning S connected with T have a road will cost C.
 
Output
If Wiskey can’t satisfy the queen’s requirement, you must be output “impossible”, otherwise, print the minimum cost in this project. After every case print one blank.
 
Sample Input
2 1 0 1 10 4 0
 
Sample Output
10 impossible
 
Author
Wiskey
#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【最小生成树】的更多相关文章

  1. 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 ...

  2. hdoj 2122 Ice_cream’s world III

    并查集+最小生成树 Ice_cream’s world III Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 ...

  3. A - Ice_cream’s world III

    Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Pract ...

  4. Ice_cream’s world III

    Ice_cream's world III Time Limit : 3000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Othe ...

  5. Ice_cream’s world III(prime)

    Ice_cream’s world III Time Limit : 3000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Othe ...

  6. hdu 5266 pog loves szh III(lca + 线段树)

    I - pog loves szh III Time Limit:6000MS     Memory Limit:131072KB     64bit IO Format:%I64d & %I ...

  7. 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 ...

  8. 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 ...

  9. Hdu 3371 Connect the Cities(最小生成树)

    地址:http://acm.hdu.edu.cn/showproblem.php?pid=3371 其实就是最小生成树,但是这其中有值得注意的地方:就是重边.题目没有告诉你两个城市之间只有一条路可走, ...

随机推荐

  1. python课程设计笔记(一)开发环境配置

    今天开始学python,一个月后交成果?还是希望自己不要浮躁,认真地去学,有所付出也不期望太大回报. 现在还是一脸懵逼的状态,看着教程一点点来吧= = 毕竟我是最棒的最发光的阳光彩虹小白马! 1. 去 ...

  2. js判断传入时间和当前时间大小

    //判断时间是否过期 function judgeTime(time){ var strtime = time.replace("/-/g", "/");//时 ...

  3. Android Handler学习笔记

    已经习惯了挖坑不填,继续任性一下,周一到周五继续挖坑,每周六周日负责填坑. 1.从Android UI线程谈起 出于性能考虑,Android 中的UI操作并不是线程安全的,所以Android中规定只能 ...

  4. datatable.rows.indexof(dr)返回的是啥?

    返回的是Int类型的 行索引值,从0开始.也就是说,第一行是0.最后一行就是rows.count - 1.不会返回-1."这是第" + OldDt.Rows.IndexOf(ite ...

  5. https://blog.csdn.net/sxf359/article/details/71082404

    https://blog.csdn.net/sxf359/article/details/71082404

  6. Test zram at kernel 3.10 4.12

    Use ltp to test zram 测试环境: #uname -r 3.10.0-327.ali2010.rc6.alios7.x86_64 没有指定zram algorithm(没有设置), ...

  7. POJ 1905 Expanding Rods( 二分搜索 )

    题意:一个钢棍在两面墙之间,它受热会膨胀成一个圆弧形物体,这个物体长 S = ( 1 + n * C ) * L,现在给出原长 L ,温度改变量 n ,和热膨胀系数 C,求膨胀后先后中点的高度差. 思 ...

  8. zabbix_get 获取agnet端mysql数据失败

    问题 在使用zabbix_get获取agent端的mysql数据时,总是报错,ERROR 2002 (HY000): Can't connect to local MySQL server throu ...

  9. Jquery学习总结(4)——高效Web开发的10个jQuery代码片段

    在过去的几年中,jQuery一直是使用最为广泛的JavaScript脚本库.今天我们将为各位Web开发者提供10个最实用的jQuery代码片段,有需要的开发者可以保存起来. 1.检测Internet ...

  10. HashMap导致死循环问题

    虽然我推测是链表形成闭环,但 没有去证明过.从网上找了一下: http://blog.csdn.net/autoinspired/archive/2008/07/16/2662290.aspx 里面也 ...