Connected Pipelines
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 number tNumber of sample groups to represent input
The input contains several sets of test data that are processed to the end of the file. Each group of test data accounted for several lines, the first row of two positive integers n,m,N,M (1≤ N, M≤ On behalf of old Jack there are n rows of *m to list 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
1#include <cstdio>2#include <queue>3#include <algorithm>4#include <cstring>5#include <cmath>6 using namespacestd;7 8 intg[1005][1005];9 intvis[1005][1005];Ten intd[4][2]={1,0,0,1,-1,0,0,-1}; One intM,n; A Const intinf=0x3f3f3f3f; - - intCalintX1,intY1,intX2,inty2) the { - returnABS (g[x1][y1]-G[x2][y2]); - }; - + structLowercost - { + intx, y; A intCost ; at BOOL operator< (ConstLowercost &temp)Const - { - returnCost>Temp.cost; - } - }; - in voidInit () - { tomemset (Vis,0,sizeof(Vis)); + } - the intPrim () * { $Priority_queue<lowercost>Q;Panax Notoginseng lowercost t1,t2; - intsum=0, i,cnt; thet1.x=t1.y=t1.cost=0; + Q.push (t1); ACnt=0; the while(!q.empty ()) + { -t1=q.top (); $ Q.pop (); $ if(Vis[t1.x][t1.y]) - Continue; -vis[t1.x][t1.y]=1; thesum+=T1.cost; - for(i=0;i<4; i++)Wuyi { thet2.x=t1.x+d[i][0]; -t2.y=t1.y+d[i][1]; Wut2.cost=cal (T1.X,T1.Y,T2.X,T2.Y); - if(t2.x>=0&&t2.x<m&&t2.y>=0&&t2.y<n&&!Vis[t2.x][t2.y]) About Q.push (T2); $ } - } - returnsum; - } A + intMain () the { - intt,i; $scanf"%d",&t); the for(i=1; i<=t;i++) the { the init (); thescanf"%d%d",&m,&n); - for(intI=0; i<m;i++) in for(intj=0; j<n;j++) thescanf"%d",&g[i][j]); the intans=Prim (); Aboutprintf"Case #%d:\n", i); theprintf"%d\n", ans); the } the return 0; +}
HDU 5253 connected Pipeline (prim+ priority queue)