hdu 2988 Dark roads
题目连接
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的更多相关文章
- HDU 2988 Dark roads(kruskal模板题)
Dark roads Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- HDU 2988 Dark roads (裸的最小生成树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2988 解题报告:一个裸的最小生成树,没看题,只知道结果是用所有道路的总长度减去最小生成树的长度和. # ...
- HDU 2988.Dark roads-最小生成树(Kruskal)
最小生成树: 中文名 最小生成树 外文名 Minimum Spanning Tree,MST 一个有 n 个结点的连通图的生成树是原图的极小连通子图,且包含原图中的所有 n 个结点,并且有保持图连通的 ...
- 【HDOJ】2988 Dark roads
最小生成树. /* */ #include <iostream> #include <string> #include <map> #include <que ...
- Dark roads(kruskal)
Dark roads Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total Su ...
- HDU 1102 Constructing Roads, Prim+优先队列
题目链接:HDU 1102 Constructing Roads Constructing Roads Problem Description There are N villages, which ...
- HDOJ(HDU).1025 Constructing Roads In JGShining's Kingdom (DP)
HDOJ(HDU).1025 Constructing Roads In JGShining's Kingdom (DP) 点我挑战题目 题目分析 题目大意就是给出两两配对的poor city和ric ...
- hdu 2988(最小生成树 kruskal算法)
Dark roads Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- HDU 1025 Constructing Roads In JGShining's Kingdom(求最长上升子序列nlogn算法)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1025 解题报告:先把输入按照r从小到大的顺序排个序,然后就转化成了求p的最长上升子序列问题了,当然按p ...
随机推荐
- Mac OS下Boot2Docker的网络映射设置
Mac系统下安装Boot2Docker造成了三层网络:Mac->VirtualBox->Docker,通过docker run 参数-p/-P,能手动或自动设置VirtualBox与Doc ...
- 洛谷P2727 01串 Stringsobits
P2727 01串 Stringsobits 24通过 55提交 题目提供者该用户不存在 标签USACO 难度普及+/提高 提交 讨论 题解 最新讨论 这题的思路是啥啊!!!跪求- 题目背景 考虑 ...
- oracle表中某个字段含有字符回车、空格的手动修改方式
select t.*, t.rowid from TB_SD_STANDARD_CHOOSE_ADVISE t where t.id =323900000 update TB_SD_STANDARD_ ...
- C#中List集合转换JSON
#region 将List<>转换为Json public string List2JSON(List<object> objlist, string classname) { ...
- psutil
tar -zxvf psutil-2.1.3.tar.gz cd psutil-2.1.3 python setup.py install 安装是出现报错 error: command 'gcc' f ...
- 【MySQL】MySQL同步报错-> Last_IO_Error: Got fatal error 1236 from master when reading data from binary log
这个报错网上搜索了一下,大部分是由于MySQL意外关闭或强制重启造成的binlog文件事务点读取异常造成的主从同步报错 Last_IO_Error: Got fatal error 1236 from ...
- 自动发送EMAIL
*&---------------------------------------------------------------------* *& Report ZPP_SEND ...
- 基本的Web控件一
ASP.NET提供了与HTML元素相对应的基本Web控件,ASP.NET提供的基本的Web控件如下: 基本的Web控件 对应的HTML元素 Label ----------------- ...
- bootstrap中弹出窗体dialog的自定义
感谢nakupanda的https://github.com/nakupanda/bootstrap3-dialog 根据需要弹出窗体,但是可以移动,不遮挡下面的内容,所以就修改了源代码,添加了一个属 ...
- C#去除List中集合的重复项(类型对象和单一类型)
去除重复类型对象BookInfo示例: bookList = bookList.Distinct(new DataRowComparer()).ToList(); //去除重复书籍 /// <s ...