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?
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
- Dark roads(MST模板)
- SOJ 3427 Dark roads
- 3427: Dark roads
- Dark roads hdu2988 Kruskal
- Uva 11631 - Dark roads
- Uva-11631- Dark roads
- hdu 2988 Dark roads
- uva11631 - Dark roads
- UVa 11631 - Dark roads
- HDU2988 Dark roads【Kruskal】
- Sicily 7766. Dark roads
- Dark roads--hdoj
- Dark roads 2988
- hdu 2988 Dark roads
- hdu2988 Dark roads--Kruskal
- 杭电 2988 Dark roads
- HDU1102Constructing Roads(MST)
- Eclipse 的 Dark 模板
- centos6上redis搭建配置
- Android 学习笔记七:使用Fragment创建动态UI
- Nim Game
- [165]Compare Version Numbers
- loaded the "XXXviewController" nib but the view outlet was not set
- Dark roads(MST模板)
- flot 绘图文档API
- 小bug?
- 如何配置oralce数据库的用户1权限
- C# VS2012下的3D显示(一)
- Android中级篇之百度地图SDK v3.5.0-一步一步带你仿各大主流APP地图定位移动选址功能
- SqlServer 数据库 分离复制备份然后附加回数据库 sql代码
- Socket连接与HTTP连接
- html中,怎么样才能获得iframe页面里的内容