Matrix (similar to Kruskal)

Source: Internet
Author: User

Matrixtime limit:2000/1000ms (java/other) Memory limit:32768/32768k (Java/other) total submission (s): 2 Accepted S Ubmission (s): 1Problem Descriptionmachines has once again attacked the kingdom of Xions. The Kingdom of Xions has N cities and N-1 bidirectional roads. The road network is such, there is a
Unique path between any pair of cities.

Morpheus have the news that K machines is planning to destroy the whole kingdom. These machines is initially living in K different cities of the kingdom and
Anytime from now they can plan and launch an attack. So he had asked Neo to destroy some of the roads to disrupt the connection among machines. I.e after destroying those roads there should not being any path between any and machines.

Since The attack can is at any time from now, Neo have to does this task as fast as possible. Each road in the kingdom takes certain time to get destroyed and they
Can is destroyed only one at a time.

You need to write a program that tells Neo the minimum amount of time he'll require to disrupt the connection among Mach Ines.

Inputthe first line is a integer t represents there is t test cases. (0
Outputfor each test case print the minimum time required to disrupt the connection among machines.
Sample INPUT1 5 3 2 1 8 1 0 5 2 4 5 1 3 4 2 4 0
Sample Output10: The problem is that some robots in different cities, through the destruction of certain roads, so that they can not contact each other; it takes time to destroy the road, the shortest time is needed, and the problem needs to be sorted from big to small, from big to small, to get those unmarked. By the end of the road marked by the small, the time is less, pay attention to the records of the VIS, as well as to let the tagged as the Boss; Code:
1#include <stdio.h>2#include <string.h>3#include <algorithm>4 using namespacestd;5 Const intmaxn=100010;6 structnode{7     ints,e,t;8 };9 Node DT[MAXN];Ten /*int cmp (const void *a,const void *b) { One Return (* (node *) a). T (* (node *) b);// A }*/ - intCMP (Node A,node b) { -     returnA.t>b.t; the } - intPRE[MAXN]; - intFindintx) { -     returnpre[x]= x==pre[x]?X:find (pre[x]); + } - intVisit[maxn],n,m,flot; + __int64 time; A voidInitial () { atmemset (Visit,0,sizeof(visit)); -memset (pre,-1,sizeof(pre)); -Time=0; flot=0; - } - voidmerge (Node a) { -     intF1,f2; in     if(pre[a.s]==-1) pre[a.s]=A.S; -     if(pre[a.e]==-1) pre[a.e]=A.E; toF1=find (A.S); f2=find (A.E); +     if(F1==F2)return; -     if(visit[f1]&&Visit[f2]) { thetime+=A.T; *flot++; $     //printf ("%d%d\n", A.S,A.E);Panax Notoginseng     } -     Else if(f1!=F2) { the         if(Visit[f1]) pre[f2]=F1; +         Elsepre[f1]=F2; A     } the } + intMain () { -     intt,temp; $scanf"%d",&T); $          while(t--){ - initial (); -scanf"%d%d",&n,&m); the              for(intI=0; i<n-1; i++){ -scanf"%d%d%d",&dt[i].s,&dt[i].e,&dt[i].t);Wuyi             } the             ///qsort (dt,n-1,sizeof (dt[0]), CMP); -Sort (dt,dt+n-1, CMP); Wu              for(intI=0; i<m;i++){ -scanf"%d",&temp); Aboutvisit[temp]=1; $             } -              for(intI=0; i<n-1; i++){ - merge (Dt[i]); -                 if(flot==m-1) Break; A             } +printf"%i64d\n", time); the         } -     return 0; $}

Matrix (similar to Kruskal)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.