并查集+最小生成树

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. Git管理unity3d项目

    如果小组中没有足够的专业版license,用不了unity3d自带的version control,可以使用git来对项目进行版本控制:只不过需要建一个.gitignore文件在git项目管理的根目录 ...

  2. CURL与PHP-CLI的应用【CURL篇】

    curl是一个极为强大的HTTP传输工具,支持文件的上传和下载; curl在命令行下的使用 命令参数 -a/--append 上传文件时,附加到目标文件 -A/--user-agent <str ...

  3. DVB系统中PCR的生成和PCR校正

    http://blog.csdn.net/chenliangming/article/details/3616720 引自<广播电视信息>2008年1月 从数字电视前端系统功能上来讲,传统 ...

  4. spring事物的传播行为

    1.spring事物的传播行为,主要是用来解决业务层拥有事物的方法,相互调用的问题. 2.声明事物, 在代码执行前,开启事务.代码执行完,提交事务 3.spring并没有提供事务具体的处理,而只是调用 ...

  5. Centos后台运行jar

    jar后台运行 nohup java -jar xx.jar >/dev/null & 此处的">/dev/null"作用是将终端输出信息输出到空洞中,即不保存 ...

  6. Android:控件GridView的使用

    如果是列表(单列多行形式)的使用ListView,如果是多行多列网状形式的优先使用GridView. <?xml version="1.0" encoding="u ...

  7. 【HDOJ】3828 A + B problem

    显然需要贪心,重叠越长越好,这样最终的串长尽可能短.需要注意的是,不要考虑中间结果,显然是个状态dp.先做预处理去重,然后求任意一对串的公共长度. /* 3828 */ #include <io ...

  8. write_chip,read_chip

    int write_chip(UINT32 addr, UINT32 data) { if(0 == fpgaRWMode) /* localbus mode */ { UINT16 datah, d ...

  9. Is there a way for me to run Adb shell as root without typing in 'su'?

    Orginal artical :http://android.stackexchange.com/questions/5884/is-there-a-way-for-me-to-run-adb-sh ...

  10. SCOI2007蜥蜴

    Description 在一个r行c列的网格地图中有一些高度不同的石柱,一些石柱 上站着一些蜥蜴,你的任务是让尽量多的蜥蜴逃到边界外. 每行每列中相邻石柱的距离为1,蜥蜴的跳跃距离是d,即蜥蜴可以跳到 ...