Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2418481
  • 博文数量: 392
  • 博客积分: 7040
  • 博客等级: 少将
  • 技术积分: 4138
  • 用 户 组: 普通用户
  • 注册时间: 2009-06-17 13:03
个人简介

范德萨发而为

文章分类

全部博文(392)

文章存档

2017年(5)

2016年(19)

2015年(34)

2014年(14)

2013年(47)

2012年(40)

2011年(51)

2010年(137)

2009年(45)

分类:

2010-03-19 10:23:26

Dark roads
Submit: 146   Accepted:45
Time Limit: 3000MS  Memory Limit: 65536K
Description
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
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
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>

#define SET_SIZE 200010

typedef struct _edge
{
    int length;
    int beg;
    int end;
}ST_EDGE;
ST_EDGE edges[SET_SIZE];

int m, n, parent[SET_SIZE];

int comp(const void *a, const void *b)
{
    ST_EDGE *pa = (ST_EDGE *)a;
    ST_EDGE *pb = (ST_EDGE *)b;
    return pa->length - pb->length;
}

void make_set()
{
    int i;

    /* make it point to itself */
    for (i=0 ; i<m ; i++)
        parent[i] = i;
}

/*注意这个函数,在查找集合的时候实现路径压缩*/
int find_set(int x)
{
    if (x != parent[x])
        parent[x] = find_set(parent[x]);
    return parent[x];
}

void union_set(int x, int y)
{
    parent[find_set(y)] = find_set(x);
}

int kruskal()
{
    int i, cnt_edge, ret1, ret2, sum;

    qsort(edges, n, sizeof(ST_EDGE), comp);
    make_set();

    cnt_edge = sum = 0;
    for (i=0 ; i<n ; i++)
    {
        ret1 = find_set(edges[i].beg);
        ret2 = find_set(edges[i].end);
        /* the same set */
        if (ret1 == ret2)
            continue;
        union_set(edges[i].beg, edges[i].end);
        sum += edges[i].length;
        cnt_edge++;
        if (m - 1 == cnt_edge)
            break;
    }

    return sum;
}

int main(int argc, char *argv[])
{
    int i, sum, mst_len;

    while (1)
    {
        scanf("%d %d", &m, &n);
        if (0 == m && 0 == n)
            break;

        sum = 0;
        for (i=0 ; i<n ; i++)
        {
            scanf("%d %d %d", &edges[i].beg, &edges[i].end, &edges[i].length);
            sum += edges[i].length;
        }

        mst_len = kruskal();
        printf("%d\n", sum - mst_len);
    }
}


阅读(933) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~