Ice_cream’s world III(prime)

Ice_cream’s world III

Time Limit : 3000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 6   Accepted Submission(s) : 3
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
题解:
联通所有的路的最小代价;不连通就输出impossible
prime代码:
 1 #include<stdio.h>
 2 #include<string.h>
 3 const int INF=0x3f3f3f3f;
 4 const int MAXN=1010;
 5 int N,M,answer;
 6 int map[MAXN][MAXN],low[MAXN],vis[MAXN];
 7 void prime(){
 8         int k,flot=1,temp;
 9         memset(vis,0,sizeof(vis));
10         vis[0]=1;
11         for(int i=0;i<N;i++)
12             low[i]=map[0][i];
13         for(int i=0;i<N;i++){
14             temp=INF;
15             for(int j=0;j<N;j++)
16                 if(!vis[j]&&temp>low[j])temp=low[k=j];
17             if(temp==INF){
18                 if(flot==N)printf("%d\n",answer);
19                 else puts("impossible");
20                 break;
21             }
22             answer+=temp;
23             vis[k]=1;
24             flot++;
25             for(int j=0;j<N;j++)
26                 if(!vis[j]&&low[j]>map[k][j])
27                 low[j]=map[k][j];
28         }
29 }
30 int main(){
31     int a,b,c;
32     while(~scanf("%d%d",&N,&M)){answer=0;
33         memset(map,INF,sizeof(map));
34         while(M--){
35             scanf("%d%d%d",&a,&b,&c);
36             if(c<map[a][b])
37                 map[a][b]=map[b][a]=c;
38         }
39         prime();
40         puts("");
41     }
42 return 0;
43 }

 

posted @ 2015-08-12 18:54  handsomecui  阅读(359)  评论(0编辑  收藏  举报