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): 1237 Accepted Submission(s): 408
city to the capital. The project’s cost should be as less as better.
2 1
0 1 10 4 0
10 impossible
#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【最小生成树】的更多相关文章
- hdoj 2122 Ice_cream’s world III
并查集+最小生成树 Ice_cream’s world III Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 ...
- 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 ...
随机推荐
- 洛谷 P1703 那个什么密码2
P1703 那个什么密码2 题目背景 https://www.luogu.org/problem/show?pid=1079 题目描述 与原题一模一样.具体不同请见输入格式 输入输出格式 输入格式: ...
- [Spark源代码剖析] DAGScheduler提交stage
转载请标明出处:http://blog.csdn.net/bigbigdata/article/details/47310657 DAGScheduler通过调用submitStage来提交stage ...
- Windows App开发之文件与数据
读取文件和目录名 这一节開始我们将陆续看到Windows App是如何操作文件的. 在Windows上读取文件名称.目录名 首先我们在XAML中定义一个Button和TextBlock,将读取文件/目 ...
- python 二分法O(logn)
def bin_search(data_set, val): low = high = len(data_set) - while low <= high: mid = (low + high) ...
- impala 概述
impala 概述 什么是Impala? Impala是用于处理存储在Hadoop集群中的大量数据的MPP(大规模并行处理)SQL查询引擎. 它是一个用C ++和Java编写的开源软件. 与其他Had ...
- 在Windows上如何安装和彻底卸载Adobe Flash Player教程
很多小伙伴在安装水晶易表的时候,经常会遇到“Xcelsius2008需要使用Adobe Flash Player(9.151或者更高版本)”报错问题,如下图所示. 导致安装进程受阻,此时就需要安装高版 ...
- caioj 1066 动态规划入门(一维一边推4:护卫队)(分组型dp总结)
很容易想到f[i]为前i项的最优价值,但是我一直在纠结如果重量满了该怎么办. 正解有点枚举的味道. 就是枚举当前这辆车与这辆车以前的组合一组,在能组的里面取最优的. 然后要记得初始化,因为有min,所 ...
- 【Uva 10817】Headmaster's Headache
[Link]: [Description] 一个学校,有s门课程(1<=s <=8),里面本身已经有m个老师了,然后还想招聘n个老师: 给出这m个老师和n个来应聘的老师的信息; (c[i] ...
- [Python] Format Strings in Python
Single quotes and double quotes can both be used to declare strings in Python. You can even use trip ...
- PHP 使用Apache 中的ab 測试站点的压力性能
打开Apacheserver的安装路径(我用的是 WampServer),在bin文件夹中有一个ab.exe的可运行程序,它就是要介绍的压力測试工具. watermark/2/text/aHR0cDo ...