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 ...
随机推荐
- 如何在Flash中新窗口打开页面而不被拦截
Flash的wmode必须是opaque或者transparent,允许Flash访问页面脚本.另外跳转必须是点击直接触发. 代码:ExternalInterface.call("windo ...
- Laxcus大数据管理系统2.0(2)- 第一章 基础概述 1.1 基于现状的一些思考
第一章 基础概述 1.1 基于现状的一些思考 在过去十几年里,随着互联网产业的普及和高速发展,各种格式的互联网数据也呈现爆炸性增长之势.与此同时,在数据应用的另一个重要领域:商业和科学计算,在各种新兴 ...
- sql语句小练习二
1.创建一个数据库StudentManage, 初始化大小10M,不足以1M每次增长 create database StudentManage on ( name = 'StudentManag ...
- 为Magento2新主题添加使用Grunt
Go to \dev\tools\grunt\configs, open your themes.js file, and change it according to the following e ...
- leetcode 104
104. Maximum Depth of Binary Tree Given a binary tree, find its maximum depth. The maximum depth is ...
- leetcode 7
此题实现比较简单,但是边界处理比较麻烦.题目要求是以32位考虑,所以可表达的数的范围是-2147483648~2147483648. 我们需要判断当前的数翻转之后是否在这个范围中,我的思路是首先对当前 ...
- 免费在线CAD文件转换
AnyCAD Exchange Cloud 提供在线的CAD文件转换服务,包括二维图纸和三维模型的数据转换. 支持的格式有: DWG/DGN/DXF 到 PDF, SVG, DAE等的转换 STEP/ ...
- Rich控件一
Calendar控件 Calendar控件用来在Web页面中显示日历中的可选日期,并显示与特定日期关联的数据. 控件声明代码如下: <asp: Calendar id=" Calend ...
- Windows Phone 8 通过一个app启动另一个app
Winphone8 通过app启动第三方app需要被启动的app支持,具体操作步骤如下: 假设要通过PhoneApp2启动PhoneApp1 PhoneApp1端做的操作如下: 1.注册Protoco ...
- .NET环境配置(二)
打开IIS服务器 首先在设置程序池 应用程序池 设置 ASP.NET v4.0 ASP.NET v4.0 Classic CLassic.NET AppPool DefaultA ...