题目连接

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. -(UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event

    在有全屏侧滑的情况下,页面上有个slider需要左右滑动的时候,经常在滑动slider的时候页面也跟着滑动                解决办法一:关闭当前页面的全屏侧滑,开启系统侧滑   self ...

  2. MSP430F149学习之路——捕获/比较模式

    1.捕获模式 #include <msp430x14x.h> unsigned ,last1=; unsigned ,j=; void mian(void) { WDTCTL = WDTP ...

  3. TCP/IP详解学习笔记(1)-- 概述

    1.TCP/IP的分层结构      网络协议通常分不同层次进行开发,每一层分别负责不同的同信功能.TCP/IP通常被认为是一个四层协议系统.      如图所示.       1)链路层(数据链路层 ...

  4. 关于cookie

    CookieHelper.WriteCookie("DEPID", "theway", depid);   //先写入cookie //再读取cookie Us ...

  5. oracle中的针对该库的表

    ALL_TAB_COLUMNS:所有用户的表字段 USER_TAB_COMMENTS:当前用户的所有表备注 USER_COL_COMMENTS:当前用户的所有列备注 USER_TAB_COLUMNS: ...

  6. WF4.0 Activities<第一篇>

    一.基元工具 1.Delay Delay用于延迟一段时间执行下面的流程.在WF中实例是单线程运行的,Delay并不是Thread.Sleep方法实现的. Delay有一个Duration属性,用于设置 ...

  7. 【MySQL】MySQL/MariaDB的优化器对in子查询的处理

    参考:http://codingstandards.iteye.com/blog/1344833 上面参考文章中<高性能MySQL>第四章第四节在第三版中我对应章节是第六章第五节 最近分析 ...

  8. 兰勃特投影C#实现

    兰勃特投影是等面积投影. static double PI = 3.1415926; //------------------------------------------------------- ...

  9. 关于fork函数中的内存复制和共享

    原来刚刚开始做linux下面的多进程编程的时候,对于下面这段代码感到很奇怪, #include<unistd.h> #include<stdio.h> #include< ...

  10. Nginx+Tomcat+Memcached集群

    Tomcat集群session同步方案有以下几种方式: 使用tomcat自带的cluster方式,多个tomcat间自动实时复制session信息,配置起来很简单.但这个方案的效率比较低,在大并发下表 ...