POJ 2421题
//解题思路:最小生成树Prim算法实现,注意将已修建的路的距离置为0
#include <stdio.h>
#include <string.h>
#define arraysize 101
int maxData = 0x7fffffff;
int dis[arraysize][arraysize];
bool final[arraysize];
int d[arraysize];
int N;
void Prime()
{
int sum = 0;
memset(final,0,sizeof(final));
for(int i=1;i<N+1;++i)
{
d[i] = dis[1][i];
}
final[1] = true;
d[1] = 0;
for(int i2=1;i2<N;++i2)
{
int min = maxData;
int v = 1;
for(int i3=1;i3<N+1;++i3)
{
if(!final[i3] && d[i3]<min)
{
min = d[i3];
v = i3;
}
}
sum += min;
final[v] = true;
for(int i4=1;i4<N+1;++i4)
{
if(!final[i4] && d[i4]>dis[v][i4])
d[i4] = dis[v][i4];
}
}
printf("%d\n",sum);
}
int main()
{
//freopen("1.txt","r",stdin);
while(scanf("%d",&N)!=EOF)
{
for(int i=1;i<N+1;++i)
{
for(int i1=1;i1<N+1;++i1)
{
scanf("%d",&dis[i][i1]);
}
}
int Q;
scanf("%d",&Q);
int start,end;
for(int i1=1;i1<Q+1;++i1)
{
scanf("%d%d",&start,&end);
dis[start][end] = 0;
dis[end][start] = 0;
}
Prime();
}
return 0;
}