HDU 3371 Kruscal/prim Find the minimum spanning tree Connect the Cities tai Hang Tai Hang

Source: Internet
Author: User

This time is shorter than 700 s#include<stdio.h> #include <string.h> #include <iostream> #include <algorithm>   using namespace std;struct node{int u;   int V; int W;} Que[100000];int father[505];bool cmp (struct node a,struct node B) {return A.W&LT;B.W;} void init (int n) {for (int i=1;i<=n;i++) father[i]=i;}    int find (int u) {if (father[u]!=u) Father[u]=find (Father[u]); return father[u];}    int main () {int t;    scanf ("%d", &t);       while (t--) {int n,m,k;       int edge=0,sum=0;       scanf ("%d%d%d", &n,&m,&k);       Init (n);       for (int i=0;i<m;i++) {scanf ("%d%d%d", &AMP;QUE[I].U,&AMP;QUE[I].V,&AMP;QUE[I].W);       } sort (que,que+m,cmp);           for (int i=1;i<=k;i++) {int cnt,root;           scanf ("%d%d", &cnt,&root);              for (int j=1;j<cnt;j++) {int x;              scanf ("%d", &x);              int C1=find (x), c2=find (root);          if (C1!=C2) {father[c1]=c2;    }}} for (int i=1;i<=n;i++) if (father[i]==i) edge++;      BOOL Flag=false;          for (int i=0;i<m;i++) {int Tx=find (QUE[I].U), Ty=find (QUE[I].V);              if (tx!=ty) {father[tx]=ty;              SUM+=QUE[I].W;          edge--;            } if (edge==1) {flag=true;          Break      }} if (flag) printf ("%d\n", sum);    else printf (" -1\n"); } return 0;}

Connect the Cities

Time Limit: MS Memory Limit: 32768 KB

64-bit integer IO format: %i64d,%i64u Java class name: Main

[Submit] [Status] [Discuss]

DescriptionIn 2100, since the sea level rise, and 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. Inputthe 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
Sourcehdoj Monthly contest–2010.04.04     The judge system absolute pit you have no words, the same code, a will hand over, after a while to hand over the time-out, Absolutely tle you don't have a temper and you don't want to say anything. In a variety of virtual OJ within the submission, but also the same problem, really no language
#include <stdio.h> #include <string.h> #include <iostream> #include <algorithm>using namespace std;struct node{int u,v,w;} Que[105000];int Father[505];bool CMP (Node A, Node B) {return A.W&LT;B.W;} void init (int n) {for (int i=1;i<=n;i++) father[i]=i;}   int find (int u) {if (father[u]!=u) {father[u]=find (father[u]); } return father[u];}   int main () {int t;   scanf ("%d", &t);      while (t--) {int n,m,k;///int sum,edge;//These variables I define in the outside will time out, can only be defined in the interior, that is also floating over, more than 980 s, good risk of good insurance int uu,vv,ww;      scanf ("%d%d%d", &n,&m,&k);      Init (n);          for (int i=0;i<m;i++) {scanf ("%d%d%d", &AMP;UU,&AMP;VV,&AMP;WW);          Que[i].u=uu;          QUE[I].V=VV;      QUE[I].W=WW;      } sort (que,que+m,cmp);      int x,zz,dd;        for (int i=1;i<=k;i++) {scanf ("%d%d", &x,&zz);              for (int j=1;j<x;j++) {scanf ("%d", &AMP;DD);              int C1=find (ZZ), C2=find (DD); if (C1!=C2)             FATHER[C2]=C1;      }} edge=0;      for (int i=1;i<=n;i++) {if (father[i]==i) edge++;    }//at this time only to judge there are several equal, one of the other meaning is to see these points divided into a few piles, it may be set to K heap, then only need to K-1 line can connect other points together, that is, other heap sum=0;   BOOL Flag=false;       for (int i=0;i<m;i++) {int a1=find (QUE[I].U);       int A2=find (QUE[I].V);              if (A1!=A2) {father[a1]=a2;              SUM+=QUE[I].W;       edge--;          } if (edge==1) {flag=true;        Break     }} if (flag) printf ("%d\n", sum);   else printf (" -1\n"); } return 0;}

HDU 3371 Kruscal/prim Find the minimum spanning tree Connect the Cities tai Hang Tai Hang

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.