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. JAVA类和对象课后作业

    1.使用类的静态字段和构造函数,我们可以跟踪某个类所创建对象的个数.请写一个类,在任何时候都可以向它查询"你已经创建了多少个对象?" 代码: //显示类 //YiMingLai 2 ...

  2. HDU 1513 最长子序列

    Palindrome Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  3. μC/OS-Ⅲ系统的资源管理

    一.各种资源管理方法简介 μC/OS-Ⅲ系统中提供了一些基本方法用于管理共享资源(典型的共享资源有:变量.数据结构体.RAM中的表格.IO设备中的寄存器等).资源共享方法名称及适用范围如下表所示. 资 ...

  4. c++学习笔记2

    T: 1.默认实参只能声明一次:  设计含有默认实参的函数时,要合理设置形参的顺序 :局部变量不能作为默认实参 2.内联机制用于优化规模较小.流程直接.频繁调用的函数 3.constexpr函数  返 ...

  5. mysql使用

    1.以查询结果建表 create table newTableName select column1 [newName1] [, column2 [newName2], .. , columnn [n ...

  6. C#注册表的读,写,删除,查找

    首先分享一下写入,这个最常用的 public bool WriteRegedit()     {         try         {             RegistryKey rk = ...

  7. latex使用问题总结1

    文献引用问题 若使用了\usepackage[super, square, number, sort&compress]{natbib},\cite的文献并不与文本平行,反而位于右上角 文献引 ...

  8. JavaScript判断数据类型

    JavaScript中判断数据类型的方式有三种: 1.typeof typeof 1;   //"number" typeof "abc";  //" ...

  9. C# 判断文件有没占用

    C# 判断文件有没占用 using System; using System.Collections.Generic; using System.Text; using System.Runtime. ...

  10. asp.net mvc api auth

    一.登录 /// <summary> /// 获取令牌 /// </summary> /// <param name="userName">用户 ...