(贪心)kruskal思想
Matrix
unique path between any pair of cities.
Morpheus has the news that K Machines are planning to destroy the whole kingdom. These Machines are initially living in K different cities of the kingdom and
anytime from now they can plan and launch an attack. So he has asked Neo to destroy some of the roads to disrupt the connection among Machines. i.e after destroying those roads there should not be any path between any two Machines.
Since the attack can be at any time from now, Neo has to do this task as fast as possible. Each road in the kingdom takes certain time to get destroyed and they
can be destroyed only one at a time.
You need to write a program that tells Neo the minimum amount of time he will require to disrupt the connection among machines.
For each test case the first line input contains two, space-separated integers, N and K. Cities are numbered 0 to N-1. Then follow N-1 lines, each containing three, space-separated integers, x y z, which means there is a bidirectional road connecting city x
and city y, and to destroy this road it takes z units of time.Then follow K lines each containing an integer. The ith integer is the id of city in which ith Machine is currently located.
2 <= N <= 100,000
2 <= K <= N
1 <= time to destroy a road <= 1000,000
1
5 3
2 1 8
1 0 5
2 4 5
1 3 4
2
4
0
10
题意:有一颗树形图王国,在有些城市会放置一种随时会爆炸的炸弹,为了使放置炸弹的这些城市两两互不连通,需要破坏掉一些道路,破坏每条道路都需要花费一定的时间,问至少花费多少时间可以完成任务;
分析:贪心算法:类似kruskal最小生成树的过程,不过此处将边按权值从大到小排列,每次将边加进来时要判断是否会使两个危险的点连通,是的话这条边就是需要被删除的,否则将它加到树上。
程序:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include"stdio.h"
#include"string.h"
#include"iostream"
#include"map"
#include"string"
#include"queue"
#include"stdlib.h"
#include"math.h"
#include"algorithm"
#include"vector"
#define M 100009
#define eps 1e-10
#define inf 1000000000
#define mod 1000000000
#define INF 1000000000
using namespace std;
struct node
{
int u,v,w;
}e[M];
int cmp(node a,node b)
{
return a.w>b.w;
}
int f[M],use[M];
int finde(int x)
{
if(x!=f[x])
f[x]=finde(f[x]);
return f[x];
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int n,k,a,i;
scanf("%d%d",&n,&k);
for(i=0;i<n;i++)
f[i]=i;
for(i=0;i<n-1;i++)
scanf("%d%d%d",&e[i].u,&e[i].v,&e[i].w);
memset(use,0,sizeof(use));
for(i=1;i<=k;i++)
{
scanf("%d",&a);
use[a]=1;
}
sort(e,e+n-1,cmp);
__int64 ans=0;
for(i=0;i<n-1;i++)//加入并查集的时候最好把每个集合的的根节点指向有标记的点,方便比较
{
if(use[e[i].u]&&use[e[i].v])
ans+=e[i].w;
else if(use[e[i].u]&&!use[e[i].v])
{
int x=finde(e[i].v);
if(use[x])
ans+=e[i].w;
else
f[x]=finde(e[i].u); }
else if(!use[e[i].u]&&use[e[i].v])
{
int x=finde(e[i].u);
if(use[x])
ans+=e[i].w;
else
f[x]=finde(e[i].v);
}
else
{
int x=finde(e[i].u);
int y=finde(e[i].v);
if(use[x]&&use[y])
ans+=e[i].w;
else if(use[x]&&!use[y])
f[y]=x;
else
f[x]=y;
}
}
printf("%I64d\n",ans);
}
return 0;
}
(贪心)kruskal思想的更多相关文章
- 『嗨威说』算法设计与分析 - 贪心算法思想小结(HDU 2088 Box of Bricks)
本文索引目录: 一.贪心算法的基本思想以及个人理解 二.汽车加油问题的贪心选择性质 三.一道贪心算法题点拨升华贪心思想 四.结对编程情况 一.贪心算法的基本思想以及个人理解: 1.1 基本概念: 首先 ...
- Codeforces Round #451 (Div. 2)-898A. Rounding 898B.Proper Nutrition 898C.Phone Numbers(大佬容器套容器) 898D.Alarm Clock(超时了,待补坑)(贪心的思想)
A. Rounding time limit per test 1 second memory limit per test 256 megabytes input standard input ou ...
- HDU 1735 字数统计(模拟+一点点贪心的思想)
题目戳我 字数统计 Time Limit: 1000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- 2016 NEERC, Moscow Subregional Contest K. Knights of the Old Republic(Kruskal思想)
2016 NEERC, Moscow Subregional Contest K. Knights of the Old Republic 题意:有一张图,第i个点被占领需要ai个兵,而每个兵传送至该 ...
- HDU—2021-发工资咯(水题,有点贪心的思想)
作为杭电的老师,最盼望的日子就是每月的8号了,因为这一天是发工资的日子,养家糊口就靠它了,呵呵 但是对于学校财务处的工作人员来说,这一天则是很忙碌的一天,财务处的小胡老师最近就在考虑一个问题:如果每 ...
- poj3122-Pie(二分法+贪心思想)
一,题意: 有f+1个人(包括自己),n块披萨pie,给你每块pie的半径,要你公平的把尽可能多的pie分给每一个人 而且每个人得到的pie来自一个pie,不能拼凑,多余的边角丢掉.二,思路: 1,输 ...
- poj1323-Game Prediction(贪心思想)
贪心的思想:尽量的从最大值找起.然后在剩余之中,再从最大值找起. 一,题意: M个人,每人N张牌,每轮比较谁出的牌大,最大者为胜.现在给定M和N,以及你的牌,要求输出你至少能确保获得几轮的胜利 从&q ...
- HDU 4857 逃生(反向建边的拓扑排序+贪心思想)
逃生 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submissi ...
- POJ 3687 Labeling Balls(反向拓扑+贪心思想!!!非常棒的一道题)
Labeling Balls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16100 Accepted: 4726 D ...
随机推荐
- 在 C++ 程序中只使用 const 常量而不使用宏常量
在 C++ 程序中只使用 const 常量而不使用宏常量,即 const 常量完 全取代宏常量. #include <iostream> /* run this program using ...
- (转)如何根据RGB值来判断这是种什么颜色?
如何根据RGB值来判断这是种什么颜色? 下面介绍几种典型颜色的RGB值,格式为:颜色(R,G,B). 想象一下有红.绿.蓝三盏射灯打出三束光. 这三束光叠加在一起时产生白色,如果三盏灯的亮度都减半就产 ...
- jquery注册文本框获取焦点清空,失去焦点赋值的简单实例
在我们开发过程中特别是用户注册时会有一个效果,就是文本框获取焦点清空提示,如果用户没有输入信息失去焦点赋值上我们的提示语. <html> <head> <meta h ...
- OpenCV学习:改变图像的对比度和亮度
本实例演示简单地改变图像的对比度和亮度,使用了如下线性变换来实现像素值的遍历操作: The parameters α > 0 and β often called the gain and bi ...
- mysql数据库中,通过mysqladmin工具,创建数据库
需求描述: 今天接触到了mysqladmin工具,主要是一个客户端工具,用来管理mysql server的 可以通过mysqladmin直接创建数据库,而不需要登录到mysql控制台中,在此记录下. ...
- 请问C#中string是值传递还是引用传递?
https://www.cnblogs.com/xiangniu/archive/2011/08/17/2143486.html 学了这么久,终于弄明白了... 是引用传递 但是string又有值传递 ...
- PHP-002
PHP URL重写 怎样在IIS环境下配置Rewrite规则_百度经验 http://jingyan.baidu.com/article/c33e3f485a7c74ea15cbb50e.html W ...
- Django管理工具django-admin.py创建项目
安装Django之后就安装了可用的管理工具django-admin.py,可以使用它来创建项目,运行django-admin.py来查看命令介绍:
- Dubbo(四) -- telnet命令
一.telnet的作用 当dubbo服务(即生产者)发布之后,我们可以通过telnet命令来来进行调试和管理,以及跟踪服务调用的次数. 注意:2.0.5以上版本服务提供端口支持telnet命令,协议一 ...
- 管理开机启动:systemd
一.CentOS7 systemd 介绍 在 CentOS7 中,使用 systemd 来管理其他服务是否开机启动,systemctl 是 systemd 服务的命令行工具 [root@localho ...