洛谷P3073 Tractor S

题目描述

One of Farmer John's fields is particularly hilly, and he wants to purchase a new tractor to drive around on it. The field is described by an N x N grid of non-negative integer elevations (1 <= N <= 500). A tractor capable of moving from one grid cell to an adjacent cell (one step north, east, south, or west) of height difference D costs exactly D units of money.

FJ would like to pay enough for his tractor so that, starting from some grid cell in his field, he can successfully drive the tractor around to visit at least half the grid cells in the field (if the number of total cells in the field is odd, he wants to visit at least half the cells rounded up). Please help him compute the minimum cost necessary for buying a tractor capable of this task.

输入格式

* Line 1: The value of N.

* Lines 2..1+N: Each line contains N space-separated non-negative integers (each at most 1 million) specifying a row of FJ's field.

输出格式

* Line 1: The minimum cost of a tractor that is capable of driving around at least half of FJ's field.

题意翻译

题目描述

FJ有块农田太崎岖了,他要买一辆新拖拉机才能在这里巡视。这块农田由N x N个格子的非负整数表示高度(1<=N<=500)。拖拉机从当前格子走到相邻格子(东、南、西、北四个方向)的代价为高度差D,则FJ驶过这两个格子的拖拉机最少也要值D块钱。

FJ愿意花足够的钱买一辆新的拖拉机使得他能以最小的高度差走遍所有格子的一半(如果格子总数是奇数,那么一半的值为四舍五入的值)。因为FJ很懒,所以他找到你帮他编程计算他最小需要花多少钱买到符合这些要求的拖拉机。

输入输出格式

输入格式:

第一行为一个整数N

第2到N+1行每行包含N个非负整数(不超过1,000,000),表示当前格子的高度。

输出格式:

共一行,表示FJ买拖拉机要花的最小价钱。

输入输出样例

输入 #1复制

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 

输出 #1复制

3 

上代码:

#include<bits/stdc++.h>
using namespace std;
const int maxn=501;
int n;
int mp[maxn][maxn];
inline int read(){//快读
	int x=0,y=1;
	char ch=getchar();
	while(!isdigit(ch)){if(ch=='-')y=-y; ch=getchar();}
	while(isdigit(ch)){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}
	return x*y;
}
int fa[maxn*maxn];
int siz[maxn*maxn];
struct node{
	int u,v,w;
	node(){}
	node(int uu,int vv,int ww){
		u=uu;
		v=vv;
		w=ww;
	}
}edge[maxn*maxn*4];
bool cmp(const node &a,const node &b){
	return a.w<b.w;
}
int ans=0;
void init(){
	for(int i=1;i<=n*n;i++){
		fa[i]=i;
		siz[i]=1;
	}
}
int find(int x){
	if(x==fa[x]) return x;
	else return fa[x]=find(fa[x]);
}
int ex(int i,int j){
	return (i-1)*n+j;
}
int main(){
	n=read();
	for(int i=1;i<=n;++i){
		for(int j=1;j<=n;++j){
			mp[i][j]=read();
		}
	}
	init();
	int tp=1;
	for(int i=1;i<=n;++i){
		for(int j=1;j<=n;++j){
			if(i>1){
				edge[tp]=node(ex(i,j),ex(i-1,j),abs(mp[i-1][j]-mp[i][j]));
				tp++;
			}
			if(j>1){
				edge[tp]=node(ex(i,j),ex(i,j-1),abs(mp[i][j-1]-mp[i][j]));
				tp++;
			}
			if(i<n){
				edge[tp]=node(ex(i,j),ex(i+1,j),abs(mp[i+1][j]-mp[i][j]));
				tp++;
			}
			if(j<n){
				edge[tp]=node(ex(i,j),ex(i,j+1),abs(mp[i][j+1]-mp[i][j]));
				tp++;
			}
		}
	}
	sort(edge+1,edge+tp,cmp);
	for(int i=1;i<tp;i++){
		int x=edge[i].u;
		int y=edge[i].v;
		int w=edge[i].w;
		x=find(x);
	    y=find(y);
	    if(x!=y){
		    fa[x]=y;
		    siz[y]+=siz[x];
		    if(siz[y]>=(n*n+1)/2){
			    cout<<w<<endl;
			    return 0;
		    }
	    }
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值