并查集+最小生成树

Ice_cream’s world III

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1121    Accepted Submission(s):
365

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
 
kruskal算法
 
#include<stdio.h>
#include<algorithm>
using namespace std;
int set[1100];
struct record
{
int beg;
int end;
int money;
}s[11000];
int find(int fa)
{
int ch=fa;
int t;
while(fa!=set[fa])
fa=set[fa];
while(ch!=fa)
{
t=set[ch];
set[ch]=fa;
ch=t;
}
return fa;
}
void mix(int x,int y)
{
int fx,fy;
fx=find(x);
fy=find(y);
if(fx!=fy)
set[fx]=fy;
}
bool cmp(record a,record b)
{
return a.money<b.money;
}
int main()
{
int n,m,j,i,sum,village,road;
while(scanf("%d%d",&village,&road)!=EOF)
{ for(i=0;i<village;i++)
set[i]=i;
for(i=0;i<road;i++)
{
scanf("%d%d%d",&s[i].beg,&s[i].end,&s[i].money);
}
sort(s,s+road,cmp);
sum=0;
for(i=0;i<road;i++)
{
if(find(s[i].beg)!=find(s[i].end))
{
mix(s[i].beg,s[i].end);
sum+=s[i].money;
}
}
m=0;
for(i=0;i<village;i++)
{
if(set[i]==i)
m++;
}
if(m>1)
printf("impossible\n\n");
else
printf("%d\n\n",sum);
}
return 0;
}

  

hdoj 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. HDU 2122 Ice_cream’s world III【最小生成树】

    解题思路:基础的最小生成树反思:不明白为什么i从1开始取,就一直WA,难道是因为村庄的编号是从0开始的吗 Ice_cream’s world III Time Limit: 3000/1000 MS ...

  3. Ice_cream’s world III(prime)

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

  4. hdoj 2121 Ice_cream’s world II 【没有最低树的根节点】

    称号:pid=2121" target="_blank">hdoj 2121 Ice_cream's world II 题意:题目是一道躶题,给n个点,m条边的有向 ...

  5. Ice_cream’s world III

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

  6. A - Ice_cream’s world III

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

  7. HDOJ.2064 汉诺塔III

    汉诺塔III Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submis ...

  8. hdoj 2120 Ice_cream's world I【求成环数】

    Ice_cream's world I Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  9. HDOJ 2120 Ice_cream's world I

    Ice_cream's world I ice_cream's world is a rich country, it has many fertile lands. Today, the queen ...

随机推荐

  1. http://jingyan.baidu.com/article/7908e85c74d85faf491ad260.html

    http://jingyan.baidu.com/article/7908e85c74d85faf491ad260.html

  2. Servlet中乱码问题

    页面编码方式为utf-8 当使用post传值时 request.setContentType("utf-8"); 当使用get传值时 String str = request.ge ...

  3. HeadFirst设计模式之代理模式

    一. 1.The Proxy Pattern provides a surrogate or placeholder for another object to control access to i ...

  4. Java 关于中文乱码处理的经验总结【转载】

    为什么说乱码是中国程序员无法避免的话题呢?这个首先要从编码机制上说起,大家都是中文和英文的编码格式不是一样,解码也是不一样的!如果中国的程序员不会遇到乱码,那么只有使用汉语编程.汉语编程是怎么回事我也 ...

  5. Android 签名(2)签名知识要点

    要点 1) 所有的应用程序都必须有数字证书,Android系统不会安装一个没有数字证书的应用程序 2) Android程序包使用的数字证书可以是自签名的,不需要一个权威的数字证书机构签名认证 3) 如 ...

  6. 编写高效的C程序与C代码优化

    本文地址:http://www.cnblogs.com/archimedes/p/writing-efficient-c-and-code-optimization.html,转载请注明源地址. 说明 ...

  7. poj2761

    表面上看是主席树之类的区间k大 实际上,除了主席树,还可以测各种结构 因为题目中说,任意区间不会完全包含 于是,我们把区间按左端点排序,依次添加,用平衡树求当前的k大 每个狗最多被添加一次,删除一次 ...

  8. bzoj1858: [Scoi2010]序列操作

    lazy-tag线段树. #include<cstdio> #include<algorithm> #include<cstring> using namespac ...

  9. bzoj2243:[SDOI2011]染色

    链剖就可以了.一开始的想法错了.但也非常接近了.妈呀调的要死...然后把字体再缩小一号查错起来比较容易QAQ. #include<cstdio> #include<cstring&g ...

  10. 在SQL Server实现最短路径的搜索

    开始 这是去年的问题了,今天在整理邮件的时候才发现这个问题,感觉顶有意思的,特记录下来. 在表RelationGraph中,有三个字段(ID,Node,RelatedNode),其中Node和Rela ...