
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 ;
}