HDU 2122 Ice_cream’s world III

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
 


PS:注意输出格式,一个答案两个换行;

Prime算法:

  1. 在图中任选一点作为开始节点,把这个顶点加入已经访问节点的集合。
  2. 求出这样一点的相邻节点的距离。并找出最短的一条边。这是第一步,找到的第一条边。把这一个节点加入已经访问过的集合。
  3. 在已经访问过的集合中,再次寻找最短的一条边。这一次还要考虑之前加入的顶点。因为之前的一个顶点到所有的距离已经求出。这一次就可以判断此点到其他点的距离,若距离小于之前对应的距离,则更新;
所以这个dijkstra的变形,只不过权值发生了替换成了累加;
AC代码:(Prime算法)
#include <bits/stdc++.h>
#define MAX 999999999
using namespace std ;
int s , t , c ,ans , ma[10010][10010],vis[10010],dis[10010],n,m;
void dijk()
{
	int mini ,  k ,flag;
	ans =flag =0 ;
	for(int i = 1 ; i < m;i++)
	{
		dis[i]=ma[0][i];
	}
	vis[0]=1;
	for(int i = 1 ; i < m ; i++)
	{
		mini = MAX ;
		for(int j = 0 ; j <m;j++ )
		{
			if(!vis[j]&&mini>dis[j])
			{
				mini = dis[j];
				k= j ;
			}
		}
		if(mini==MAX)
		{
			printf("impossible\n\n");
			flag = 1 ;
			break;
		}
		vis[k]=1 ;
		ans+=mini;
		for(int j = 0 ; j < m ; j++)
		{
			if(!vis[j]&&dis[j]>ma[k][j])
			dis[j]=ma[k][j];
		}
	}
	if(flag==0) printf("%d\n\n",ans);
}
int main()
{
	while(cin>>m>>n)
	{
		for(int i = 0 ; i < m ; i++)
		{
			for(int j = 0 ; j < m ; j++)
			{
				ma[i][j]=MAX;
			}
			ma[i][i]=0;
			vis[i]=0;
			dis[i]=MAX;
		}
		for(int i = 0 ; i < n ;i++)
		{
			int a , b , c ;
			cin>>a>>b>>c;
			if(ma[a][b]>c)
			{
				ma[a][b]=ma[b][a]=c;
			}
		}
		dijk();
	}
}



Kruskal
AC代码:
#include <bits/stdc++.h>
#define N 2000
#define MAX 10010
using namespace std ;
int n , m , pre[N];
struct node {
	int from ;
	int to ;
	int val;
}ma[MAX];
int find(int x )
{
	if(pre[x]!=x)
	return pre[x]=find(pre[x]);
	return x ;
}
bool cmp(node a,  node b )
{
	return a.val<b.val;
}
void kru()
{
	int ans , count ;
	ans = count = 0 ;
	for(int i = 0 ; i < m ; i++)
	{
		int u = find(ma[i].from);
		int v = find(ma[i].to);
		if(u!=v)
		{
			ans+=ma[i].val;
			pre[u]=v;  //*并查集特性 也可以 pre[v] = u ;  
			count++;
		}
		if(count == n - 1 )
			break;
	}
	if(count == n - 1 ) printf("%d\n\n",ans);
	else printf("impossible\n\n");
}
int main()
{
	while(cin>>n>>m)
	{
		memset(ma,0,sizeof(ma));
		for(int i = 0 ; i <=n;i++)
		{
			pre[i]=i;			
		}			
		for(int i= 0 ; i < m ; i++)
		{
			cin>>ma[i].from>>ma[i].to>>ma[i].val;
		}
		sort(ma,ma+m,cmp);
		kru();
    } 
    return 0 ;
}



你可能感兴趣的:(HDU 2122 Ice_cream’s world III)