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$需要排在第一位,只有$ ...
随机推荐
- mysql解压版配置
2. 解压MySQL压缩包 将以下载的MySQL压缩包解压到自定义目录下,我的解压目录是: "D:\Program Files\MySQL\mysql-5.6.13-win32" ...
- kvm-GT
REF: http://los-vmm.sc.intel.com/wiki/How-to-setup-kvmgthttp://xenvgt.sh.intel.com/image/bdw-h/ Host ...
- hdu 1050 Moving Tables_贪心
题意:你搬n个桌子,桌子从一个地方搬到另一个地方,走廊只允许同时一个桌子通过,教室分布在两边,奇数在一边,偶数在一边,当桌子不冲突时可以同时搬运,冲突时要等别的那个桌子搬完再搬. 思路:因为奇数桌子在 ...
- C Deepin指针
L-value -->标识了一个可以存储结果值的内存存储地址--指针变量 or 普通内存变量; R-value -->数值;
- OpenStack IdentityService Keystone V3 API Curl实战
v3 API Examples Using Curl <Tokens> 1,Default scope 获取token Get an token with default scope (m ...
- Windows下安装Python3.4.2
一.Windows下安装Python3.4.2 1.下载Windows下的Python3.4.2.exe 2.指定一个目录安装,然后下一步 3.配置环境变量包括Python.exe的文件.目录如下图所 ...
- 标准C++的string类使用
原文:http://www.cnblogs.com/xFreedom/archive/2011/05/16/2048037.html 要想使用标准C++中string类,必须要包含#include & ...
- https配置
总结了一下: 所谓用HTTPS的时候 app 前端要配置SSL 证书的意思就是:相当于服务器端与app 前端, 一个拿公钥,一个拿私钥.简单得说就是客户端发送请求的时候,用配置好的SSL证书里的加密方 ...
- windows下如何使用ssh远程登录Linux
1.ssh的安装 sudo apt-get install ssh 或者在Linux情况下,自带有ssh,从第二步开始 2.生成密钥 ssh-keygen 中间过程会提示你存放密钥的地方Enter ...
- new关键字和newInstance()方法的区别
转载:http://blog.csdn.net/iloveyin/article/details/7965183 1.类的加载方式不同 在执行Class.forName("a.clas ...