POJ2421 最小生成树/MST(prim)


题意:

给出各个村的距离,并给出已经部分有路连通的两个村,求加最少的路的让各个连通。


思路:


让连通的边的权值设为零,然后求最小生成树即可。



#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
using namespace std;
const int MAXN=110;
const int INF=100000000;
int dist[MAXN];
struct cedge
{
  int to;
  int w;
  cedge(int _to=0,double _w=INF)
  {
    to=_to;
    w=_w;
  }
}edge;
bool operator<(const cedge &e1,const cedge &e2)
{
  return e1.w>e2.w;
}
int MST(int n,int map[][110])
{
           int ans;
	   int sum=n;
	   int i;
	  	  	   int  visit[110];
  		ans=0;
	priority_queue<cedge> qu;
	memset(visit,0,sizeof(visit));
	qu.push(cedge(1,0));
	memset(dist,127,sizeof(dist));
	cedge current;
	while(sum--)
	  {
	    do
	      {
		current=qu.top();
		qu.pop();
		//	cout<<current.to<<"  "<<current.w<<endl;
		
	      }while(visit[current.to]==true&&!qu.empty());
	    visit[current.to]=true;
	    ans+=current.w;
	    for(i=0;i<n;i++)
	      {
		if(visit[i+1]==false)
		  {
		    if(map[current.to-1][i]<dist[i+1])
		      {
			dist[i+1]=map[current.to-1][i];
			//	cout<<"dist: "<<i+1<<"  "<<dist[i+1]<<endl;
			qu.push(cedge(i+1,dist[i+1]));
		      }
		    
		  }
	      }
	    
	  }
	return ans;
  
}
int main()
{
  int map[110][110];
    int n;
    int t;
    int i,j;
    int from;
    int to;

    while(scanf("%d",&n)!=EOF)
      {

	for(i=0;i<n;i++)
	  {
	    for(j=0;j<n;j++)
	      {
		scanf("%d",&map[i][j]);
	      }
	  }
	scanf("%d",&t);
	for(i=0;i<t;i++)
	  {
	    scanf("%d %d",&from,&to);
	    from--;
	    to--;
	    map[from][to]=0;
	    map[to][from]=0;
	    
	  }

	
	int ans=MST(n,map);
	
	printf("%d\n",ans);
      }
    
    return 0;
}






你可能感兴趣的:(struct,ini)