Connect the Cities(hdu 3371)と検証セット(テストデータ付)

16745 ワード

Connect the Cities
Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 7338    Accepted Submission(s): 2093
Problem Description
In 2100, since the sea level rise, most of the cities disappear. Though some survived cities are 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 money. 
 
Input
The first line contains the number of test cases. Each test case 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 <= 100) stands for the number of still connected cities. To make it easy, the cities are signed from 1 to n. Then follow m lines, each contains three integers p, q and c (0 <= c <= 1000), means it takes c to connect p and q. Then follow k lines, each line starts with an integer t (2 <= t <= n) stands for the number of this connected cities. Then t integers follow stands for the id of these cities.
 
Output
For each case, output the least money you need to take, if it’s impossible, just output -1.
 
Sample Input
1
6 4 3
1 4 2
2 6 1
2 3 5
3 4 33
2 1 2
2 1 3
3 4 5 6
 
Sample Output
1
 
 
この問題の私の構想は:まずk個の数を処理して、それから更にそして集を調べて残りの連通していないことを求めます.前に並べ替えて、最後に連通しているかどうかをチェックします...
 
╮(╯▽╰)╭  自分は何度もタイムアウトして、それから、题意がおろそかになって、また何度もWAして、、、悲剧.
 
ps: http://acm.hdu.edu.cn/showproblem.php?pid=3371
#include <iostream>

#include <cstdlib>

#include<algorithm>

using namespace std;



int father[560],Q;



struct sum

{

    int a;

    int b;

    int c;

}num[50005];    //   





bool cmp(const sum &x,const sum &y)  //         ,

{

    return x.c<y.c;//      

}



int Find(int x)  //    

{

    while(x!=father[x])

        x=father[x]; 

    return x;

}



void Union(int a,int b,int i)

{

    if(a!=b)

    {

        father[a]=b;

        Q+=num[i].c;  //           

    }

}



int main()

{

    int T,k,n,m,i,j,l,p,q,c,t;

    int ss[505];

    scanf("%d",&T);

    while(T--)

    {

        scanf("%d%d%d",&n,&m,&k);

        for(i=1;i<=n;i++)

            father[i]=i;

        for(i=0;i<m;i++)

            scanf("%d%d%d",&num[i].a,&num[i].b,&num[i].c);

        memset(ss,0,sizeof(ss));

        for(l=0;l<k;l++)

        {

            scanf("%d",&t);

            for(j=0;j<t;j++)

                scanf("%d",&ss[j]);

            for(j=1;j<t;j++)

            {    

                if(Find(ss[0])!=Find(ss[j]))

                    father[Find(ss[j])]=Find(ss[0]);

            }

            memset(ss,0,sizeof(ss));

        }

        sort(num,num+m,cmp);//  

        for(i=0,Q=0;i<m;i++)

        {

                Union(Find(num[i].a),Find(num[i].b),i);

        }

        for(i=1,t=0;t<2&&i<=n;i++)

            if(father[i]==i)

                t++;

        if(t==2)

            printf("-1
"); else printf("%d
",Q); } return 0; } /* 5 6 4 3 1 4 2 2 6 1 2 3 5 3 4 33 2 1 2 2 1 3 3 4 5 6 6 4 3 1 4 3 2 6 2 6 2 1 3 4 33 2 1 2 2 1 3 3 4 5 6 6 4 3 1 4 2 2 6 1 2 3 5 3 4 33 2 1 2 2 2 3 2 4 5 6 4 3 1 4 3 2 6 2 6 2 1 3 4 33 2 1 2 2 1 3 2 4 6*/

 
ネットでKruskal関数を探しましたが、   勉強しなければなりません!
 
#include<stdio.h>           //Kruskal  

#include<algorithm>

using namespace std;



typedef struct{

        int u;

        int v;

        int w;

}Edge;



const int EdgeNum=50010;

const int PointNum=510;



Edge E[EdgeNum];

int P[PointNum];



int union_find(int x) //    

{

        return P[x]==x? x : P[x]=union_find(P[x]);

}



bool cmp(Edge a,Edge b){ return a.w<b.w; }



int MST_Kruskal(int n,int m) //        

{

        int i,j,x,y,k=1,sum=0;

        for(i=0;i<n;++i)

        {

                for(j=0;j<n;++j)

                {

                        if(i==j) continue;

                        if(P[j]==i)

                        {

                                k++;

                                // printf("P[%d]=%d
",j,i);
} } } // printf("k=%d
",k);
sort(E,E+m,cmp); for(i=0;k<n&&i<m;++i) { x=union_find(E[i].u); y=union_find(E[i].v); if(x!=y) { sum+=E[i].w; P[x]=y; k++; } } if(k<n) return -1; return sum; } int f[510]; int solve(int n,int m,int k) { int i,j,t; for(i=0;i<n;++i) { // P[i]=i; } for(i=0;i<k;++i){ scanf("%d",&t); for(j=0;j<t;++j) { scanf("%d",&f[j]); f[j]--; } for(j=1;j<t;++j) P[union_find(f[j])]=union_find(f[j-1]); } return MST_Kruskal(n,m); } int main() { int t,n,m,k,i,p,q,c,ans; scanf("%d",&t); while(t--) { scanf("%d%d%d",&n,&m,&k); for(i=0;i<m;++i) { scanf("%d%d%d",&p,&q,&c); p--;q--; E[i].u=p;E[i].v=q;E[i].w=c; } ans=solve(n,m,k); printf("%d
",ans); } return 0; }