Ice_cream’s world III--2122

Ice_cream’s world III

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 576    Accepted Submission(s): 185


Problem Description
ice_cream’s world becomes stronger and stronger; every road is built as undirected. The queen enjoys traveling around her world; the queen’s requirement is like II problem, beautifies the roads, by which there are some ways from every city to the capital. The project’s cost should be as less as better.
 

 

Input
Every case have two integers N and M (N<=1000, M<=10000) meaning N cities and M roads, the cities numbered 0…N-1, following N lines, each line contain three integers S, T and C, meaning S connected with T have a road will cost C.
 

 

Output
If Wiskey can’t satisfy the queen’s requirement, you must be output “impossible”, otherwise, print the minimum cost in this project. After every case print one blank.
 

 

Sample Input
2 1
0 1
10
4 0
 

 

Sample Output
10
impossible
 
思路:prime,考虑重边!!!
AC代码:
 1 #include<stdio.h>
 2 #include<string.h>
 3 int map[1001][1001],dist[1001];
 4 int vis[1001],n;
 5 void init1()
 6 {
 7     int i,j;
 8     for(i = 0;i < n;i ++)
 9     {
10         for(j = 0;j < n;j ++)
11         {
12             if(i != j)
13                 map[i][j] = 1 << 30;
14         }
15     }
16 }
17 void init2()
18 {
19     int i;
20     memset(vis,0,sizeof(vis));
21     for(i = 0;i < n;i ++)
22         dist[i] = map[0][i];
23 }
24 int main()
25 {
26     int m,i,j,k,cnt;
27     int min,len,sum,a,b;
28     while(~scanf("%d%d",&n,&m))
29     {
30         init1();
31         sum = cnt = 0;
32         while(m--)
33         {
34             scanf("%d%d%d",&a,&b,&len);
35             if(len < map[a][b])
36                 map[a][b] = map[b][a] = len;
37         }
38         init2();
39         vis[0] = 1;
40         for(i = 0;i < n;i ++)
41         {
42             min = 1 << 30;
43             for(j = 0;j < n;j ++)
44             {
45                 if(!vis[j] && min > dist[j])
46                 {
47                     min = dist[j];
48                     k = j;
49                 }
50             }
51             vis[k] = 1;
52             if(min != 1 << 30)
53             {
54                 cnt++;
55                 sum += min;
56             }
57             for(j = 0;j < n;j ++)
58             {
59                 if(!vis[j] && dist[j] > map[k][j])
60                     dist[j] = map[k][j];
61             }
62         }
63         if(cnt == n-1)
64             printf("%d

",sum);
65         else
66             printf("impossible

");
67     }
68     return 0;
69 }
原文地址:https://www.cnblogs.com/anhuizhiye/p/3377254.html