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 ...
随机推荐
- linux grep和正则表达式
虽然正则表达式经常都在用,但是很少能够静下心来仔细的总结一下.最近看了一个台湾人的网站叫做鸟哥Linux私房菜,关于正则表达式的描述挺详细的.在此,我进行一下总结,如果想仔细的学习正则表达式,请访问鸟 ...
- BZOJ 2553 禁忌
首先我们要考虑给定一个串,如何将他划分,使得他有最多的禁忌串 我们只需要按里面出现的禁忌串们的出现的右端点排序然后贪心就可以啦 我们建出AC自动机,在AC自动机等价于走到一个包含禁忌串的节点就划分出一 ...
- 你只是看起来很努力(只是做了一遍真题,草草的对了一遍答案,然后冲出自习室继续她学生会的事情了,骗自己更容易)good——想起了自己在六大时候的无奈
(转)你只是看起来很努力一次上课,一个女孩子垂头丧气的跟我说,老师,我考了四次四级,还没过,究竟是为什么. 我说,你真题做了吗?单词背了吗?她拿出已经翻破了的真题,跟我说,你讲的所有的题目我连答案都记 ...
- VMware 11完全安装Mac OS X 10.10
本文已迁移到我的个人网站 http://www.wshunli.com 文章地址: http://www.wshunli.com/2016/03/17/VMware-12安装Mac-OS-X-10-1 ...
- Java IO2:字节流
输入输出流: • 输入/输出时, 数据在通信通道中流动. 所谓“数据流(stream)”指的是所有数据通信通道之中,数据的起点和终点. 信息的通道就是一个数据流.只要是数据从一个地方“流” 到另外一个 ...
- 117. Populating Next Right Pointers in Each Node II
题目: Follow up for problem "Populating Next Right Pointers in Each Node". What if the given ...
- UVA548——Tree(中后序建树+DFS)
Tree You are to determine the value of the leaf node in a given binary tree that is the terminal nod ...
- 简单的神经元算法实现(python)
参考python代码如下 #perceptron x=[[1 ,0, 0],[1,0,1],[1, 1, 0],[1, 1, 1],[0,0,1],[0,1,0],[0,1,1],[0,0,0]] y ...
- 食物卡喉别拍背部!救了100多万人性命的“海姆立克急救法"
先讲三个事例: 一.近日,浙江金华一个17月大的小贝边玩边吃花生,被噎住.10多分钟后,奶奶发现小贝大口喘气,以为他玩累了就抱他回家,等父母赶到送医已晚.小贝大脑受损严重-父母含泪同意放弃治疗,孩子走 ...
- css揭秘之按钮的实现技巧
<html> <title>css</title> <style> button{ padding: .3em .8em; border: 1px so ...