HDU-3371 Connect the Cities

    浓缩prim的模板

                 Connect the Cities

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 7278    Accepted Submission(s): 2079

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
 
Author
dandelion
 
Source
 
Recommend
lcy
 1 #include<stdio.h>
 2 #include<string.h>
 3 #define MAX 1000000
 4 int map[510][510];
 5 void prim(int n)//最好的prim的模板。
 6 {
 7     int k[510],v[510],i,j;
 8     int min,sum=0,flag,count=1;
 9     memset(k,0,sizeof(k));
10     memset(v,0,sizeof(v));
11     for(i=1;i<=n;i++)
12         k[i]=map[1][i];
13     for(i=2;i<=n;i++)
14     {
15         min=MAX;
16         for(j=2;j<=n;j++)
17         {
18             if(min>k[j]&&v[j]==0)
19             {
20                 min=k[j];
21                 flag=j;
22             }
23         }
24         sum+=min;
25         v[flag]=1;
26         for(j=1;j<=n;j++)
27             if(v[j]==0&&k[j]>map[flag][j]&&flag!=j)
28                 k[j]=map[flag][j];
29     }
30     if(sum<MAX)
31         printf("%d\n",sum);
32     else
33         printf("-1\n");
34 }
35 int main()
36 {
37     int T,n,m,k,i,j,o,p,q,r,s,l;
38     int b[100];
39     scanf("%d",&T);
40     while(T--)
41     {
42         scanf("%d%d%d",&n,&m,&k);
43         for(i=1;i<=n;i++)
44             for(j=1;j<=n;j++)
45             {
46                 if(i==j) map[i][j]=0;
47                 else 
48                     map[i][j]=MAX;
49             }
50             for(i=0;i<m;i++)
51             {
52                 scanf("%d%d%d",&p,&q,&r);
53                 if(r<map[p][q])
54                 {
55                     map[p][q]=map[q][p]=r;
56                 }
57             }
58             for(i=0;i<k;i++)
59             {
60                 scanf("%d",&s);
61                 for(j=0;j<s;j++)
62                     scanf("%d",&b[j]);
63                 for(o=0;o<s;o++)
64                     for(l=o+1;l<s;l++)
65                     {
66                         map[b[o]][b[l]]=map[b[l]][b[o]]=0;
67                     }
68             }
69             prim(n);
70     }
71     return 0;
72 }

 

posted @ 2013-09-03 19:28  疯狂的癫子  阅读(263)  评论(2编辑  收藏  举报