标签:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 100 + 10 ;
int g[maxn][maxn],vis[maxn],lowc[maxn];
int prim(int n){
int ans = 0;
memset(vis,0,sizeof(vis));
vis[0] = 1;
for(int i = 1 ; i < n ; ++i) lowc[i] = g[0][i];
for(int i = 1 ; i < n ;++i){
int minc = 0x3f3f3f3f;
int p = -1;
for(int j = 0 ; j < n ; ++j){
if(!vis[j] && minc > lowc[j]){
minc = lowc[j];
p = j;
}
}
if(minc == 0x3f3f3f3f) return -1;
ans += minc;
vis[p] = 1;
for(int j = 0 ; j < n ; ++j){
if(!vis[j] && lowc[j] > g[p][j]){
lowc[j] = g[p][j];
}
}
}
return ans;
}
int main(){
int n,a;
while(~scanf("%d",&n)){
memset(g,0x3f,sizeof(g));
for(int i = 0 ; i < n ; ++i){
for(int j = 0 ; j < n ; ++j){
scanf("%d",&a);
g[i][j] = min(g[i][j],a);
}
}
printf("%d\n",prim(n));
}
return 0;
}
[2016-04-14][POJ][1258][Agri-Net]
标签:
原文地址:http://www.cnblogs.com/qhy285571052/p/c0d0b2c96fa8a573aebd753b43e819be.html