hdu 1102(prime 算法)

#include <iostream>
#include<cstdio>
#include <queue>
#include<string.h>
using namespace std;
const int MAX=99999;
int mat[105][105];
int N,lowcost[105],road;
void prime()
{
	for(int i=2;i<=N;i++)
		lowcost[i]=mat[1][i];
	for(int i=2;i<=N;i++)
	{
		int temp=lowcost[i],k=i;
		for(int j=2;j<=N;j++)
			if(temp>lowcost[j])
			{
				temp=lowcost[j];
				k=j;
			}
			road+=temp;
			lowcost[k]=MAX;
			for(int j=2;j<=N;j++)
				if(mat[k][j]<lowcost[j]&&lowcost[j]<MAX)
					lowcost[j]=mat[k][j];
	}

}
void makeSet(int n)
{
	for(int i=1;i<=n;i++)
		for(int j=1;j<=n;j++)
			cin>>mat[i][j];
}
int main()
{
	int Q,m,n;
	while(cin>>N)
	{
		road=0;
		makeSet(N);
		cin>>Q;
		while(Q--)
		{
			cin>>m>>n;
			mat[m][n]=mat[n][m]=0;
		}
		prime();
		cout<<road<<endl;
	}
}


posted on 2011-07-29 19:32  ljfbest  阅读(87)  评论(0)    收藏  举报

导航