24h購物| | PChome| 登入
2012-01-29 10:40:30| 人氣1,454| 回應0 | 上一篇 | 下一篇

[UVA] 11631 - Dark roads [Kruskal]

推薦 0 收藏 0 轉貼0 訂閱站台

Problem D: Dark roads

Economic times these days are tough, even in Byteland. To reduce the operating costs, the government of Byteland has decided to optimize the road lighting. Till now every road was illuminated all night long, which costs 1 Bytelandian Dollar per meter and day. To save money, they decided to no longer illuminate every road, but to switch off the road lighting of some streets. To make sure that the inhabitants of Byteland still feel safe, they want to optimize the lighting in such a way, that after darkening some streets at night, there will still be at least one illuminated path from every junction in Byteland to every other junction.

What is the maximum daily amount of money the government of Byteland can save, without making their inhabitants feel unsafe?

Input Specification

The input file contains several test cases. Each test case starts with two numbers m and n, the number of junctions in Byteland and the number of roads in Byteland, respectively. Input is terminated by m=n=0. Otherwise, 1 ≤ m ≤ 200000 and m-1 ≤ n ≤ 200000. Then follow n integer triples x, y, z specifying that there will be a bidirectional road between x and y with length z meters (0 ≤ x, y < m and x ≠ y). The graph specified by each test case is connected. The total length of all roads in each test case is less than 231.

Output Specification

For each test case print one line containing the maximum daily amount the government can save.

Sample Input

7 11
0 1 7
0 3 5
1 2 8
1 3 9
1 4 7
2 4 5
3 4 15
3 5 6
4 5 8
4 6 9
5 6 11
0 0

Sample Output

51




做法 : 全部成本 減去 最小生成樹成本

#include <stdio.h>
#include <stdlib.h>
#define maxN 400000
typedef struct {
    int x, y, v;
}Link;
Link D[maxN];
int Parent[maxN], Rank[maxN];
void MakeInit(int n) {
    int i;
    for(i = 0; i <= n; i++)
        Parent[i] = i, Rank[i] = 1;
}
int FindParent(int x) {
    if(Parent[x] != x)
        Parent[x] = FindParent(Parent[x]);
    return Parent[x];
}
void PLink(int x, int y) {
    if(Rank[x] > Rank[y]) {
        Rank[x] += Rank[y];
        Parent[y] = x;
    } else {
        Rank[y] += Rank[x];
        Parent[x] = y;
    }
}
int Union(int x, int y) {
    x = FindParent(x), y = FindParent(y);
    if(x != y) {
        PLink(x, y);
        return 1;
    }
    return 0;
}
int cmp(const void *i, const void *j) {
    Link *a, *b;
    a = (Link *)i, b = (Link *)j;
    return a->v - b->v;
}
int main() {
    int N, M;
    while(scanf("%d %d", &N, &M) == 2) {
        if(N == 0 && M == 0)
            break;
        int sum = 0, i, test = 0, flag;
        for(i = 0; i < M; i++) {
            scanf("%d %d %d", &D[i].x, &D[i].y, &D[i].v);
            D[M+i].x = D[i].y;
            D[M+i].y = D[i].x;
            D[M+i].v = D[i].v;
            sum += D[i].v;
        }
        M = M*2;
        qsort(D, M, sizeof(Link), cmp);
        MakeInit(N);
        for(i = 0; i < M; i++) {
            flag = Union(D[i].x, D[i].y);
            sum -= flag * D[i].v;
            test += flag;
            if(test == N-1)        break;
        }
        printf("%d\n", sum);
    }
    return 0;
}

台長: Morris
人氣(1,454) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA] 10943 - How do you add?
此分類上一篇:[UVA] 908 - Re-connecting Computer Sites

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文