HDU 6187 Destroy Walls (思维,最大生成树)
HDU 6187 Destroy Walls (思维,最大生成树)
Destroy Walls
*Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Others)
Total Submission(s): 1784 Accepted Submission(s): 692
*
Problem Description
Long times ago, there are beautiful historic walls in the city. These walls divide the city into many parts of area.
Since it was not convenient, the new king wants to destroy some of these walls, so he can arrive anywhere from his castle. We assume that his castle locates at (0.6∗2√,0.6∗3√).
There are n towers in the city, which numbered from 1 to n. The ith's location is (xi,yi). Also, there are m walls connecting the towers. Specifically, the ith wall connects the tower ui and the tower vi(including the endpoint). The cost of destroying the ith wall is wi.
Now the king asks you to help him to divide the city. Firstly, the king wants to destroy as less walls as possible, and in addition, he wants to make the cost least.
The walls only intersect at the endpoint. It is guaranteed that no walls connects the same tower and no 2 walls connects the same pair of towers. Thait is to say, the given graph formed by the walls and towers doesn't contain any multiple edges or self-loops.
Initially, you should tell the king how many walls he should destroy at least to achieve his goal, and the minimal cost under this condition.
Input
There are several test cases.
For each test case:
The first line contains 2 integer n, m.
Then next n lines describe the coordinates of the points.
Each line contains 2 integers xi,yi.
Then m lines follow, the ith line contains 3 integers ui,vi,wi
|xi|,|yi|≤105
3≤n≤100000,1≤m≤200000
1≤ui,vi≤n,ui≠vi,0≤wi≤10000
Output
For each test case outout one line with 2 integers sperate by a space, indicate how many walls the king should destroy at least to achieve his goal, and the minimal cost under this condition.
Sample Input
4 4
-1 -1
-1 1
1 1
1 -1
1 2 1
2 3 2
3 4 1
4 1 2
Sample Output
1 1
Source
题意:
什么坐标什么的都是胡人的,题意有点迷。
给定一个含有n个点,m个边的无向图。
问最少删除多少个边,同时删除的边的边权sum和尽可能少,使其满足剩下的图仍然联通。
思路:
在该DAG上跑一个最大生成树,并记录生成树的边数cnt和权值sum和cost。
同时记sum为DAG的每一个边的sum和。
那么 m-cnt 和 sum- cost 就是答案1和答案2.
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define sz(a) int(a.size())
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
#define du3(a,b,c) scanf("%d %d %d",&(a),&(b),&(c))
#define du2(a,b) scanf("%d %d",&(a),&(b))
#define du1(a) scanf("%d",&(a));
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) {a %= MOD; if (a == 0ll) {return 0ll;} ll ans = 1; while (b) {if (b & 1) {ans = ans * a % MOD;} a = a * a % MOD; b >>= 1;} return ans;}
void Pv(const vector<int> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%d", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}}
void Pvl(const vector<ll> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%lld", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}}
inline void getInt(int *p);
const int maxn = 1000010;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
struct node {
int f, t, w;
node(int ff, int tt, int ww)
{
f = ff;
t = tt;
w = ww;
}
bool operator < (const node &b) const
{
return w > b.w;
}
};
std::vector<node> v;
int x, y;
int n, m;
int ans1 = 0;
ll cost = 0ll;
ll sum = 0ll;
int fa[maxn];
void init()
{
repd(i, 1, n) {
fa[i] = i;
}
}
int findpar(int x)
{
return x == fa[x] ? x : fa[x] = findpar(fa[x]);
}
void mg(int x, int y)
{
x = findpar(x);
y = findpar(y);
fa[x] = y;
}
void solve()
{
init();
sort(ALL(v));
for (auto temp : v) {
x = temp.f;
y = temp.t;
x = findpar(x);
y = findpar(y);
if (x != y) {
mg(x, y);
cost += temp.w;
ans1++;
}
}
}
int main()
{
//freopen("D:\\code\\text\\input.txt","r",stdin);
//freopen("D:\\code\\text\\output.txt","w",stdout);
gbtb;
while (cin >> n >> m) {
ans1 = 0ll;
sum = 0ll;
cost = 0ll;
v.clear();
repd(i, 1, n) {
cin >> x >> y;
}
int z;
repd(i, 1, m) {
cin >> x >> y >> z;
sum += z;
v.push_back(node(x, y, z));
}
solve();
cout << m - ans1 << " " << sum - cost << endl;
}
return 0;
}
inline void getInt(int *p)
{
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '0');
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 - ch + '0';
}
} else {
*p = ch - '0';
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 + ch - '0';
}
}
}
HDU 6187 Destroy Walls (思维,最大生成树)的更多相关文章
- HDU 6187 Destroy Walls
Destroy Walls Long times ago, there are beautiful historic walls in the city. These walls divide the ...
- HDU 6187 Destroy Walls (对偶图最小生成树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6187 题意:有一个V个结点M条边的带边权无向平面图,有一个人在一个区域,要拆一些墙使得他可以到达任意一 ...
- HDU - 6187 (最大生成树) 最小生成树
Destroy Walls Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Others) ...
- hdu 3367(Pseudoforest ) (最大生成树)
Pseudoforest Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tot ...
- hdu 4940 Destroy Transportation system(水过)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4940 Destroy Transportation system Time Limit: 2000/1 ...
- HDU 1692 Destroy the Well of Life 水题
Destroy the Well of Life Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showprob ...
- hdu 4940 Destroy Transportation system (无源汇上下界可行流)
Destroy Transportation system Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 ...
- HDU 6651 Final Exam (思维)
2019 杭电多校 7 1006 题目链接:HDU 6651 比赛链接:2019 Multi-University Training Contest 7 Problem Description Fin ...
- HDU 4940 Destroy Transportation system(无源汇有上下界最大流)
看不懂题解以及别人说的集合最多只有一个点..... 然后试了下题解的方法http://blog.sina.com.cn/s/blog_6bddecdc0102uzka.html 首先是无源汇有上下界最 ...
随机推荐
- Selenium登录126邮箱,chrome定位不到账号输入框解决办法
使用id定位Message: no such element: Unable to locate element: {"method":"id","s ...
- linux 文件描述符表 打开文件表 inode vnode
在Linux中,进程是通过文件描述符(file descriptors,简称fd)而不是文件名来访问文件的,文件描述符实际上是一个整数.Linux中规定每个进程能最多能同时使用NR_OPEN个文件 ...
- TypeScript(二)使用Webpack搭建环境
今天继续来更新,本篇文章我们讲环境搭建,主要分享一些环境搭建的学习资源及安装步骤,解决一些安装时可能会出现的问题.下面就让我们一起进入学习第一步,搭建TypeScript环境:一. 环境搭建1.1. ...
- Spring中声明式事务的注解@Transactional的参数的总结(REQUIRED和REQUIRES_NEW的与主方法的回滚问题)
一.事务的传播行为1.介绍 当事务方法被另一个事务方法调用时,必须指定事务应该如何传播.例如:方法可能继续在现有事务中运行,也可能开启一个新事务,并在自己的事务中运行.2.属性 事务的传播行为可以由传 ...
- 【坑】前后端分离开发中 跨域问题以及前台不带cookie的问题
文章目录 前言 跨域问题 cookie问题 拦截器导致的跨域问题 后记 前言 场景一: 前台哒哒哒的点击页面,发送请求,但是后台服务器总是没有回应,后台接口虽打了断点,但是根本进不到断点处: 前端:我 ...
- poj 3061 题解(尺取法|二分
题意 $ T $ 组数据,每组数据给一个长度 $ N $ 的序列,要求一段连续的子序列的和大于 $ S $,问子序列最小长度为多少. 输入样例 2 10 15 5 1 3 5 10 7 4 9 2 8 ...
- HCIA SWITCHING&ROUTTING 笔记——第一章 TCP/IP基础知识(2)
视频地址:https://ilearningx.huawei.com/courses/course-v1:HuaweiX+EBGTC00000336+Self-paced/courseware/abb ...
- HCIA SWITCHING&ROUTTING 笔记——第一章 TCP/IP基础知识(1)
视频地址:https://ilearningx.huawei.com/courses/course-v1:HuaweiX+EBGTC00000336+Self-paced/courseware/abb ...
- AVR单片机教程——序言
我一直觉得现在的网络环境对电子技术的学习有一点问题,但始终无法确切地指出,更何况网络上相关资源已经那么丰富. 但我觉得是问题的,无论它到底是不是问题,对我来说总归是一个问题.我学习也不算深入,很多东西 ...
- 网易自动化测试工具(airtest)的环境部署
airtest 环境配置: 1.安装Python2.7 及 Python3.6 版本(2个需要都安装) 2.配置python环境变量(AirtestIDE 需要在python2.x的环境下运行,所以尽 ...