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. 基于TensorFlow的车牌号识别系统

    简介 过去几周我一直在涉足深度学习领域,尤其是卷积神经网络模型.最近,谷歌围绕街景多位数字识别技术发布了一篇不错的paper.该文章描述了一个用于提取街景门牌号的单个端到端神经网络系统.然后,作者阐述 ...

  2. 删除过期备份报错RMAN-06207 RMAN-06208解决方案

    RMAN备份日志中出现了警告 日志文件目录如下: [root@erpdbs rmanback]# ll total 88 -rw-r--r-- 1 oraprod dba 81011 Sep 7 22 ...

  3. [Windows Server 2012] PHPWind安全设置

    ★ 欢迎来到[护卫神·V课堂],网站地址:http://v.huweishen.com★[护卫神·V课堂]是护卫神旗下专业提供服务器教学视频的网站,每周更新视频. ★ 本节我们将带领大家:PHPWin ...

  4. [Windows Server 2012] 手工破解MySQL密码

    ★ 欢迎来到[护卫神·V课堂],网站地址:http://v.huweishen.com★ 护卫神·V课堂 是护卫神旗下专业提供服务器教学视频的网站,每周更新视频.★ 本节我们将带领大家:破解MySQL ...

  5. 【sqli-labs】 less58 GET -Challenge -Double Query -5 queries allowed -Variation1 (GET型 挑战 双查询 只允许5次查询 变化1)

    单引号闭合成功,但是union select结果不对 http://192.168.136.128/sqli-labs-master/Less-58/?id=0' union select 1,2,3 ...

  6. C# SqlParameter 使用

    //System.Data.SqlClient.SqlParameter[] sqlParameters = new System.Data.SqlClient.SqlParameter[]{ };  ...

  7. 内网jenkins如何配置gitlab自动拉取代码打包

    在全局工具配置中添加git安装目录的配置 http://10.2.1.92:8080/jenkins/configureTools/git1.8.3.1/usr/bin/git 打开系统设置配置git ...

  8. 【剑指Offer】65、矩阵中的路径

      题目描述:   请设计一个函数,用来判断在一个矩阵中是否存在一条包含某字符串所有字符的路径.路径可以从矩阵中的任意一个格子开始,每一步可以在矩阵中向左,向右,向上,向下移动一个格子.如果一条路径经 ...

  9. 00.模块1.模块(Module)和包(Package)

    转自廖雪峰老师官方网站 在计算机程序的开发过程中,随着程序代码越写越多,在一个文件里代码就会越来越长,越来越不容易维护. 为了编写可维护的代码,我们把很多函数分组,分别放到不同的文件里,这样,每个文件 ...

  10. 解决git pull每次提示输入账号密码的问题

    每次用git同步代码的时候,都会提示输入账号密码,很麻烦,费时间,所以找了一种可以免去每次都要输入账号密码的方法 1. git bash进入你的项目目录 2. 输入以下命令会在配置文件里添加信息,作用 ...