(贪心)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 ...
随机推荐
- Jquery判断某字符串中是否包含某个字符
if(!(to_city_value.indexOf("(")>0){ //code..... }
- ggplot饼图
目录: 原始图样 如何去除饼图中心的杂点 如何去除饼图旁边的标签 如何去掉左上角多出来的一横线 如何去掉图例的标题,并将图例放到上面 如何对图例的标签加上百分比 如何让饼图的小块按顺时针从大到小的顺序 ...
- 写给测试人员:不是所有的bug都需要修复
用户往往对产品中各种各样的bug抱怨不已,而测试人员往往认为自己的职责就是揪出这些所有的bug并把它们全都修复.然而,这是一个误区.微软卓越测试工程总监Alan Page近日撰文,再次解释了有哪些bu ...
- 25个非常实用的jQuery/CSS3应用组件
今天分享25款功能十分强大的jQuery/CSS3应用插件,欢迎收藏. 1.jQuery水晶样式下拉导航 这是一款非常不错的jQuery多功能下拉菜单插件,菜单外观呈水晶样式,晶莹剔透,功能丰富,包含 ...
- ibus拼音安装_ubuntu10.04
ubuntu10.04自带的拼音输入发太难用,所以从新安装ibus拼音. sudo apt-get install ibus ibus-pinyin ibus-qt4 ibus-gtk 然后运行 ib ...
- Qt Creater中Clang-format的使用
起因在于习惯性的想格式化代码,发现Qt Creater默认居然是没有代码格式化的,只有一个缩进,搞毛线啊!!! 搜索了下,倒是很容易就搜到了,Qt Creater中有个插件:beautifier,在 ...
- erlang-百度云推送Android服务端功能实现-erlang
百度云推送官方地址http://developer.baidu.com/wiki/index.php?title=docs/cplat/push 简单的介绍下原理: 百度云推送支持IOS和Androi ...
- (转)Spring开启Annotation<context:annotation-config> 和 <context:component-scan>诠释及区别
转自:https://www.cnblogs.com/leiOOlei/p/3713989.html <context:annotation-config> 和 <context:c ...
- Java基础--生成验证码
HTML <%@ page language="java" contentType="text/html; charset=UTF-8" pageEnco ...
- 针对后台列表table拖拽比较实用的jquery拖动排序
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&qu ...