题目传送门
/*
此题为:HDOJ 1233 + HDOJ 1232
*/
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
const int MAX_N = + ;
int pre[MAX_N];
int tot;
int sum;
struct NODE
{
int x, y;
int len;
}node[MAX_N];
int find(int root)
{
int son, tmp;
son = root;
while (root != pre[root])
{
root = pre[root];
}
while (son != root)
{
tmp = pre[son];
pre[son] = root;
son = tmp;
}
return root;
}
bool cmp(NODE a, NODE b)
{
return a.len < b.len;
}
int main(void) //HDOJ 1863 畅通工程
{
//freopen ("inC.txt", "r", stdin);
int n, m; //n路, m村庄
while (~scanf ("%d%d", &n, &m) && n)
{
tot = m - ;
for (int i=; i<=m; ++i)
{
pre[i] = i;
}
for (int i=; i<=n; ++i)
{
scanf ("%d%d%d", &node[i].x, &node[i].y, &node[i].len);
}
sort (node+, node++n, cmp);
sum = ;
for (int i=; i<=n; ++i)
{
int a, b;
a = find (node[i].x);
b = find (node[i].y);
if (a != b)
{
pre[a] = b;
sum += node[i].len;
--tot;
}
}
if (tot == )
printf ("%d\n", sum);
else
puts ("?");
}
}