Luo Gu P3073 [USACO13FEB] Tractor Tractor

Luo Gu P3073 [USACO13FEB] Tractor Tractor

Description

  • There are too rugged FJ block farmland, and he wants to buy a new tractor to patrol here. This represents the height of farmland (1 <= N <= 500) by a non-negative integer number N x N grid. Tractors come from the current grid adjacent to the grid (east, south, west and north direction) price for the height difference D, the FJ passing tractor least two lattice D dollars have value.

    FJ willing to spend enough money to buy a new tractor so that he traveled half (if the total number of lattice is odd, then the value of half of the value of rounding) all the grid with minimal height difference. Because FJ lazy, so he helped him find the program you need to calculate how much money to buy his youngest tractor to comply with these requirements.

Input

  • Conduct a first integer N

    2 to N + 1 lines each comprising N non-negative integers (not exceeding 1,000,000), indicates the current height of the lattice.

Output

  • Total row, represents the minimum price of FJ to buy a tractor to spend.

Sample Input

5 
0 0 0 3 3 
0 0 0 0 3 
0 9 9 3 3 
9 9 9 3 3 
9 9 9 9 3 

Sample Output

3 

answer:

  • Minimum spanning tree.
  • First, each grid lattice around it are related to the construction side. Then the right side in ascending order sequentially added. After each addition of an edge, to determine whether there are elements of the collection of more than half of the number of lattice. If it does, the direct output of this edge to the right.
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#define N 505
#define M (500 * 500 * 2)
using namespace std;

struct E {int u, v, w;} e[M];
int n, tot, cnt, ans;
int fat[N * N], size[N * N];
int a[N][N];

int cal(int x, int y) {return (x - 1) * n + y;}
bool cmp(E x, E y) {return x.w < y.w;}

int getFat(int x)
{
    if(x == fat[x]) return x;
    return fat[x] = getFat(fat[x]);
}

int main()
{
    cin >> n, tot = n * n / 2;
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= n; j++)
            scanf("%d", &a[i][j]);
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= n; j++)
        {
            int x = i, y = j + 1;
            if(x >= 1 && x <= n && y >= 1 && y <= n)
            {
                e[++cnt].u = cal(i, j);
                e[cnt].v = cal(x, y);
                e[cnt].w = abs(a[i][j] - a[x][y]);
            }
            x = i + 1, y = j;
            if(x >= 1 && x <= n && y >= 1 && y <= n)
            {
                e[++cnt].u = cal(i, j);
                e[cnt].v = cal(x, y);
                e[cnt].w = abs(a[i][j] - a[x][y]);
            }
        }
    for(int i = 1; i <= n * n; i++)
        fat[i] = i, size[i] = 1;
    sort(e + 1, e + 1 + cnt, cmp);
    for(int i = 1; i <= cnt; i++)
    {
        int fx = getFat(e[i].u), fy = getFat(e[i].v);
        if(fx == fy) continue;
        if(size[fx] > size[fy]) swap(fx, fy);
        size[fy] += size[fx], fat[fx] = fy;
        if(size[fy] >= tot) {cout << e[i].w; break;}
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/BigYellowDog/p/11505418.html