hdu 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): 926    Accepted Submission(s): 303

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

Source

HDU 2007-10 Programming Contest_WarmUp

最小生成树:prime 算法,还有克鲁斯卡尔算法。

两者都可以处理。

prime代码如下:

#include<stdio.h>
#include<string.h>
int v[10010],map[10010][10010];
#define inf 0xfffffff;
int main()
{
	int n,m,i,j,a,b,c;
	while(~scanf("%d%d",&n,&m))
	{
		int sum=0;
		memset(v,0,sizeof(v));
		for(i=0;i<n;i++)//初始化
		{
			for(j=0;j<n;j++)
			{
				map[i][j]=inf;
			}
		}
		for(i=0;i<m;i++)
		{
			scanf("%d%d%d",&a,&b,&c);
			if(c<map[a][b])//判断是否是重边,要是起点终点都相同的时候,就去权值最小的作为两个顶点间的距离即是权值
			map[a][b]=map[b][a]=c;
		}
		v[0]=1;
		int flag;
		for(i=1;i<n;i++)
		{
			int min=inf;
			flag=-1;
			for(j=0;j<n;j++)
			{
				if(!v[j]&&map[0][j]<min)
				{
					min=map[0][j];
					flag=j;
				}
			}
			if(flag==-1)
			break;
			v[flag]=1;
			sum+=map[0][flag];
			for(j=0;j<n;j++)
			{
				if(!v[j]&&map[0][j]>map[flag][j])
				map[0][j]=map[flag][j];
			}
		}
		if(flag==-1)
		printf("impossible\n\n");
		else
		printf("%d\n\n",sum);
	}
	return 0;
}

kuskal算法代码如:

#include<stdio.h>
#include<algorithm>
using namespace std;
struct node{
	int a,b,c;
}s[10010];
int father[10010];
int find(int r)
{
	return r==father[r]?r:find(father[r]);
}
int cmp(node x,node y)
{
	return x.c<y.c;
}
int main()
{
	int n,m,i,j;
	while(~scanf("%d%d",&n,&m))
	{
		for(i=0;i<n;i++)//初始化
		{
			father[i]=i;
		}
		for(i=0;i<m;i++)
		{
			scanf("%d%d%d",&s[i].a,&s[i].b,&s[i].c);
		}
		sort(s,s+m,cmp);//求的是最小的生成树,所以按照权值 增序排列
		int count=0,sum=0;
		for(i=0;i<m;i++)//一共有m条边,所有循环到m
		{
			int fa=find(s[i].a);
			int fb=find(s[i].b);
			if(fa!=fb)
			{
				father[fa]=fb;
				sum+=s[i].c;
				count++;//除了这一点都是克鲁斯卡尔算法模板
			}
		}
		if(count!=n-1)//是否是n-1条边 即建立了一个满足条件的最小生成树
		{
			printf("impossible\n\n");
		}
		else
		printf("%d\n\n",sum);
	}
	return 0;
} 

时间: 2024-10-12 04:58:17

hdu 2122(Ice_cream’s world III)(最小生成树,两种算法都可以)的相关文章

hdu 2122 Ice_cream’s world III(最小生成树)

感觉就是 畅通工程的改版 直接贴代码了 #include<stdio.h> #include<string.h> #include<math.h> #include<iostream> #include<algorithm> #include<queue> #include<stack> #define mem(a,b) memset(a,b,sizeof(a)) #define ll __int64 #define MA

HDU - 2122 Ice_cream’s world III

题目来源:http://acm.hdu.edu.cn/showproblem.php?pid=2122 最小生成树问题,可采用Kruskal算法,贪心策略,每次选取无向带权图的最短边,并把两端点用 并查集的方式添加到一个集合内. 1 #include<stdio.h> 2 #include<string.h> 3 #include<algorithm> 4 const int maxn=1000+5,maxm=10000+10; 5 int u[maxm],v[maxm

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

解题思路:基础的最小生成树反思:不明白为什么i从1开始取,就一直WA,难道是因为村庄的编号是从0开始的吗 Ice_cream’s world III Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1032    Accepted Submission(s): 335 Problem Description ice_cream’s wo

树:最小生成树-两种算法

先来说说什么是树. 树实际上是图的一种,当一个有N个点的无向连通图,只有N-1条边时,就是一棵树,即树中不会有环出现:所以对于一个图,删除某些环中的某条边,使得该图成为一棵树,那么这棵树就称为生成树. 而最小生成树的意思就是,给定有n个顶点的带权图G(E,V),找到一棵生成树,求该生成树的边权和. Kruskal算法: 算法步骤: 1.构造一个有n个顶点的无边子图: 2.从原图选择边权最小的边加入该子图,直至子图成为一棵树: 3.边能加入子图的条件是,边的两个端点u,v还未连通,Kruskal算

hdu 3371 Connect the Cities Prim + Kruskal两种算法分别AC 水过~~~~

Connect the Cities Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 11727    Accepted Submission(s): 3278 Problem Description In 2100, since the sea level rise, most of the cities disappear. Tho

图的深度优先搜索和广度优先搜索算法、最小生成树两种算法 --C++实现

一:通用图结构 #ifndef _GRAPH_H #define _GRAPH_H #include <iostream> #include <string.h> #include <assert.h> #include <queue> using namespace::std; #define MAX_COST 0x7FFFFFFF //花费无限大设为整型最大值 ///////////////////////////////////////////////

hdu 1162 Eddy&#39;s picture 最小生成树入门题 Prim+Kruskal两种算法AC

Eddy's picture Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7428    Accepted Submission(s): 3770 Problem Description Eddy begins to like painting pictures recently ,he is sure of himself to

HDU 1162 Eddy&#39;s picture【最小生成树,Prime算法+Kruskal算法】

Eddy's picture Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 9334    Accepted Submission(s): 4711 Problem Description Eddy begins to like painting pictures recently ,he is sure of himself to

最小生成树的两种算法:Prim和Kruskal算法

越来越明白了一个道理:你写不出代码的原因只有一个,那就是你没有彻底理解这个算法的思想!! 以前写过最小生成树,但是,水了几道题后,过了一段时间,就会忘却,一点也写不出来了.也许原因只有一个,那就是我没有彻底理解这两种算法. 主题: 其实,求最小生成树有两个要点,一个是权值最小,还有一个就是这个图必须是树.而Prim和Kruskal的不同之处在于两者选择的变量不同,Prim选择的是始终保持权值最小,然后逐个加点构建一棵树.而Kruskal则是始终保证是一棵树(虽然构建过程中不一定是真正的树,但并查