http://acm.hdu.edu.cn/showproblem.php?pid=2988
Dark roads
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1304 Accepted Submission(s): 573
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
Source
Recommend
lcy | We have carefully selected several similar problems for you: 2985 2986 2992 2991 2990
思路
题目大意:给你N个点M条路的图,点的变化由0~N-1,求最小生成树。
思路:裸的Kruskal算法,直接求就可以。
AC Code
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
const int nmax=200000+10;
const int mmax=200000+10;
int n,m;//点数、边数
struct Edge{
int u,v;
ll val;//起点、终点、边权
}edge[mmax];
bool cmp(Edge a,Edge b){
return a.val<b.val; //按照边权从小到大排序,求最小生成树
}
int father[nmax];
int findFather(int u){
if(u==father[u]) return u;
else{
int f=findFather(father[u]);
father[u]=f;
return f;
}
}
void init(int n){
for(int i=0;i<n;i++){
father[i]=i;
}
}
ll Kruskal(){//返回最小生成树的边权和
init(n);
//sort(edge,edge+m,cmp);
int cnt=0;//有效合并次数
ll ans=0;//最小边权和
for(int i=0;i<m;i++){//遍历m条边
int fu=findFather(edge[i].u);
int fv=findFather(edge[i].v);
if(fu!=fv){
father[fu]=fv;
ans+=edge[i].val;
cnt++;
}
if(cnt==n-1)//合并了N-1条边,已经找到了最小生成树
break;
}
if(cnt==n-1)//找到最小生成树
return ans;
else //图不连通
return -1;
}
int main(int argc, char** argv) {
while(scanf("%d %d",&n,&m)!=EOF){
if(n==0&&m==0){
break;
}
memset(father,0,sizeof(father));
memset(edge,0,sizeof(edge));
init(n);
ll sum=0;//sum是图中所有边的边权
for(int i=0;i<m;i++){
scanf("%d %d %d",&edge[i].u,&edge[i].v,&edge[i].val);
sum+=edge[i].val;
}
sort(edge,edge+m,cmp);
ll ans=Kruskal();//最小生成树的边权和
//可以省的最大金额为:sum-ans
printf("%lld\n",sum-ans);
}
return 0;
}