Connect the Cities (prim) with Prim can be timed out, handed over 20

Source: Internet
Author: User

Connect the Cities
Time limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 701 Accepted Submission (s): 212
Problem DescriptionIn 2100, since the sea level rise, most of the cities disappear. Though Some survived cities is still connected with others, but most of them become disconnected. The government wants to build some roads to connect all of these cities again, but they don't want to take too much.
Input The first line contains the number of test cases.
Each test case is starts with three integers:n, M and K. N (3 <= n <=500) stands for the number of survived cities, m (0 <= M <= 25000) stands for the number of roads you can choose to connect the cities and K (0 <= K <=) St ANDs for the number of still connected cities.
To make it easy, the cities is signed from 1 to N.
Then follow m lines, each contains three integers p, Q and C (0 <= C <=), means it takes C to connect P and Q.
Then follow k lines, each line starts with a integer t (2 <= t <= N) stands for the number of this connected cities . Then T integers follow stands for the ID of these cities.
Outputfor each case, the output of the least money is need to take, if it's impossible, just output-1.
Sample Input
16 4 31 4 22 6 12 3 53 4 332 1 22 1 33 4 5 6
Sample Output
1
Authordandelion
Sourcehdoj Monthly contest–2010.04.04
Recommendlcy
/*dot variable less time with Kliscal algorithm, point less edge when with prim algorithm heart tired, handed over 20 issued by the*/#include<bits/stdc++.h>#defineN 550#defineINF 0x3f3f3f3fusing namespacestd;/********* input/output plug ************/Template<classT>inlineBOOLScan_d (T &ret) {    CharC; intSGN; if(C=getchar (), c==EOF)return 0;//EOF     while(c!='-'&& (c<'0'|| C>'9')) C=GetChar (); SGN= (c=='-')?-1:1; RET= (c=='-')?0:(C-'0');  while(C=getchar (), c>='0'&&c<='9') ret=ret*Ten+ (C-'0'); RET*=SGN; return 1;} Inlinevoid  out(intx) {    if(x>9)         out(x/Ten); Putchar (x%Ten+'0');}/********* input/output plug ************/intT;intn,m,k;intmapn[550][550];//used to build diagramsBOOLvis[550];//used to record the point of Access .intd[550];//indicates the minimum distance from the selected node to the current node.intx,y,val,q,rt[550];voidinit () {memset (Mapn,inf,sizeofMAPN); memset (Vis,false,sizeofvis); memset (D,inf,sizeofd);}intPrimintm) {    intRoot//represents the currently selected node .    intminn=INF; Root=m; VIS[M]=true; Long LongCur=0; intlen=0;  for(intI=2; i<=n;i++)//optimization of the prim algorithm, a total of n points to find, first find the M, and then find n-1 a point is enough{Minn=INF;  for(intj=1; j<=n;j++)//and start looking for some            if(!vis[j])//this point has not been traversed.            {                if(d[j]>Mapn[root][j]) d[j]=Mapn[root][j]; if(minn>D[j]) {Minn=D[J]; m=J; }            }        if(!vis[m])//this point has not been traversed.{cur+=Minn; VIS[M]=true; Root=m; Len++; }    }    if(len==n-1)        returncur; return-1;}intMain () {//freopen ("C:\\users\\acer\\desktop\\in.txt", "R", stdin);Scan_d (t);  while(t--) {Scan_d (n); Scan_d (m); Scan_d (k); Init ();//Initialize         for(intI=0; i<m;i++{scan_d (x); Scan_d (y); Scan_d (Val); if(Mapn[x][y]>val)//The heavy side of the sentence{Mapn[x][y]=mapn[y][x]=Val; }        }         for(intI=0; i<k;i++) {scan_d (q);  for(intj=0; j<q;j++) Scan_d (Rt[j]); /*now that we're connected, there's no need to spend money on roads.*/             for(intj=0; j+1<q;j++) Mapn[rt[j]][rt[j+1]]=mapn[rt[j+1]][rt[j]]=0; } printf ("%d\n", Prim (1)); }    return 0;}

Connect the Cities (prim) with Prim can be timed out, handed over 20

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.