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$需要排在第一位,只有$ ...
随机推荐
- JAVA笔记1-00
package chapter1; public class Demo1 { public static void main(String[] args) { System.out.println(& ...
- Jquery基础之动画操作
Jquery的动画效果能够轻松的为网页添加动画效果,为网页带来了新的活力. 一.show()方法和hide()方法 show()方法和hide()方法是jquery中最基本的动画方法.使用show() ...
- mybatis数据库数据分页问题
http://www.cnblogs.com/jcli/archive/2011/08/09/2132222.html 借花献佛,天天进步
- 2.6. Statistical Models, Supervised Learning and Function Approximation
Statical model regression $y_i=f_{\theta}(x_i)+\epsilon_i,E(\epsilon)=0$ 1.$\epsilon\sim N(0,\sigma^ ...
- poj 1018 Communication System_贪心
题意:给你n个厂,每个厂有m个产品,产品有B(带宽),P(价格),现在要你求最大的 B/P 明显是枚举,当P大于一定值,B/P为零,可以用这个剪枝 #include <iostream> ...
- virtualbox 复制多个虚拟机 (宿主机redhat)
我用VirtualBox做了一个winxp虚拟镜像. 想实现不重新安装而直接复制几个,也就是同时装载几个虚拟机. 但是直接复制已安装好机子的vdi文件,系统会报uuid已存在的错误. 所以,就需要修改 ...
- 聚类算法初探(五)DBSCAN
最近由于工作需要,对聚类算法做了一些相关的调研.现将搜集到的资料和自己对算法的一些理解整理如下,供大家参考. 另外在算法代码方面,我也做了一些实现(包括串行和并行),欢迎感兴趣的朋友探讨和交流. 第一 ...
- C# 获取网站的 IIS 站点名称 ,获取站点当前连接数
System.Web.Hosting.HostingEnvironment.ApplicationHost.GetSiteName(); System.Management.ManagementObj ...
- hadoop结构出现后format变态
14/07/10 18:50:47 FATAL conf.Configuration: error parsing conf file: com.sun.org ...
- SQL 时间格式化函数
1 取值后格式化 {0:d}小型:如2005-5-6 {0:D}大型:如2005年5月6日 {0:f}完整型 2 当前时间获取 DateTime.Now.ToShortDateString 3 取值中 ...