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. 重现apache commons fileupload DOS漏洞

    这个漏洞是2014年2月4日被发现的, 因为该组件试用范围非常广, 所以该漏洞的影响也非常巨大.通过特制的包含畸形header的http请求,可以导致使用该组件的应用程序进入无限循环从而耗尽CPU等资 ...

  2. JS高级——instanceof语法

    基本语法 对象 instanceof 构造函数 基本使用 <script> function Person() { } //p--->Person.prototype--->O ...

  3. 快速学习mybatis框架

    一.介绍Mybatis(主要从以下两点进行介绍) 1.MyBatis是一个优秀的持久层框架,它对jdbc的操作数据库的过程进行封装,使开发者只需要关注 SQL 本身,而不需要花费精力去处理例如注册驱动 ...

  4. 【译】x86程序员手册00 - 翻译起因

    从上一次学习MIT的操作系统课程又过去了一年.上次学习并没有坚持下去.想来虽有种种原因,其还在自身无法坚持罢了.故此次再鼓起勇气重新学习,发现课程都已由2014改版为2016了.但大部分内容并没有改变 ...

  5. ELK基本统计图表

    ELK基本统计图表,现在基本在用自带的功能,复杂的功能还需要去摸索了

  6. @viewChild

    https://www.cnblogs.com/mttcug/p/8004359.html

  7. linux下如何限制普通用户更改密码

    问题描述: 为了方便linux管理员对所有用户的进行管理,如何限制普通用户更改密码? 解决方法: 禁止普通用户更改密码: /usr/bin/passwd 若要允许普通用户更改密码: /usr/bin/ ...

  8. C# MVC ajax上传 文件

    用普通的ajax提交表单的时候,不能把文件流传到后端去,所以要用到jquery.form.js jquery.form.js到官网下载或者从这里下载:http://pan.baidu.com/s/1c ...

  9. Oracle 【基 本 操 作】

    1.日期时间 select SYSDATE from DUAl; select TO_CHAR(SYSDATE, 'YYYY-MM-DD') from DUAL; select TO_CHAR(SYS ...

  10. office 2016最新安装及激活教程(KMS)【亲测有效】!!

    前言 博主的一个朋友,咳咳--你们懂得,想装office,于是我就上网找了一下激活的方法,亲测有效,而且也没有什么广告病毒之类的,还比较方便,所以传上来方便大家.好了,进入正题: 安装office 首 ...