Dark roads(MST模板)

来源:互联网 发布:程序员专用昵称 编辑:程序博客网 时间:2024/05/21 22:58

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2988

题目大意:个m个点n条边求mst

Dark roads

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 858    Accepted Submission(s): 356


Problem 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 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
 

#include <cstdio>#include <algorithm>#include <cstring>using namespace std;struct node{    int u,v,w;    node(int uu,int vv,int ww):u(uu),v(vv),w(ww){}    node(){}    friend bool operator <(const node &a,const node &b){        return a.w<b.w;    }};int n,m;node  edge[200010];bool used[200010];int parent[200010];int getroot(int t){    if(parent[t]==t)return t;    return parent[t]=getroot(parent[t]);}void Merge(int t,int w){    int pt=getroot(t);    int pw=getroot(w);    if(pt==pw)return;    parent[pt]=pw;}int main(){    while(scanf("%d%d",&m,&n)&&n+m){        memset(used,0,sizeof(used));        int sum=0;        for(int i=0;i<n;i++){            scanf("%d%d%d",&edge[i].u,&edge[i].v,&edge[i].w);            sum+=edge[i].w;        }        sort(edge,edge+n);        for(int i=0;i<n;i++)            parent[i]=i;        int cnt=0;        int ans=0;        for(int i=0;i<n;i++){            int u=edge[i].u;            int v=edge[i].v;            int w=edge[i].w;            if(getroot(u)!=getroot(v)){                Merge(u,v);                ans+=w;                cnt++;            }            if(cnt==m-1)break;        }        printf("%d\n",sum-ans);    }    return 0;}


0 0
原创粉丝点击