Ice_cream’s world III

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 1237    Accepted Submission(s): 408

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
 
Author
Wiskey
浮在水面上的小岛要连通,求最少花费,假设没有,则输出impossible。
代码1【克鲁斯卡尔】:


#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<cmath>
using namespace std; int n,m;
int pre[1010];
struct node{
int u;
int v;
int w;
};
node sb[10010]; bool cmp(node a,node b)
{
return a.w<b.w;
} int find(int x)
{
if(pre[x]==x)
return x;
return pre[x]=find(pre[x]);
} bool join(int x,int y)
{
int f1,f2;
f1=find(x);
f2=find(y);
if(f1==f2)
return false;
if(f1!=f2)
pre[f1]=f2;
return true;
} int main()
{
int sum;
while(scanf("%d%d",&n,&m)!=EOF)
{
sum=0;
for(int i=0;i<n;i++)
pre[i]=i;
for(int i=0;i<m;i++)
scanf("%d%d%d",&sb[i].u,&sb[i].v,&sb[i].w);
sort(sb,sb+m,cmp);
for(int i=0;i<m;i++)
{
if(join(sb[i].u,sb[i].v))
sum+=sb[i].w;
}
int cnt=0;
for(int i=0;i<n;i++)
{
if(pre[i]==i)
cnt++;
}
if(cnt>1)
printf("impossible\n\n");
else
printf("%d\n\n",sum);
}
return 0;
}

代码2【普利姆】:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
const int INF= 0x3f3f3f3f;
const int maxb=1010;
int map[maxb][maxb];
int vis[maxb];
int n,m,sum;
int a,b,c; void prime()
{
int i,j,k,dis[maxb];
int min;
memset(vis,0,sizeof(vis));
int ans=1;
vis[0]=1;
for(i=0;i<n;i++)
dis[i]=map[0][i];
for(i=0;i<n;i++)
{
min=INF;
for(j=0;j<n;j++)
if(!vis[j]&&min>dis[j])
min=dis[k=j];
if(min==INF)
{
if(ans==n)
printf("%d\n",sum);
else
puts("impossible");
break;
}
sum+=min;
vis[k]=1;
ans++;
for(j=0;j<n;j++)
if(!vis[j]&&dis[j]>map[k][j])
dis[j]=map[k][j];
}
} int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(map,INF,sizeof(map));
sum=0;
while(m--)
{
scanf("%d%d%d",&a,&b,&c);
if(map[a][b]>c)
map[a][b]=map[b][a]=c;
}
//getchar();
prime();
//getchar();
puts("");
}
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 ...

  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. PKU 2528 Mayor's posters

    题意: 一个公告板上面贴海报,宽度都是一样的,长度可能不一样.后面的海报可能把前面的覆盖掉.问最后能看见多少张不同的海报. 思路: 这题原来做过,是用线段树的区间染色写的.记录每个区间是纯色还是杂色. ...

  2. 【AHOI2013】【BZOJ3238】差异

    Description Input 一行,一个字符串S Output 一行.一个整数.表示所求值 Sample Input cacao Sample Output 54 HINT 2<=N< ...

  3. 在Windows上调整SGA大小遭遇ora-27100、ora-27102错误的处理方法

    今天早上去一公司合作伙伴那里,协助处理他们某客户的数据库性能问题,那个库是Oracle 10.2.0.1的,前台业务系统是政府某机构查询系统,碰到的问题是首页展示很慢,与之相关的SQL语句查询结果须要 ...

  4. Array 对象常用的方法总结

    shift:删除原数组的第一项,返回删除元素的值:如果数组为空则返回undefined var arr = [1, 2, 3, 4, 5]; var out = arr.shift(); consol ...

  5. Python(九) Python的高级语法与用法

    本章节我们揭开Python进阶部分的高级特性,详细讲解枚举.闭包,并对函数式编程做出介绍 一. 枚举其实是一个类 from enum import Enum #枚举类 class VIP(Enum): ...

  6. OOM框架AutoMapper基本使用(1)

    OOM顾名思义,Object-Object-Mapping实体间相互转换,AutoMapper也是个老生常谈了,其意义在于帮助你无需手动的转换简单而又麻烦的实体间关系,比如ViewModel和enti ...

  7. spring 中国下载点

    http://repo.spring.io/libs-release-local/org/springframework/spring/ spring 中国下载点

  8. css3 实现动画效果

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. Conventions and patterns for multi-platform development

    For Developers‎ > ‎Design Documents‎ > ‎ Conventions and patterns for multi-platform developme ...

  10. 打印机共享为什么老是出现“操作无法完成(错误 0X00000709)。再次检查打印机名称、并确保打印机连接网络

    这个情况应该是访问IP连接打印机才会出现的.解决办法:不使用IP访问,使用网上邻居找计算机名称再连接打印机即可. ------------------------------------------- ...