hdu4313

Matrix

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2204    Accepted Submission(s): 824
Problem Description
Machines have once again attacked the kingdom of Xions. The kingdom of Xions has N cities and N-1 bidirectional roads. The road network is such that there is a

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.
 
Input
The first line is an integer T represents there are T test cases. (0<T <=10)

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
Output
For each test case print the minimum time required to disrupt the connection among Machines.
Sample Input
1
5 3
2 1 8
1 0 5
2 4 5
1 3 4
2
4
0
 
Sample Output
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思想的更多相关文章

  1. 『嗨威说』算法设计与分析 - 贪心算法思想小结(HDU 2088 Box of Bricks)

    本文索引目录: 一.贪心算法的基本思想以及个人理解 二.汽车加油问题的贪心选择性质 三.一道贪心算法题点拨升华贪心思想 四.结对编程情况 一.贪心算法的基本思想以及个人理解: 1.1 基本概念: 首先 ...

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

  3. HDU 1735 字数统计(模拟+一点点贪心的思想)

    题目戳我 字数统计 Time Limit: 1000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  4. 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个兵,而每个兵传送至该 ...

  5. HDU—2021-发工资咯(水题,有点贪心的思想)

    作为杭电的老师,最盼望的日子就是每月的8号了,因为这一天是发工资的日子,养家糊口就靠它了,呵呵  但是对于学校财务处的工作人员来说,这一天则是很忙碌的一天,财务处的小胡老师最近就在考虑一个问题:如果每 ...

  6. poj3122-Pie(二分法+贪心思想)

    一,题意: 有f+1个人(包括自己),n块披萨pie,给你每块pie的半径,要你公平的把尽可能多的pie分给每一个人 而且每个人得到的pie来自一个pie,不能拼凑,多余的边角丢掉.二,思路: 1,输 ...

  7. poj1323-Game Prediction(贪心思想)

    贪心的思想:尽量的从最大值找起.然后在剩余之中,再从最大值找起. 一,题意: M个人,每人N张牌,每轮比较谁出的牌大,最大者为胜.现在给定M和N,以及你的牌,要求输出你至少能确保获得几轮的胜利 从&q ...

  8. HDU 4857 逃生(反向建边的拓扑排序+贪心思想)

    逃生 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submissi ...

  9. POJ 3687 Labeling Balls(反向拓扑+贪心思想!!!非常棒的一道题)

    Labeling Balls Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16100   Accepted: 4726 D ...

随机推荐

  1. jquery easy ui 简单字段选择搜索实现

    code <!DOCTYPE html><html><head> <meta charset="UTF-8"> <title& ...

  2. MySQL多表数据记录查询详解

    在实际应用中,经常需要实现在一个查询语句中显示多张表的数据,这就是所谓的多表数据记录连接查询,简称来年将诶查询. 在具体实现连接查询操作时,首先将两个或两个以上的表按照某个条件连接起来,然后再查询到所 ...

  3. WCF系列

    转自:1) http://www.cnblogs.com/zhili/p/WCFSummary.html 2)  http://www.cnblogs.com/artech/archive/2009/ ...

  4. MVC路由机制(转)

    今天我来缕一下MVC的路由机制,因为使用MVC已经快一年了,之前也只是上手,没有系统去理会.项目完了,结合实际使用,回过头来深入一下. MVC 学习索引(点击即可) 一个请求进入IIS后 传统ASP. ...

  5. C的内存四大区

    前提 看视频得来的内容,只知道不止4个区,但主要是这4个区. 四区 静态区 用于存放所有的全局变量和静态变量. ; //静态区 int main(){ ; //静态区 ; } 代码区 就是存放程序的执 ...

  6. Runtime是什么?

    在看 RPC 的概念模型与实现解析 的时候,看到图片上有Runtime,又想到见过很多Runtime之类的东西,所以就想弄明白这到底是个什么东西. (因为是程序名,所以根本没想到代码的“编译-运行”~ ...

  7. bson.errors.InvalidStringData: strings in documents must be valid UTF-8

    场景: pymongo 查询数据库的时候报错. for gscode in GSList_StockPool_Mongo_MktStop: self._collection_flash.find({& ...

  8. 【Java面试题】31 介绍Collection框架的结构

    Collection:List列表,Set集 Map:Hashtable,HashMap,TreeMap   Collection  是单列集合 List   元素是有序的.可重复 有序的 colle ...

  9. AssetBundle中Unload()方法的作用

    AssetBundle.Unload(false)的作用: 官网的解释是这样的: When unloadAllLoadedObjects is false, compressed file data ...

  10. makefile--参数传递、条件判断、include (五)

    原创博文,转载请标明出处--周学伟http://www.cnblogs.com/zxouxuewei/ 在多个Makefile嵌套调用时,有时我们需要传递一些参数给下一层Makefile.比如我们在顶 ...