Dark roads

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

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
所以边的权值-最小生成树的权值
裸题

 #include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<queue>
#include<map>
#include<set>
#include<vector>
#include<cstdlib>
#include<string>
#define eps 0.000000001
typedef long long ll;
typedef unsigned long long LL;
using namespace std;
const int N=+;
int parent[N];
int ans;
int m,n;
struct node{
int u,v,w;
}a[N];
bool cmp(node x,node y){
return x.w<y.w;
}
void init(){
for(int i=;i<=N;i++)parent[i]=i;
}
int find(int x){
int r=x;
while(parent[r]!=r)r=parent[r];
int i=x;
int j;
while(i!=r){
j=parent[i];
parent[i]=r;
i=j;
}
return r;
}
void kruskal(){
for(int i=;i<m;i++){
int x=find(a[i].u);
int y=find(a[i].v);
if(x!=y){
parent[x]=y;
ans=ans+a[i].w;
}
}
}
int main(){
while(scanf("%d%d",&n,&m)!=EOF){
init();
if(m==&&n==)break;
int sum=;
ans=;
for(int i=;i<m;i++){
scanf("%d%d%d",&a[i].u,&a[i].v,&a[i].w);
sum=sum+a[i].w;
}
//cout<<sum<<endl;
sort(a,a+m,cmp);
kruskal(); //cout<<ans<<endl;
printf("%d\n",sum-ans);
}
}

hdu 2988(最小生成树 kruskal算法)的更多相关文章

  1. 【转】最小生成树——Kruskal算法

    [转]最小生成树--Kruskal算法 标签(空格分隔): 算法 本文是转载,原文在最小生成树-Prim算法和Kruskal算法,因为复试的时候只用到Kruskal算法即可,故这里不再涉及Prim算法 ...

  2. 最小生成树——kruskal算法

    kruskal和prim都是解决最小生成树问题,都是选取最小边,但kruskal是通过对所有边按从小到大的顺序排过一次序之后,配合并查集实现的.我们取出一条边,判断如果它的始点和终点属于同一棵树,那么 ...

  3. 最小生成树Kruskal算法

    Kruskal算法就是把图中的所有边权值排序,然后从最小的边权值开始查找,连接图中的点,当该边的权值较小,但是连接在途中后会形成回路时就舍弃该边,寻找下一边,以此类推,假设有n个点,则只需要查找n-1 ...

  4. 最小生成树------Kruskal算法

    Kruskal最小生成树算法的概略描述:1 T=Φ:2 while(T的边少于n-1条) {3 从E中选取一条最小成本的边(v,w):4 从E中删去(v,w):5 if((v,w)在T中不生成环) { ...

  5. 求最小生成树——Kruskal算法

    给定一个带权值的无向图,要求权值之和最小的生成树,常用的算法有Kruskal算法和Prim算法.这篇文章先介绍Kruskal算法. Kruskal算法的基本思想:先将所有边按权值从小到大排序,然后按顺 ...

  6. 最小生成树 kruskal算法&prim算法

    (先更新到这,后面有时间再补,嘤嘤嘤) 今天给大家简单的讲一下最小生成树的问题吧!(ps:本人目前还比较菜,所以最小生成树最后的结果只能输出最小的权值,不能打印最小生成树的路径) 本Tianc在刚学的 ...

  7. 算法实践--最小生成树(Kruskal算法)

    什么是最小生成树(Minimum Spanning Tree) 每两个端点之间的边都有一个权重值,最小生成树是这些边的一个子集.这些边可以将所有端点连到一起,且总的权重最小 下图所示的例子,最小生成树 ...

  8. 模板——最小生成树kruskal算法+并查集数据结构

    并查集:找祖先并更新,注意路径压缩,不然会时间复杂度巨大导致出错/超时 合并:(我的祖先是的你的祖先的父亲) 找父亲:(初始化祖先是自己的,自己就是祖先) 查询:(我们是不是同一祖先) 路径压缩:(每 ...

  9. 数据结构之最小生成树Kruskal算法

    1. 克鲁斯卡算法介绍 克鲁斯卡尔(Kruskal)算法,是用来求加权连通图的最小生成树的算法. 基本思想:按照权值从小到大的顺序选择n-1条边,并保证这n-1条边不构成回路. 具体做法:首先构造一个 ...

随机推荐

  1. [Android]异常5-throwable:java.lang.OutOfMemoryError: pthread_create

    背景:线程初始化耗时任务 异常原因: 可能一>多个new Thread()嵌套 解决办法有: 解决一>使用Handler分离new Thread()嵌套 注: 06-30 09:12:26 ...

  2. 【PostgreSQL-9.6.3】临时表

    PostgreSQL中的临时表分两种,一种是会话级临时表,一种是事务级临时表.在会话级临时表中,数据可以存在于整个会话的生命周期中,在事务级临时表中的数据只能存在于事务的生命周期中.1. 会话级临时表 ...

  3. CSS——宠物demo

    注意:ul中自带padding值,需要清除. <!DOCTYPE html> <html lang="en"> <head> <meta ...

  4. 将MongoDB服务器设置成Windows启动服务(win10)

    如题,这个问题也百度了很久,百度还是挺给力的,但是都没能解决问题,后来在大神(原谅我不知道大神叫什么)的指导下,终于设置成功,特分享下设置过程.. MongoDB设置数据库我就不说了...额..算了, ...

  5. React-Native WebView动态加载字体

    背景 使用react-native构建的iOS/Android双端APP,通过WebView加载本地页面,需要根据服务器提供的字体列表实现下载和动态加载. 本地字体检查 有些字体手机操作系统已经提供了 ...

  6. JAVA趣味逻辑算法

    /**已知4位同学中的一位数学考了100分,当小李询问这4位是谁考了100分时,4个人的回答如下: A说:不是我. B说:是C C说:是D. D说:他胡说. 已知三个人说的是真话,一个人说的是假话.现 ...

  7. codeforces_731D_(前缀和)(树状数组)

    D. 80-th Level Archeology time limit per test 2 seconds memory limit per test 256 megabytes input st ...

  8. 发布自己的nuget包

    1.先到www.nuget.org注册账户,然后在用户中心获取apikey 2.到https://dist.nuget.org/index.html下载最新的nuget.exe,放到你的项目根目录下 ...

  9. 取三级分销上下级用户id

    //取上三级的用户idpublic function _get_up_third_id($member_id){ $up_id=array(); $invite_id=dbselect('invite ...

  10. modelsim 仿真软件 百度云分享 modelsim se 10.7 10.6d 10.6c 10.5 10.4

    modelsim se 10.7 链接:https://pan.baidu.com/s/1NDC2yMCZmA4bIRSk2dUiTg 提取码:4l1d 复制这段内容后打开百度网盘手机App,操作更方 ...