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. centos 设置 ip地址

    linux设置ip,主要是修改/etc/sysconfig/network-scripts/ifcfg-** 里面的网卡配置文件,然后命令 service network restart 生效 自动获 ...

  2. Linux内核系统调用处理过程

    原创作品转载请注明出处 + https://github.com/mengning/linuxkernel/ 学号末三位:168 下载并编译Linux5.0 xz -d linux-.tar.xz . ...

  3. oracle的递归运算(树运算) 无限树形

    oracle的递归运算(树运算)start with org_id ='1'connect by prior parent_id=son_id 1.前言   oracle的递归运算,在我们web页面的 ...

  4. hdu 1584 蜘蛛纸牌

    把小的牌放到大的牌上,求最小移动的距离和 DFS遍历所有的可能,把每一张牌与之要移动的牌都进行两层for的循环,注意回溯条件满足立刻break 代码(算法借鉴) #include <bits/s ...

  5. 第一节:numpy之ndarray对象数据类型及属性

  6. 8 pandas模块,多层索引

      1 创建多层索引     1)隐式构造         最常见的方法是给DataFrame构造函数的index参数传递两个或更多的数组           · Series也可以创建多层索引    ...

  7. java常见知识

    在JSP页面获取当前项目名称的方法: 方法1: <%= this.getServletContext().getContextPath() %> 方法2: 使用EL表达式 ${pageCo ...

  8. 【IntelliJ IDEA】idea上安装Translation插件后,需要AppKey才能生效的解决方案

    使用idea安装的翻译插件translation,但是使用的时候并不友好 无奈,如果想使用翻译软件并且更方便的话,可以如下: 可以选择将translation进行卸载 清除缓存并进行重启 然后再启动之 ...

  9. BUPT2017 wintertraining(15) #9

    下面不再说明题意了请自行读题,直接放contest链接. https://vjudge.net/contest/151607 A.考虑当火车隔k站一停时 区间长度 >= k 的纪念品一定能买到 ...

  10. [bzoj2502]清理雪道[上下界网络流]

    bzoj状态里有两种,一种时间是个位数,一种是四位数,我就是四位数的那种,,,估计都是看了hzwer.. #include <bits/stdc++.h> #define INF 0x3f ...