Matrix(类似kruskal)
Matrix
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 2 Accepted Submission(s) : 1
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.
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int MAXN=;
struct Node{
int s,e,t;
};
Node dt[MAXN];
/*int cmp(const void *a,const void *b){
return (*(Node *)a).t-(*(Node *)b).t) ;//
}*/
int cmp(Node a,Node b){
return a.t>b.t;
}
int pre[MAXN];
int find(int x){
return pre[x]= x==pre[x]?x:find(pre[x]);
}
int visit[MAXN],n,m,flot;
__int64 time;
void initial(){
memset(visit,,sizeof(visit));
memset(pre,-,sizeof(pre));
time=;flot=;
}
void merge(Node a){
int f1,f2;
if(pre[a.s]==-)pre[a.s]=a.s;
if(pre[a.e]==-)pre[a.e]=a.e;
f1=find(a.s);f2=find(a.e);
if(f1==f2)return;
if(visit[f1]&&visit[f2]){
time+=a.t;
flot++;
// printf("%d %d\n",a.s,a.e);
}
else if(f1!=f2){
if(visit[f1])pre[f2]=f1;
else pre[f1]=f2;
}
}
int main(){
int T,temp;
scanf("%d",&T);
while(T--){
initial();
scanf("%d%d",&n,&m);
for(int i=;i<n-;i++){
scanf("%d%d%d",&dt[i].s,&dt[i].e,&dt[i].t);
}
///qsort(dt,n-1,sizeof(dt[0]),cmp);
sort(dt,dt+n-,cmp);
for(int i=;i<m;i++){
scanf("%d",&temp);
visit[temp]=;
}
for(int i=;i<n-;i++){
merge(dt[i]);
if(flot==m-)break;
}
printf("%I64d\n",time);
}
return ;
}
Matrix(类似kruskal)的更多相关文章
- BZOJ 1196 [HNOI2006]公路修建问题:二分 + 贪心生成树check(类似kruskal)
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1196 题意: n个城市,m对城市之间可以修公路. 公路有两种,一级公路和二级公路,在第i对 ...
- (贪心)kruskal思想
hdu4313 Matrix Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- 【leetcode】Spiral Matrix II
Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 to n2 in s ...
- BZOJ4883 棋盘上的守卫 基环树、Kruskal
题目传送门:https://www.lydsy.com/JudgeOnline/problem.php?id=4883 题意:给出一个$N \times M$的棋盘,每个格子有权值.你需要每一行选中一 ...
- LeetCode: Spiral Matrix II 解题报告-三种方法解决旋转矩阵问题
Spiral Matrix IIGiven an integer n, generate a square matrix filled with elements from 1 to n2 in sp ...
- UVA 1664 Conquer a New Region (Kruskal,贪心)
题意:在一颗树上要求一个到其他结点容量和最大的点,i,j之前的容量定义为i到j的路径上的最小边容量. 一开始想过由小到大的去分割边,但是很难实现,其实换个顺序就很容易做了,类似kruskal的一个贪心 ...
- [LeetCode] 59. Spiral Matrix II 螺旋矩阵 II
Given an integer n, generate a square matrix filled with elements from 1 to n^2 in spiral order. For ...
- Codeforces #270 D. Design Tutorial: Inverse the Problem
http://codeforces.com/contest/472/problem/D D. Design Tutorial: Inverse the Problem time limit per t ...
- Ideas and Tricks
1.树上拓扑排序计数 结论$\dfrac{n!}{\prod\limits_{i=1}^n size_i}$ 对于节点$i$,其子树随意排序的结果是$size[i]!$ 但$i$需要排在第一位,只有$ ...
随机推荐
- 【转】关于Activity和Task的设计思路和方法
Activity和Task是Android Application Framework架构中最基础的应用,开发者必须清楚它们的用法和一些开发技巧.本文用大量的篇幅并通过引用实例的方式一步步深入全面讲解 ...
- Qt widget--杭州小笼包
1,QPainter::scale(double,double);第一个参数水培方向缩放 shear剪切 QPainter::rotate()旋转,旋转度数,rotate QPainter::tran ...
- jquery如何获得页面元素的坐标值
http://www.cnblogs.com/pansly/archive/2011/05/25/2056222.html jquery如何获得页面元素的坐标值 yulutxt是输入经典语录的输入 ...
- golang基础数据结构
一.概述: 这里主要讨论四种类型---数组.slice.map和结构体 数组和结构体是聚合类型:它们的值都是由很多个元素或者成员字段的值组成.数组是有同构元素组成--每个数组的元素的类型相同:结构体为 ...
- Mysql安装时出现APPLY security settings错误
在安装mysql数据库时,如果重新安装,很容易遇见apply security setting error(access denied for user 'root@localhost'(using ...
- (二)Activity启动模式
一.标准模式(standrard) 1.当新建一个Activity时,默认情况下就是标准模式,也可以通过AndroidManifest文件显示指定其launchMode为standard <?x ...
- win10系统安装 VS 2015 安装包下载
这个VS2015安装包 我找了好久才找到 能在WIN 10系统下安装 下面分享链接地址给大家: http://www.ithome.com/html/win10/215213.htm
- git管理修改
为什么Git比其他版本控制系统设计得优秀,因为Git跟踪并管理的是修改,而非文件. 新增了一行,这就是一个修改,删除了一行,也是一个修改,更改了某些字符,也是一个修改,删了一些又加了一些,也是一个修改 ...
- 本地网址连不上远程mysql问题
问题:host 'XXX.XXX.XXX.XXX'is not allowed to connect to this MySQL server 解决办法: 进入远程mysql #mysql -u ro ...
- 编译安装apache2.4
一.编译安装apache2.4Apache官方说:与Apache 2.2.x相比,Apache 2.4.x提供了很多性能方面的提升,包括支持更大流量.更好地支持云计算.利用更少的内存处理更多的并发等. ...