Destroy Walls

  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

解题思路:
  首先,很重要的一点,本题给出的所有坐标值都没用,都是吓人的。

  本题的意思是有一个城堡,被数条城墙分隔为多个区域,城墙的端点只会是塔楼,并且城墙只在端点相交,一条城墙的两端不会连在同一个塔楼上(图没有自环),拆除每个城墙都会有一定消耗,国王希望通过最少的消耗,使城堡所有区域都连通。

  本题有多组测试用例,每组测试用例包括,塔楼数量(端点数量)n, 城墙数量(边数)m,首先跟随n行,每行包括两个整数x y为塔楼的坐标(没用),之后m行跟随,每行包括三个整数,分别为城墙连接的两个塔楼u , v,拆毁该城墙的消耗w。

  要求输出使所有区域连通所需的最少的消耗。

  仔细思考一下就会发现,本题的最终要求就是让我们把给定的图通过抹去边变成无环图,而且抹去边的权值要尽可能的小。而将平面图变成无环图叫什么?——生成树!!

  提到生成树,我们想到两个算法Prim算法与Kruskal算法,因为本题可以通过将边由大到小排序求的最大生成树,在这里我们使用Kruskal算法。

kruskal算法核心思想:  

  既然已经给出了邻接表。初始视所有塔楼都为不连通(即拆除所有城墙),之后将城墙按消耗排序,从大到小枚举所有城墙,判断城墙两端的塔楼是否已经连通,若已经连通不做处理(即该墙需要拆除)拆除的墙数加一,若不连通则将该边记录入最大生成树(该墙无需拆除),并从拆毁所有城墙的总消耗里减去该城墙的消耗。

bool cmp(edge a, edge b){   //城墙排序为拆除消耗由大到小
return a.w > b.w;
}
LL kruskal(int n, int m, LL sum, int &cnt){ //kruskal算法
//由于需要改变cnt的值所以在这里cnt传引用
LL ans = sum; //传入的sum为拆除所有城墙所需的总消耗
for(int i = ; i <= n; i++){
father[i] = i; //初始化所有塔楼为不连通
}
sort(Edge + , Edge + + m, cmp); //城墙权值从大到小排序
for(int i = ; i <= m; i++){
int faNode1 = getFather(Edge[i].u);
int faNode2 = getFather(Edge[i].v);
if(faNode1 != faNode2){ //判断城墙连接的两个塔楼是否连通
father[faNode1] = faNode2; //不连通则标记为连通
ans -= Edge[i].w; //该城墙不需要拆数
}else{ //如果城墙两个端点塔楼已经连通则该城墙需要拆除
cnt++; //记录需要拆除的城墙数量
}
}
return ans; //返回的ans为拆除的最小消耗

kruskal

判断是否连通使用并查集

int father[maxn];
int getFather(int x)
{
if(father[x] == x)
return x;
else
return father[x] = getFather(father[x]); }

并查集

AC代码

 #include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long LL;
const int maxn = 2e5+;
struct edge{ //edge储存城墙
int u, v; //城墙连接的两个结点
LL w; //拆除的消耗
}Edge[maxn];
int father[maxn];
int getFather(int x) //并查集部分
{
if(father[x] == x)
return x;
else
return father[x] = getFather(father[x]); }
bool cmp(edge a, edge b){ //城墙排序为拆除消耗由大到小
return a.w > b.w;
}
LL kruskal(int n, int m, LL sum, int &cnt){ //kruskal算法
//由于需要改变cnt的值所以在这里cnt传引用
LL ans = sum; //传入的sum为拆除所有城墙所需的总消耗
for(int i = ; i <= n; i++){
father[i] = i; //初始化所有塔楼为不连通
}
sort(Edge + , Edge + + m, cmp); //城墙权值从大到小排序
for(int i = ; i <= m; i++){
int faNode1 = getFather(Edge[i].u);
int faNode2 = getFather(Edge[i].v);
if(faNode1 != faNode2){ //判断城墙连接的两个塔楼是否连通
father[faNode1] = faNode2; //不连通则标记为连通
ans -= Edge[i].w; //该城墙不需要拆数
}else{ //如果城墙两个端点塔楼已经连通则该城墙需要拆除
cnt++; //记录需要拆除的城墙数量
}
}
return ans; //返回的ans为拆除的最小消耗
}
int main(){
int n, m;
while(scanf("%d%d", &n, &m) != EOF){ //输入塔楼数n与城墙数m
for(int i = ; i <= n; i++){
int x, y;
scanf("%d%d", &x, &y); //吸收掉这些没用的坐标
}
LL sum = ;
for(int i = ; i <= m; i++){ //输入邻接表
scanf("%d%d%lld", &Edge[i].u, &Edge[i].v, &Edge[i].w);
sum += Edge[i].w; //记录总权值(拆除所有城墙的消耗)
}
int cnt = ; //cnt记录需要拆除的城墙
LL ans = kruskal(n, m, sum, cnt); //得到最小消耗
printf("%d %lld\n",cnt, ans);
}
return ;
}

HDU 6187 Destroy Walls的更多相关文章

  1. HDU 6187 Destroy Walls (思维,最大生成树)

    HDU 6187 Destroy Walls (思维,最大生成树) Destroy Walls *Time Limit: 8000/4000 MS (Java/Others) Memory Limit ...

  2. HDU 6187 Destroy Walls (对偶图最小生成树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6187 题意:有一个V个结点M条边的带边权无向平面图,有一个人在一个区域,要拆一些墙使得他可以到达任意一 ...

  3. HDU - 6187 (最大生成树) 最小生成树

    Destroy Walls Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others) ...

  4. hdu 4940 Destroy Transportation system(水过)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4940 Destroy Transportation system Time Limit: 2000/1 ...

  5. 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 ...

  6. hdu 4940 Destroy Transportation system (无源汇上下界可行流)

    Destroy Transportation system Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 ...

  7. HDU 4940 Destroy Transportation system(无源汇有上下界最大流)

    看不懂题解以及别人说的集合最多只有一个点..... 然后试了下题解的方法http://blog.sina.com.cn/s/blog_6bddecdc0102uzka.html 首先是无源汇有上下界最 ...

  8. 最短路(数据处理):HDU 5817 Ice Walls

    Have you ever played DOTA? If so, you may know the hero, Invoker. As one of the few intelligence car ...

  9. HDU 4940 Destroy Transportation system(无源汇上下界网络流)

    Problem Description Tom is a commander, his task is destroying his enemy’s transportation system. Le ...

随机推荐

  1. Javascript的事件模型和Promise实现

    1. Javascript的运行时模型——事件循环 JS的运行时是个单线程的运行时,它不像其他编程语言,比如C++,Java,C#这些可以进行多线程操作的语言.当它执行一个函数时,它只会一条路走到黑, ...

  2. Postgresql 日志收集

    PG安装完成后默认不会记录日志,必须修改对应的(${PGDATA}/postgresql.conf)配置才可以,这里只介绍常用的日志配置. 1.logging_collector = on/off - ...

  3. 使用Base64进行string的加密和解密

    //字符串转bytes var ebytes = System.Text.Encoding.Default.GetBytes(keyWord); //bytes进行base64加密 var strBa ...

  4. Modular Arithmetic ( Arithmetic and Algebra) CGAL 4.13 -User Manual

    1 Introduction Modular arithmetic is a fundamental tool in modern algebra systems. In conjunction wi ...

  5. linux命令之文件备份与压缩命令

    1.tar:打包备份 该命令是将多个命令打包到一起,并且可以实现解压打包.打包是将多个文件或者目录变成一个总的文件,压缩则是将一个大的文件通过压缩算法变成一个小文件. 参数 说明 z(常用) 通过gz ...

  6. 开发一个小的php扩展

    今天试了一下在php添加扩展,看了挺多资料,细节上不一致,其他大体是差不多的. 我们来开发一个叫ccvita_string的函数,他的主要作用是返回一段字符,对应的php代码可能如此: functio ...

  7. pkuwc 前的任务计划

    菜鸡 wxw 的计划(肯定会咕咕咕 12.27 luogu P4244 [SHOI2008]仙人掌图 II(咕咕咕 luogu P4246 [SHOI2008]堵塞的交通 (没有咕! luogu P1 ...

  8. Flink学习笔记-新一代Flink计算引擎

    说明:本文为<Flink大数据项目实战>学习笔记,想通过视频系统学习Flink这个最火爆的大数据计算框架的同学,推荐学习课程: Flink大数据项目实战:http://t.cn/EJtKh ...

  9. 对Routers的理解

    路由Routers 对于视图集ViewSet,我们除了可以自己手动指明请求方式与动作action之间的对应关系外,还可以使用Routers来帮助我们快速实现路由信息. REST framework提供 ...

  10. Python3 操作系统与路径 模块(os / os.path / pathlib)

    #!/usr/bin/env python # coding=utf-8 __author__ = 'Luzhuo' __date__ = '2017/5/7' import os def os_de ...