标签:
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 25002 | Accepted: 11533 |
Description
Input
Output
Sample Input
1 3 0 990 692 990 0 179 692 179 0
Sample Output
692
Hint
Source
1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 using namespace std; 5 6 const int INF = 0x3f3f3f3f; 7 int map[550][550], dis[550]; bool vis[550]; 8 int n; 9 10 void Prim() 11 { 12 memset(vis, true, sizeof(vis)); 13 for(int i = 1; i <= n; i++) 14 dis[i] = map[1][i]; 15 vis[1] = false; int max = -INF; 16 for(int i = 1; i < n; i++) 17 { 18 int temp, min = INF; 19 for(int j = 1; j <= n; j++) //寻找吓一条you最小权值边。 20 { 21 if(vis[j] && dis[j] < min) 22 { 23 temp = j; 24 min = dis[j]; 25 } 26 } 27 if(min > max) 28 max = min; 29 vis[temp] = false; 30 for(int 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", max); 35 } 36 int main() 37 { 38 int t; 39 scanf("%d", &t); 40 while(t--) 41 { 42 scanf("%d", &n); 43 for(int i = 1; i <= n; i++) 44 for(int j = 1; j <= n; j++) 45 cin >> map[i][j]; 46 Prim(); 47 } 48 return 0; 49 }
Kl ;
1 #include <cmath> 2 #include <cstdio> 3 #include <cstring> 4 #include <iostream> 5 #include <algorithm> 6 using namespace std; 7 8 const int INF = 0x3f3f3f3f; 9 int map[550][550], father[550]; 10 int n; 11 struct rode 12 { 13 int a, b, w; 14 } num[250000]; 15 16 bool cmp(rode a, rode b) 17 { 18 return a.w < b.w; 19 } 20 21 void init() 22 { 23 for(int i = 1; i <= n; i++) 24 father[i] = i; 25 } 26 27 int find(int a) 28 { 29 int r, j , k; 30 r = a; 31 while(r != father[r]) 32 r = father[r]; 33 j = a; 34 while(j != r) 35 { 36 k = father[j]; 37 father[j] = r; 38 j = k; 39 } 40 return r; 41 } 42 43 bool mercy(int a, int b) 44 { 45 int q = find(a); 46 int p = find(b); 47 if(q != p) 48 { 49 father[q] = p; 50 return true; 51 } 52 else 53 return false; 54 } 55 56 int main() 57 { 58 int t; 59 while(~scanf("%d", &t)) 60 { 61 while(t--) 62 { 63 int k = 0; 64 scanf("%d", &n); 65 init(); 66 for(int i = 1; i <= n; i++) 67 for(int j = 1; j <= n; j++) 68 { 69 cin >> map[i][j]; 70 if(i != j) 71 {num[k].a = i; num[k].b = j; num[k++].w = map[i][j];} 72 } 73 sort(num, num+k, cmp); 74 int max = -INF; 75 for(int i = 0; i < k; i++) 76 { 77 if(mercy(num[i].a, num[i].b)) 78 { 79 if(num[i].w > max) 80 max = num[i].w; 81 } 82 } 83 printf("%d\n", max); 84 } 85 } 86 return 0; 87 }
标签:
原文地址:http://www.cnblogs.com/fengshun/p/4725485.html