标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 17541 Accepted Submission(s): 7557
1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 using namespace std; 5 6 const int INF = 0x3f3f3f3f; 7 int map[110][110], vis[110], dis[110]; 8 int i, j, n; 9 10 void Prime() 11 { 12 int sum = 0; 13 memset(vis, 0, sizeof(vis)); 14 for(i=1; i<=n; i++) 15 dis[i]=map[1][i]; 16 vis[1]=1; 17 for(i=1; i<n; i++) 18 { 19 int temp, min=INF; 20 for(j=1; j<=n; j++) 21 { 22 if(!vis[j] && dis[j] < min) 23 { 24 min = dis[j]; 25 temp = j; 26 } 27 } 28 vis[temp]=1; 29 sum += min; 30 for(j=1; j<=n; j++) 31 if(!vis[j] && dis[j] > map[temp][j]) 32 dis[j]=map[temp][j]; 33 } 34 printf("%d\n", sum); 35 } 36 int main() 37 { 38 while(~scanf("%d", &n), n) 39 { 40 for(i=1; i<=n; i++) 41 for(j=1; j<=n; j++) 42 map[i][j]=(i==j?0:INF); 43 int u, v, w, p, t; t = n*(n-1)/2; 44 for(i=1; i<=t; i++){ 45 scanf("%d %d %d %d", &u, &v, &w, &p); 46 if(p==1) 47 map[u][v]=map[v][u]=0; 48 else //if(map[u][v] > w) 49 map[u][v]=map[v][u]=w; 50 } 51 Prime(); 52 } 53 return 0; 54 }
标签:
原文地址:http://www.cnblogs.com/fengshun/p/4693046.html