poj 1251 最小生成树

来源:互联网 发布:美国cpi数据 编辑:程序博客网 时间:2024/06/05 11:29

题意:用桥把n个岛屿连接起来


直接最小生成树就行

#include <iostream>#include <cstring>#include <cstdio>#include <algorithm>#include <vector>#define rep(i, j, k) for(int i = j; i <= k; i++)#define maxn 200009using namespace std;int n, m, tt, father[1009];struct cadongllas{int x, y, value;}t[100];bool cmp (cadongllas x, cadongllas y){return x.value < y.value;}int get_father (int x){if (father[x] == x)return x;return get_father (father[x]);}int main (){while (scanf ("%d", &n) == 1 && n){int ans = 0, cnt = 0;rep (i, 1, n - 1){char s[10];scanf ("%s", s);scanf ("%d", &m);rep (j, 1, m)scanf ("%s%d", s, &tt), t[++cnt] = (cadongllas){i, s[0] - 'A' + 1, tt};// printf ("add %d %d --%d\n", i, s[0] - 'A' + 1, tt);}sort (t + 1, t + 1 + cnt, cmp);rep (i, 1, n)father[i] = i;rep (i, 1, cnt){int u = get_father (t[i].x), v = get_father (t[i].y);if (u != v)father[u] = v, ans += t[i].value;}cout << ans << endl;}return 0;}


0 0