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

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
简单的最小生成树问题,用并查集来做。
 #include<cstdio>
#include<string.h>
#include<algorithm>
using namespace std;
int ans;
int father[],rank[];//父节点,深度记录(这道题没必要用深度记录)
//int max1;
struct Line{
int first;
int last;
int cost;
}line[];//还没学会用数组来记录边,还是在用结构体做的,编程风格有待提高
int cmp(Line a,Line b){return a.cost<b.cost;}//排序的依据函数
int find(int x)//找点的父节点,把一条边的点搞到一个集合里面
{
if(father[x]!=x)
{
father[x]=find(father[x]);//这里有个优化,不会出现长链的情况,直接把father[x]=find[father[x]];
}
return father[x];
}
bool Union(int a,int b)//合并集合
{
int a1=find(a),b1=find(b);
if(a1==b1) return false;//如果是同一个父亲节点的,return false;
else{
father[b1]=a1;//否则合并成一个集合
ans++;
rank[a1]+=rank[b1];
if(max1<rank[a1]) max1=rank[a1];
return true;
}
} int main()
{
int a,b,c,n,m;
while(scanf("%d%d",&n,&m)!=EOF)
{
for(int i=;i<n;i++)
{
father[i]=i;
rank[i]=;
}
for(int i=;i<=m;i++)
{
scanf("%d %d %d",&a,&b,&c);
line[i].first=a;
line[i].last=b;
line[i].cost=c; }
sort(line+,line+m+,cmp);
max1=;
int sum=;
ans=;
for(int i=;i<=m;i++)
{
if(Union(line[i].first,line[i].last))
sum+=line[i].cost;
}
if(ans==n-) printf("%d\n",sum);
else printf("impossible\n");
printf("\n");
}
return ;
}

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

  2. Ice_cream’s world III(prime)

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

  3. Ice_cream’s world III

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

  4. HDU 2122 Ice_cream’s world III【最小生成树】

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

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

  6. 【HDU2122】Ice_cream’s world III(MST基础题)

    2坑,3次WA. 1.判断重边取小.2.自边舍去. (个人因为vis数组忘记初始化,WA了3次,晕死!!) #include <iostream> #include <cstring ...

  7. Ice_cream’s world III--2122

    Ice_cream’s world III Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

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

  9. 【转】并查集&MST题集

    转自:http://blog.csdn.net/shahdza/article/details/7779230 [HDU]1213 How Many Tables 基础并查集★1272 小希的迷宫 基 ...

随机推荐

  1. CodeForces #363 div2 Vacations DP

    题目链接:C. Vacations 题意:现在有n天的假期,对于第i天有四种情况: 0  gym没开,contest没开 1  gym没开,contest开了 2 gym开了,contest没开 3 ...

  2. mac 解决eclipse OutOfMemoryError

    1.找到eclipse.ini文件 找到你的eclipse图标右击————>显示包内容-->contents -->macos -->eclipse.ini 2.修改内容 -s ...

  3. ajax的探究与使用

    前端必须掌握ajax,这是几乎所有前端招聘都会要求的一项. 但其实ajax也就是一种异步请求的技术,没有什么很深的东西,不过接触ajax很长一段时间了,早该整理下ajax的学习和使用: PART1: ...

  4. spark示例

    1)java(App.java) package com.ejiajie.bi.hello; import org.apache.spark.api.java.JavaSparkContext; im ...

  5. 简单研究下Retrofit

    2015-09-24 15:36:26 第一部分: 1. 什么是Retrofit? (点击图片有惊喜) 以上是来自官网的解释,言简意赅,咳咳,我就不翻译了~ 2. 如何使用Retrofit? 2.1 ...

  6. 在PHP中如何实现在做了么个操作后返回到指定页面

    我们经常会碰到类似用户在没有登录的情况下进行提问.评论,需要用户登录后返回刚才浏览的网页,这种功能用cookie保存当前url地址来实现.我用的是jquery,读者需要懂点jquery中的ajax请求 ...

  7. HDU 1005 F(Contest #1)

    题意: 已知f[1] = f[2] = 1,输入三个数a,b,n,求f[n] = (a*f[n-1]+b*f[n-2])%7的结果 分析: f[n-1]和f[n-2]最多为7种情况(0,1,2,3,4 ...

  8. HTML,CSS,font-family:中文字体的英文名称 (宋体 微软雅黑)

    工作中遇到的问题,上网看到别人整理的,我就记下来,嘻嘻!!! 宋体 SimSun 黑体 SimHei 微软雅黑 Microsoft YaHei 微软正黑体 Microsoft JhengHei 新宋体 ...

  9. java打jar包,引用其他.jar文件

    大家都知道一个java应用项目可以打包成一个jar,当然你必须指定一个拥有main函数的main class作为你这个jar包的程序入口. 具体的方法是修改jar包内目录META-INF下的MANIF ...

  10. hibernate的一种报错

    Exception in thread "main" java.lang.NoClassDefFoundError: javax/tools/StandardJavaFileMan ...