标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 653 Accepted Submission(s): 268
1 #include <iostream> 2 #include <cstdio> 3 #include <string> 4 #include <queue> 5 #include <vector> 6 #include <map> 7 #include <algorithm> 8 #include <cstring> 9 #include <cctype> 10 #include <cstdlib> 11 #include <cmath> 12 #include <ctime> 13 using namespace std; 14 15 const int SIZE = 1005; 16 struct Node 17 { 18 int from,to,cost; 19 }G[SIZE * SIZE * 2]; 20 int MAP[SIZE][SIZE],FATHER[SIZE * SIZE]; 21 int N,M,NUM; 22 23 void ini(void); 24 int find_father(int); 25 void unite(int,int); 26 bool same(int,int); 27 bool comp(const Node &,const Node &); 28 long long kruskal(void); 29 int main(void) 30 { 31 int t; 32 int count = 0; 33 34 scanf("%d",&t); 35 while(t --) 36 { 37 count ++; 38 scanf("%d%d",&N,&M); 39 ini(); 40 for(int i = 1;i <= N;i ++) 41 for(int j = 1;j <= M;j ++) 42 scanf("%d",&MAP[i][j]); 43 for(int i = 1;i <= N;i ++) 44 for(int j = 1;j <= M;j ++) 45 { 46 if(j + 1 <= M) 47 { 48 G[NUM].from = (i - 1) * M + j; 49 G[NUM].to = (i - 1) * M + j + 1; 50 G[NUM].cost = abs(MAP[i][j] - MAP[i][j + 1]); 51 NUM ++; 52 } 53 if(i + 1 <= N) 54 { 55 G[NUM].from = (i - 1) * M + j; 56 G[NUM].to = (i) * M + j; 57 G[NUM].cost = abs(MAP[i][j] - MAP[i + 1][j]); 58 NUM ++; 59 } 60 } 61 printf("Case #%d:\n%lld\n",count,kruskal()); 62 } 63 64 return 0; 65 } 66 67 void ini(void) 68 { 69 NUM = 0; 70 for(int i = 1;i <= N * M;i ++) 71 FATHER[i] = i; 72 } 73 74 int find_father(int n) 75 { 76 if(n == FATHER[n]) 77 return n; 78 return FATHER[n] = find_father(FATHER[n]); 79 } 80 81 void unite(int x,int y) 82 { 83 x = find_father(x); 84 y = find_father(y); 85 86 if(x == y) 87 return ; 88 FATHER[x] = y; 89 } 90 91 bool same(int x,int y) 92 { 93 return find_father(x) == find_father(y); 94 } 95 96 bool comp(const Node & a,const Node & b) 97 { 98 return a.cost < b.cost; 99 } 100 101 long long kruskal(void) 102 { 103 long long ans = 0; 104 int count = 0; 105 106 sort(G,G + NUM,comp); 107 for(int i = 0;i < NUM;i ++) 108 if(!same(G[i].from,G[i].to)) 109 { 110 ans += G[i].cost; 111 unite(G[i].from,G[i].to); 112 if(count == N * M - 1) 113 break; 114 } 115 return ans; 116 }
标签:
原文地址:http://www.cnblogs.com/xz816111/p/4546968.html