Kruskal connected to hdu 5253 (2015 Baidu STAR Program Design Competition, hdu5253
Connected MPs queueTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission (s): 1323 Accepted Submission (s): 519
Problem Description old Jack has a farmland which has been used for daily meals in the past few years. However, this year, we are not very excited. So Jack decided to connect all his adjacent farmland in a pipe so that he could water from a distance for irrigation. When Jack bought all the pipelines in each farmland, Jack encountered a new problem because the Terrain Height of each farmland was different, to connect the two farmland pipelines, Jack needs to purchase pipelines of the same length as the height of the two farmland.
Now, we provide data on the old Jack farmland. You need to tell the old Jack how long it will take to purchase pipelines while ensuring that all the farmland can be connected to irrigation. In addition, each farmland is square and other big, and a farmland can only be connected with four adjacent farmland upper and lower.
Enter a number in the first line of Input. T (T ≤ 10) Indicates the number of input sample groups.
The input contains several groups of test data, which are processed until the end of the file. Each group of test data occupies several rows. The first row has two positive integers. N, M (1 ≤ N, M ≤ 1000) Which indicates that the old Jack has N rows and * M columns of farmland. In the next N rows, each row has M numbers, representing the height of each farmland. The height of the farmland cannot exceed 100. Numbers are separated by spaces.
Output outputs two rows for each group of test data:
Output in the first line: "Case # I :". I Represents group I test data.
The second line outputs a positive integer, representing the length of the pipe purchased by the old Jack at least.
Sample Input
24 39 12 47 8 5632 32 4321 12 122 334 56 5612 23 4
Sample Output
Case #1:82Case #2:74
Source 2015 Baidu STAR Program Design Competition-Preliminary Round (2)
Ideas:
Minimum Spanning Tree: Calculate the shortest weight when the graph requires all connections, and use the shortest short circuit. Consider the data size if you want to use a search.
Code:
#include<iostream>#include<cstring>#include<cstdio>#include<cmath>#include<algorithm>using namespace std;struct node{ int x,y,len; bool operator < (const node &a) const { return len < a.len; }};const int N=1100;int x[N][N],m,n,k;int father[N*N];node ve[N*N*4];int Find(int x){ return x==father[x]?x:father[x]=Find(father[x]);}int Union(int x,int y){ int a=Find(x),b=Find(y); if(a!=b) { father[a]=b; return 1; } return 0;}int kruskal(){ node s; int sum=0; for(int i=0;i<k;i++) { if(Union(ve[i].x,ve[i].y)) sum+=ve[i].len; } return sum;}int main(){ int na,ca=1; scanf("%d",&na); while(na--) { k=0; scanf("%d%d",&m,&n); for(int i=0;i<m;i++) for(int j=0;j<n;j++) { node q; father[i*n+j]=i*n+j; scanf("%d",&x[i][j]); if(j>0) { q.x=i*n+j,q.y=i*n+j-1; q.len=abs(x[i][j]-x[i][j-1]); ve[k++]=q; } if(i>0) { q.x=i*n+j,q.y=i*n+j-n; q.len=abs(x[i][j]-x[i-1][j]); ve[k++]=q; } } sort(ve,ve+k); printf("Case #%d:\n",ca++); int xx=kruskal(); printf("%d\n",xx); } return 0;}
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.