题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1233
裸模板题,我都敲了好久。。。
/*
* @Author: Samson
* @Date: 2018-06-14 16:12:07
* @Last Modified by: Samson
* @Last Modified time: 2018-06-14 17:38:25
*/
// @URL : http://acm.hdu.edu.cn/showproblem.php?pid=1233
#include<bits/stdc++.h>
#include<algorithm>
#include <cstdlib>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long LL;
const int MAXN = 1e2+10;
int n,p[MAXN],d[MAXN],color[MAXN],a[MAXN][MAXN];
int prim()
{
memset(color,0,sizeof color);
memset(d,INF,sizeof d);
memset(p,-1,sizeof p);
d[1] = 0;
int u,minv,cnt = 0;
while(1)
{
u = -1,minv = INF;
for(int i = 1; i <= n; ++i)
{
if(d[i] < minv && !color[i])
minv = d[i],u = i;
}
if(u == -1) break;
color[u] = 1;
for(int v = 1; v <= n; ++v)
{
if(!color[v] && a[u][v] != INF)
{
if(d[v] > a[u][v])
d[v] = a[u][v], p[v] = u;
}
}
}
int sum = 0;
for(int i = 1; i <= n; ++i)
if(p[i] != -1)
sum += a[p[i]][i];
return sum;
}
int main(void)
{
ios::sync_with_stdio(false);
//cin.tie(0);
int u,v,w;
while(cin>>n,n)
{
memset(a,INF,sizeof a);
for(int i = 1; i <= n*(n-1)/2; ++i)
{
cin>>u>>v>>w;
a[u][v] = a[v][u] = w;
}
cout<<prim()<<'\n';
}
return 0;
}