您的位置:首页 > 其它

hdu 1102(prime 算法)

2011-07-29 19:32 330 查看
#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]
=mat
[m]=0;
		}
		prime();
		cout<<road<<endl;
	}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: