Connected Pipelines
Time limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others) total submission (s): 1961 Accepted Submission (s): 687
Problem description Old Jack has a farmland, in the past few years is dependent on. But this year the heavens are especially not eyes, drought. So old Jack decided to pipe all his neighboring farmland together so that he could divert from a distance to irrigate. When old Jack bought all the pipes laying inside every farm, old Jack had a new problem, because each farmland had a different height, so to link two of farmland, old Jack would need to buy additional pipes with the same length of height as the two farmland.
Now give the old Jack farmland data, you need to tell old Jack in the case to ensure that all farmland can be connected to irrigation, at least also need to buy more than how long pipeline. In addition, each farmland is square and so on, a farmland can only with it up and down about four blocks adjacent farmland connected.
Input first line Enter a numberT(t≤ten) , representing the number of sample groups entered
The input contains several sets of test data that are processed to the end of the file. Each group of test data occupies several rows, the first row two positive integers n,m(1≤n,m≤) , on behalf of old Jack has n row *m farmland. The next N rows, each row of M numbers, represent the height of each farmland, and the farmland height will not exceed 100. The numbers are separated by a space.
Output outputs two lines for each set of test data:
The first line of output: "Case #i:". I represents the test data for group I.
The second line outputs 1 positive integers, representing the length of the old Jack's purchase of at least one pipe.
Sample Input24 39 12 47 8 5632 32 4321 12 122 334 56 5612 23 4
Sample outputcase #1:82Case #2:74
Kruskal: Bare, because only adjacent to the connection, so only to the farmland assigned a number, and then sort the smallest spanning tree;
Just started to put the map and Kruth in the stack overflow, put in the overall good, because the global variable is heap memory, that is, as long as the maximum memory limit of OJ given, you can open a large object, but the stack memory is generally only 2 m, and there are some thread basic information and breakpoint information, So the actual usable stack memory is less than 2m2m, the int array is open at most 5e5.
---Ming xin seniors 2016/4/8 10:45:58
Code:
#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespacestd;Const intMAXN =1010;intdisx[2] = {0,-1};intdisy[2] = {-1,0};structnode{intx, y, V; FriendBOOL operator<(Node A, Node B) {returnA.V <B.V; }}; Node DT[MAXN* MAXN *2];intMP[MAXN][MAXN];intHSH[MAXN][MAXN];classmap{Private: intN, M; Public: Map () {N=0; M=0; } Map (intNintM) { This->n =N; This->m =M; intTP =0; for(inti =1; I <= N; i++){ for(intj =1; J <= M; J + +) {scanf ("%d",&Mp[i][j]); HSH[I][J]= ++TP; } } } voidGETDT (int&TP) { //cout << N << "" << M << Endl; for(inti =1; I <= N; i++){ for(intj =1; J <= M; J + +){ for(intK =0; K <2; k++){ intNX, NY; NX= i +Disx[k]; NY= j +Disy[k]; if(NX <=0|| NY <=0|| NX > N | | NY >M)Continue; dt[tp].x=Hsh[nx][ny]; Dt[tp].y=Hsh[i][j]; DT[TP].V= ABS (Mp[nx][ny]-Mp[i][j]); TP++; } } } }};intPRE[MAXN *MAXN];classkruth{Private: Public: Kruth () { for(inti =1; I <= MAXN * MAXN; i++) {Pre[i]=i; } } intFindintR) { intx =R; while(r! =Pre[r]) {R=Pre[r]; } inti =X, J; while(I! =R) {J=Pre[i]; Pre[i]=R; I=J; } returnR; } voidMergeintXintYintVint&ans) { intF1 = Find (x), F2 =find (y); if(F1! =F2) {PRE[F1]=F2; Ans+=v; } }};intMain () {intT; intKase =0; CIN>>T; while(t--){ intN, M; CIN>> N >>M; Map A (n,m); intTP =0; intAns =0; A.GETDT (TP); Sort (dt, DT+TP); Kruth Kru; for(inti =0; i < TP; i++) {Kru.merge (dt[i].x, Dt[i].y, dt[i].v, ans); } printf ("Case #%d:\n%d\n",++Kase,ans); } return 0;}
Connected pipelines (minimum spanning tree)