题目连接

http://acm.hdu.edu.cn/showproblem.php?pid=2988

Dark roads

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

最小生成树。。

#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
#include<set>
using std::set;
using std::sort;
using std::pair;
using std::swap;
using std::multiset;
using std::priority_queue;
#define pb(e) push_back(e)
#define sz(c) (int)(c).size()
#define mp(a, b) make_pair(a, b)
#define all(c) (c).begin(), (c).end()
#define iter(c) decltype((c).begin())
#define cls(arr, val) memset(arr, val, sizeof(arr))
#define cpresent(c, e) (find(all(c), (e)) != (c).end())
#define rep(i, n) for(int i = 0; i < (int)n; i++)
#define tr(c, i) for(iter(c) i = (c).begin(); i != (c).end(); ++i)
const int N = 200010;
const int INF = 0x3f3f3f3f;
typedef unsigned long long ull;
struct edge {
int u, v, w;
inline bool operator<(const edge &x) const {
return w < x.w;
}
}G[N];
struct Kruskal {
int E, sum, par[N], rank[N];
inline void init(int n) {
E = sum = 0;
rep(i, n + 1) {
par[i] = i, rank[i] = 0;
}
}
inline void built(int m) {
int u, v, w;
while (m--) {
scanf("%d %d %d", &u, &v, &w);
G[E++] = { u, v, w }, sum += w;
}
}
inline int find(int x) {
while (x != par[x]) {
x = par[x] = par[par[x]];
}
return x;
}
inline bool unite(int x, int y) {
x = find(x), y = find(y);
if (x == y) return false;
if (rank[x] < rank[y]) {
par[x] = y;
} else {
par[y] = x;
rank[x] += rank[x] == rank[y];
}
return true;
}
inline int kruskal() {
int ans = 0;
sort(G, G + E);
rep(i, E) {
edge &e = G[i];
if (unite(e.u, e.v)) {
ans += e.w;
}
}
return ans;
}
inline void solve(int n, int m) {
init(n), built(m);
printf("%d\n", sum - kruskal());
}
}go;
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w+", stdout);
#endif
int n, m;
while (~scanf("%d %d", &n, &m), n + m) {
go.solve(n, m);
}
return 0;
}

hdu 2988 Dark roads的更多相关文章

  1. HDU 2988 Dark roads(kruskal模板题)

    Dark roads Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  2. HDU 2988 Dark roads (裸的最小生成树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2988 解题报告:一个裸的最小生成树,没看题,只知道结果是用所有道路的总长度减去最小生成树的长度和. # ...

  3. HDU 2988.Dark roads-最小生成树(Kruskal)

    最小生成树: 中文名 最小生成树 外文名 Minimum Spanning Tree,MST 一个有 n 个结点的连通图的生成树是原图的极小连通子图,且包含原图中的所有 n 个结点,并且有保持图连通的 ...

  4. 【HDOJ】2988 Dark roads

    最小生成树. /* */ #include <iostream> #include <string> #include <map> #include <que ...

  5. Dark roads(kruskal)

    Dark roads Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Su ...

  6. HDU 1102 Constructing Roads, Prim+优先队列

    题目链接:HDU 1102 Constructing Roads Constructing Roads Problem Description There are N villages, which ...

  7. HDOJ(HDU).1025 Constructing Roads In JGShining's Kingdom (DP)

    HDOJ(HDU).1025 Constructing Roads In JGShining's Kingdom (DP) 点我挑战题目 题目分析 题目大意就是给出两两配对的poor city和ric ...

  8. hdu 2988(最小生成树 kruskal算法)

    Dark roads Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  9. HDU 1025 Constructing Roads In JGShining's Kingdom(求最长上升子序列nlogn算法)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1025 解题报告:先把输入按照r从小到大的顺序排个序,然后就转化成了求p的最长上升子序列问题了,当然按p ...

随机推荐

  1. Flash Air 打包安卓 ane

    工具: 1.flash builder 2.adt打包工具 3.数字证书 一. 创建 jar 文件 1. 打开flash builder, 新建一个java 项目. 2.点击项目属性,选择Java构建 ...

  2. 【PL/SQL练习】复合变量: 可以一次传递多个值到变量中。

    1.案例: 通过record类型处理一行多列的数据 复合变量--record 记录类型record:在一个变量中可以存储多个区域,每个区域可以是一个标量.记录或table (用于处理一行多列的信息) ...

  3. c#中using System.Runtime.Serialization.Json;不能引用

    原因:生成项目对于的.net版本是2.0的.需要手动切换成4.0 操作步骤:右键项目,属性页面,下拉框选择最高版本.net 截图:

  4. JSTL的全称:JSP Standard Tag Library, jsp 标准标签库

    JSTL的全称:JSP Standard Tag Library, jsp 标准标签库 JSTL的作用     提供给Java web开发人员一个标准通过的标签函数库和EL来取代传统直接在页面上嵌入j ...

  5. ES6生成器基础

    ES6引进的最令人兴奋的特性就是一种新的函数生成方式,称为生成器(generator).名称有点奇怪,但是第一眼看上去行为更加奇怪.文章主要介绍生成器如何工作,然后让你明白为什么他们对于未来的JS会有 ...

  6. 开源Cheops软件在企业中的应用

    笔者在所属分公司的网络改造的网络改造中采用了开源软件作为其网络管理软件,曾经使用时间达2年多,没有出现过问题,其本身具备的主要管理功能完全可以商业的网管软件相媲美,下文将其部署心得和大家一起分享.一. ...

  7. H264转成RGB24格式-2016.01.21

    #include "libavcodec/avcodec.h" #include "libswscale/swscale.h" #include "l ...

  8. CSS3文字描边 CSS3字体外部描边

    给需要实现文字描边的元素添加如下CSS3的属性 text-shadow:#000 1px 0 0,#000 0 1px 0,#000 -1px 0 0,#000 0 -1px 0; -webkit-t ...

  9. 查看cics 运行状态

     查看cics 运行状态cicscp -v status all 

  10. typedef 及其与struct的结合使用

    //相当于为现有类型创建一个别名,或称类型别名. //整形等 typedef int size; //字符数组 ]; ];//=> typedef ]; Line text, secondlin ...