Dark roads(MST模板)
2015-10-13 10:59
204 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2988
题目大意:个m个点n条边求mst
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
Sample Output
题目大意:个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 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 <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; }
相关文章推荐
- C#使用PrintDocument打印 多页 打印预览
- css sprint 生成工具 bg2css
- Laravel5 cookie和session设置
- 图文详解远程部署ASP.NET MVC 5项目 [转载]
- JavaScript中创建对象的几种方式
- Scala基本知识点
- Xcode6中创建分类、协议等文件的方法
- Android_数据存储——SharedPreferences .
- NSFileManager Class Reference
- Yarn HA解决方案
- spark1.5 scala.collection.mutable.WrappedArray$ofRef cannot be cast to ...解决办法
- spark1.5 scala.collection.mutable.WrappedArray$ofRef cannot be cast to ...解决办法
- spark1.5 scala.collection.mutable.WrappedArray$ofRef cannot be cast to ...解决办法
- Android编译系统
- spark1.5 scala.collection.mutable.WrappedArray$ofRef cannot be cast to ...解决办法
- js事件封装(已兼容)
- MFC网络编程
- php curl流方式远程下载大文件
- oracle 第06章 数据字典
- Excel中的“定时器”