#include <iostream>
#include <cstring>
using namespace std;
#define maxn 100+10
#define max 100000
int graph[maxn][maxn];
bool visited[maxn];
int dis[maxn];
int nodes;
int prime(int start){
memset(visited,false,sizeof(visited));
for(int i=1;i<=nodes;i++)
dis[i] = max;
dis[1] = 0;
int ans = 0;
for(int i=1;i<=nodes;i++){
int k,temp = 1<<20;
for(int j=1;j<=nodes;j++){
if(!visited[j] && dis[j] < temp){
temp = dis[j];
k = j;
}
}
ans += dis[k];
visited[k] = true;
for(int j=1;j<=nodes;j++)
if(!visited[j] && dis[j] > graph[k][j])
dis[j] = graph[k][j];
}
return ans;
}
int main()
{
while(cin>>nodes){
for(int i=1;i<=nodes;i++)
for(int j=1;j<=nodes;j++){
cin>>graph[i][j];
graph[j][i] = graph[i][j];
}
cout<<prime(1)<<endl;
}
return 0;
}
poj 1258 Agri-Nets
最新推荐文章于 2020-03-06 15:47:33 发布