HDU 5253 Connected Pipes

Source: Internet
Author: User

Topic Connection

http://acm.hdu.edu.cn/showproblem.php?pid=5253

Connected Pipe Description

Old Jack has a farmland that has been dependent on in the past few years. 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

The first line enters a number $t\ (T \leq 10) $, which represents 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 accounted for several lines, the first line two positive integers $N, M (1 \leq n,m \leq 1000) $, on behalf of old Jack has N rows *m row 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

Output 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 Input

2
4 3
9 12 4
7 8 56
32 32 43
21 12 12
2 3
34 56 56
12 23 4

Sample Output

Case #1:
+ +
Case #2:
"

#include <algorithm> #include <iostream> #include <cstdlib> #include <cstring> #include < cstdio> #include <vector> #include <queue> #include <map>using std::map;using std::min;using std:: Sort;using std::p air;using std::vector;using std::multimap;using std::p riority_queue; #define PB (E) push_back (e) # Define SZ (c) (int) (c). Size () #define MP (A, b) Make_pair (A, B) #define ALL (c) (c). Begin (), (c). End () #define ITER (c) __typeof ((c). Begin ()) #define CLS (arr, Val) memset (arr, Val, sizeof (arr)) #define Cpresent (C, E) (Find (All (c), (e))! = (c). End ()) #d Efine Rep (i, n) for (int i = 0; l < (int) n; i++) #define TR (c, I) for (ITER (c) i = (c). Begin (); I! = (c). end (); ++i) const    int N = 1100;const int INF = 0x3f3f3f3f;struct edge {int u, V, W;    inline bool operator< (const edge &e) Const {return W < E.W;        }}g[(n * N) << 1];int H, W, E, x[n][n], Par[n * n], Rank[n * n];namespace work {inline int find (int X) {        while (x! = Par[x]) {    x = par[x] = par[par[x]];    } return x;        } inline bool Unite (int x, int y) {x = find (x), y = find (y);        if (x = = y) return false;        if (Rank[x] < Rank[y]) {par[x] = y;            } else {par[y] = x;        RANK[X] + = rank[x] = = Rank[y];    } return true;        } inline int Kruskal () {Sort (g, G + E);        int res = 0;            Rep (i, E) {Edge &e = G[i];            if (Unite (E.U, E.V)) {res + = E.W;    }} return res;        } inline void init () {E = 0;                Rep (i, H) {Rep (J, W) {scanf ("%d", &x[i][j]);                int v = i * W + j;                PAR[V] = V, rank[v] = 0;                if (i) {g[e++] = (edge) {V, v-w, ABS (X[i][j]-x[i-1][j])};                } if (j) {g[e++] = (edge) {V, V-1, ABS (X[i][j]-x[i][j-1])}; }            }        }}};int Main () {#ifdef LOCAL freopen ("In.txt", "R", stdin);    Freopen ("OUT.txt", "w+", stdout); #endif int T, V, k = 1;    scanf ("%d", &t);        while (t--) {scanf ("%d%d", &h, &w);        Work::init ();        v = Work::kruskal ();    printf ("Case #%d:\n%d\n", k++, V); } return 0;}

Hdu 5253 Connected pipelines

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.