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/Others)
Total Submission(s): 1121 Accepted Submission(s):
365
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.
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.
must be output “impossible”, otherwise, print the minimum cost in this project.
After every case print one blank.
#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的更多相关文章
- 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 ...
- HDU 2122 Ice_cream’s world III【最小生成树】
解题思路:基础的最小生成树反思:不明白为什么i从1开始取,就一直WA,难道是因为村庄的编号是从0开始的吗 Ice_cream’s world III Time Limit: 3000/1000 MS ...
- Ice_cream’s world III(prime)
Ice_cream’s world III Time Limit : 3000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Othe ...
- hdoj 2121 Ice_cream’s world II 【没有最低树的根节点】
称号:pid=2121" target="_blank">hdoj 2121 Ice_cream's world II 题意:题目是一道躶题,给n个点,m条边的有向 ...
- Ice_cream’s world III
Ice_cream's world III Time Limit : 3000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Othe ...
- A - Ice_cream’s world III
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Status Pract ...
- HDOJ.2064 汉诺塔III
汉诺塔III Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submis ...
- 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 ...
- 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 ...
随机推荐
- ???????????? no permissions
1.一手鞋地址 google http://developer.android.com/tools/device.html 我处理的方法如下: 我的问题: android的版卡 在Ubuntu12.0 ...
- java jdbc dbcp连接SQL Server
使用到的jar: commons-collections-3.1.jar commons-dbcp-1.4.jar commons-pool-1.5.6.jar sqljdbc4.jar dbcp配置 ...
- 一周一话题之四(JavaScript、Dom、jQuery全面复习总结<Dom篇>)
-->目录导航 一. 初探Dom 1. Dom介绍 二. Dom基础 1. window顶级对象 2. body.document对象事件 3. 通用的HTML元素的事件 4. 冒泡事件 5. ...
- [Gauss]HDOJ3364 Lanterns
题意:有n个灯笼,m个开关 每个开关可以控制k个灯笼, 然后分别列出控制的灯笼的编号(灯笼编号为1到n) 下面有Q个询问,每个询问会有一个最终状态(n个灯笼为一个状态)0代表关 1代表开 问到达这种状 ...
- Ubuntu 12.04安装字体
http://www.2cto.com/os/201210/160645.html 安装方法,终端输入: $ sudo thunar /usr/share/fonts/truetype 待ture ...
- 功能齐全、效率一流的免费开源数据库导入导出工具(c#开发,支持SQL server、SQLite、ACCESS三种数据库),每月借此处理数据5G以上
软件名:DataPie 功能:支持SQL server.SQLite.ACCESS数据库的导入.导出.存储过程调用,支持EXCEL2007.EXCEL2003.ACCESS2007. CSV文件导入数 ...
- JavaScript DOM高级程序设计1.3-常见陷阱--我要坚持到底!
区分大小写 单引号双引号 大多数开发人员选择用单引号,因为XTHML要求所有XHTML的属性都必须使用双引号 var html='<h2 class="a">A lis ...
- 【TopCoder】SRM 680 DIV 2
1. BearPair之bigDistance1.1 题目概述在 <= 50的字符串中找位置i,j 满足(1) s[i] != s[j];(2) abs(i-j)尽可能大.若不存在返回-1, 否 ...
- 【HDOJ】4297 One and One Story
综合性很强的题目.存在环,可以用tarjan处理,然后需要求LCA.并查集+RMQ可以搞.非常不错的题目. /* 4297 */ #include <iostream> #include ...
- 基于邻接矩阵的深度优先搜索(DFS)
题目:http://acm.sdut.edu.cn/sdutoj/showproblem.php?pid=2107&cid=1186 #include<stdio.h> #incl ...