Sicily 7766. Dark roads

来源:互联网 发布:华为matebook x知乎 编辑:程序博客网 时间:2024/06/05 04:26

7766. Dark roads

Constraints

Time Limit: 2 secs, Memory Limit: 256 MB

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 mand 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 110 1 70 3 51 2 81 3 91 4 72 4 53 4 153 5 64 5 84 6 95 6 110 0

Sample Output

51

Problem Source

2013年每周一赛第三场暨校赛模拟赛I

// Problem#: 7766// Submission#: 3380544// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/// All Copyright reserved by Informatic Lab of Sun Yat-sen University#include <stdio.h>#include <iostream>#include <vector>#include <string>#include <stack>#include <iomanip>#include <algorithm>#include <queue>#include <functional>#include <map>#include <string.h>#include <math.h>using namespace std;const int MAX_N = 200005;const int MAX_E = 200005;struct edge {    int u, v, cost;};int N, E;int par[MAX_N];edge ED[MAX_E];inline bool cmp(const edge & e1, const edge & e2) {    return e1.cost < e2.cost;}void init() {    for (int i = 0; i < N; i++) par[i] = i;}inline int find(int x) {    if (par[x] == x) return x;    else return par[x] = find(par[x]);}inline void unite(int x, int y) {    x = find(x);    y = find(y);    if (x == y) return;    else par[x] = y;}int KRUSKAL() {    sort(ED, ED + E, cmp);    init();    int ans = 0;    for (int i = 0; i < E; i++) {        if (find(ED[i].u) != find(ED[i].v)) {            unite(ED[i].u, ED[i].v);            ans += ED[i].cost;        }    }    return ans;}int main() {    //std::ios::sync_with_stdio(false);    char text[50];    while (1) {        scanf("%d%d", &N, &E);        gets(text);        if (!N && !E) break;        int sum = 0, j;        for (int i = 0; i < E; i++) {            gets(text);            ED[i].u = ED[i].v = ED[i].cost = 0;            for (j = 0; text[j] != ' '; j++) ED[i].u = ED[i].u * 10 + text[j] - '0';            j++;            for (; text[j] != ' '; j++) ED[i].v = ED[i].v * 10 + text[j] - '0';            j++;            for (; text[j] != '\0'; j++) ED[i].cost = ED[i].cost * 10 + text[j] - '0';            sum += ED[i].cost;        }        cout << sum - KRUSKAL() << endl;    }    return 0;}                                 


0 0
原创粉丝点击