标签:des style blog http color os java io strong
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3371
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 10313 Accepted Submission(s):
2937
1 #include <iostream> 2 #include <cstdio> 3 using namespace std; 4 5 int map[1010][1010],r[1010],Min,n,sum,node[1010]; 6 const int INF=9999999; 7 8 void set() 9 { 10 for (int i=1; i<=n; i++) 11 { 12 node[i]=INF; 13 for (int j=1; j<=n; j++) 14 map[i][j]=INF; 15 } 16 } 17 18 void prim() 19 { 20 int tm=1,m; 21 int vis[1010]= {0}; 22 node[tm]=0; 23 vis[tm]=1; 24 for (int k=2; k<=n; k++) 25 { 26 Min=INF; 27 for (int i=1; i<=n; i++) 28 if (!vis[i]) 29 { 30 if (node[i]>map[tm][i]) 31 node[i]=map[tm][i]; 32 if (Min>node[i]) 33 { 34 Min=node[i]; 35 m=i; 36 } 37 } 38 tm=m; 39 sum+=Min; 40 vis[m]=1; 41 } 42 } 43 44 int main () 45 { 46 int T; 47 cin>>T; 48 while (T--) 49 { 50 sum=0; 51 int m,k; 52 scanf("%d%d%d",&n,&m,&k); 53 set(); 54 for (int i=1; i<=m; i++) 55 { 56 int p,q,c; 57 scanf("%d%d%d",&p,&q,&c); 58 if (map[p][q]>c) 59 map[p][q]=map[q][p]=c; 60 } 61 while (k--) 62 { 63 int t; 64 scanf("%d",&t); 65 for (int i=1; i<=t; i++) 66 { 67 cin>>r[i]; 68 } 69 for (int i=1; i<=t; i++) 70 { 71 for (int j=1; j<=t; j++) 72 { 73 map[r[i]][r[j]]=map[r[j]][r[i]]=0; 74 } 75 } 76 } 77 prim(); 78 if (sum<INF) 79 printf ("%d\n",sum); 80 else 81 printf ("-1\n"); 82 } 83 return 0; 84 }
标签:des style blog http color os java io strong
原文地址:http://www.cnblogs.com/qq-star/p/3941357.html