Lightoj 1123 - Trail Maintenance(最小增量生成树)

题目链接 https://vjudge.net/problem/LightOJ-1123

Tigers in the Sunderbans wish to travel freely among the N fields (numbered from 1 to N), even though they are separated by trees. The tigers wish to maintain trails between pairs of fields so that they can travel from any field to any other field using the maintained trails. Tigers may travel along a maintained trail in either direction.

The tigers do not build trails. Instead, they maintain deer trails that they have discovered. On any week, they can choose to maintain any or all of the deer animal trails they know about. Always curious, the tigers discover one new deer trail at the beginning of each week. They must then decide the set of trails to maintain for that week so that they can travel from any field to any other field. Tigers can only use trails which they are currently maintaining.

The tigers always want to minimize the total length of trail they must maintain. The tigers can choose to maintain any subset of the deer trails they know about, regardless of which trails were maintained the previous week. Deer trails (even when maintained) are never straight. Two trails that connect the same two fields might have different lengths. While two trails might cross, tigers are so focused; they refuse to switch trails except when they are in a field. At the beginning of each week, the tigers will describe the deer trail they discovered. Your program must then output the minimum total length of trail the tigers must maintain that week so that they can travel from any field to any other field, if there is such a set of trails.

Input
Input starts with an integer T (≤ 25), denoting the number of test cases.

Each case starts with two integers N (1 ≤ N ≤ 200) and W. W is the number of weeks the program will cover (1 ≤ W ≤ 6000).

Each of the next W lines will contain three integers describing the trail the tigers found that week. The first two numbers denote the end points (filed numbers) and the third number denotes the length of the trail (1 to 10000). No trail has the same field as both of its end points.

Output
For each case, print the case number in a line. Then for every week, output a single line with the minimum total length of trail the tigers must maintain so that they can travel from any field to any other field. If no set of trails allows the tigers to travel from any field to any other field, output “-1”.

Sample Input
1
4 6
1 2 10
1 3 8
3 2 3
1 4 3
1 3 6
2 1 2
Sample Output
Case 1:
-1
-1
-1
14
12
8

【题意】
从包含n个点的空图开始,依次加入m条带权无向边,每加入一条边就立即输出当前图中最小生成树的权值,如果还未连通则输出-1.

【思路】
题目描述即最小增量生成树的定义,如果在每次加边后都调用一次kruscal算法求解,那么复杂度是O(m^2logn),难以承受。正确的做法是根据生成树的回路性质删除一些边,回路性质即图中任意一条回路上边权最大的边一定不在最小生成树当中,所以我们还是仿照kruscal算法的过程,从空图开始不断加边,假设加入若干条边以后求出了当前的最小生成树,那么下一次加边后一定会构成一个环,我们只需要把这个环上边权最大的边删除就会得到新的最小生成树,怎么去找边权最大的那条边呢?其实根本不用找,因为在调用kruscal算法之前我们肯定会对边集按照边权升序排序的,当新加入的这条边构成环路时,这条边肯定是环路中权值最大的边,也是我们要删掉的边,因为每次操作最多删一条边,所以直接用最后一条边覆盖掉当前边即可,下一次调用前的sort会重新对边集排序。

#include<bits/stdc++.h>
using namespace std;

const int maxn = 220;
const int maxm = 6050;

int n, m, cnt;
int par[maxn];

struct Edge {
    int from, to, dist;
    Edge(int f = 0, int t = 0, int d = 0) :from(f), to(t), dist(d) {}
}edges[maxm];

bool cmp(Edge x, Edge y) { return x.dist < y.dist; }
int find(int x) { return par[x] == x ? x : par[x] = find(par[x]); }

int kruscal() {
    int cpy = cnt, num = 0, ans = 0;
    for (int i = 0; i <= n; ++i) par[i] = i;

    for (int i = 0; i < cpy; ++i) {
        int x = find(edges[i].from);
        int y = find(edges[i].to);
        if (x == y) {
            edges[i] = edges[cnt - 1];
            --cnt;
            continue;
        }
        ++num;
        par[x] = y;
        ans += edges[i].dist;
    }
    return num < n - 1 ? -1 : ans;
}

int main() {
    int t;
    scanf("%d", &t);
    for (int kase = 1; kase <= t; ++kase) {
        scanf("%d%d", &n, &m);
        printf("Case %d:\n", kase);
        cnt = 0;
        for (int i = 0; i < m; ++i) {
            int u, v, c;
            scanf("%d%d%d", &u, &v, &c);
            edges[cnt++] = Edge(u, v, c);
            sort(edges, edges + cnt, cmp);
            int ans = kruscal();
            printf("%d\n", ans);
        }
    }
    return 0;
}
posted @ 2018-02-21 12:37  不想吃WA的咸鱼  阅读(118)  评论(0编辑  收藏  举报