USACO 2016 Feb Fenced In 最小生成树

Farmer John has realized that many of his cows are strangely agoraphobic (being fearful of large open spaces). To try and make them less afraid of grazing, he partitions his large field into a number of smaller regions by building vertical (north-south) and horizontal (east-west) fences.

The large field is a rectangle with corner points at  (0,0) (0,0) and  (A,B) (A,B). FJ builds  n n vertical fences ( 0n25,000 0≤n≤25,000) at distinct locations  a1an a1…an ( 0<ai<A 0<ai<A); each fence runs from  (ai,0) (ai,0) to  (ai,B) (ai,B). He also builds  m m horizontal fences ( 0m25,000 0≤m≤25,000) at locations  b1bm b1…bm ( 0<bi<B 0<bi<B); each such fence runs from  (0,bi) (0,bi) to  (A,bi) (A,bi). Each vertical fence crosses through each horizontal fence, subdividing the large field into a total of  (n+1)(m+1) (n+1)(m+1) regions.

Unfortunately, FJ completely forgot to build gates into his fences, making it impossible for cows to leave their enclosing region and travel around the entire field! He wants to remedy this situation by removing pieces of some of his fences to allow cows to travel between adjacent regions. He wants to select certain pairs of adjacent regions and remove the entire length of fence separating them; afterwards, he wants cows to be able to wander through these openings so they can travel anywhere in his larger field.

For example, FJ might take a fence pattern looking like this:

+---+--+
|   |  |
+---+--+
|   |  |  
|   |  |
+---+--+

and open it up like so:

+---+--+
|      |  
+---+  +  
|      |  
|      |
+---+--+

Please help FJ determine the minimum total length of fencing he must remove to accomplish his goal.

INPUT FORMAT (file fencedin.in):
The first line of input contains  A A B B n n, and  m m ( 1A,B1,000,000,000 1≤A,B≤1,000,000,000). The next  n n lines contain  a1an a1…an, and the next  m m lines after that contain  b1bm b1…bm.

OUTPUT FORMAT (file fencedin.out):
Please write the minimum length of fencing FJ must remove. Note that this might be too large to fit into a standard 32-bit integer, so you may need to use 64-bit integer types (e.g., "long long" in C/C++).

SAMPLE INPUT:
15 15 5 2
2
5
10
6
4
11
3
SAMPLE OUTPUT:
44

题意:求最少需要去掉多少长度的栅栏使得每一块都连通


题解:我们可以把每个块看成一个点  那么这道题就变成了最小生成树

但由于数据太大  我们不能建图

假设我们 x 轴删除了 xx 次   y轴删除了 yy 次

在开始时  xx=yy=0

那么第一次删除肯定是删除 m个最小的或者n个最小的  因为要把一列或者一行都联通

我们还可以发现  只要 xx 或者 yy 任意一个为0  那么一行或者一列的栅栏就都要删除

当都不为0时  假设我们接下来要删除 x 轴的  因为已经有 yy 列联通了   所以我们就可以少删除 yy 个  那么就是删除  m+1-yy 个

同理  如果要删除 y 轴的  我们删除 n+1-xx 个即可


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
struct node{
	ll num,lab;
	bool operator <(const node& a)const{
		return num<a.num;
	}
}e[50005];
ll aa[25005],bb[25005];
int main(){
	ll a,b,n,m,i,j,mis=0;
	scanf("%lld%lld%lld%lld",&a,&b,&n,&m);
	for(i=1;i<=n;i++){
		scanf("%lld",&aa[i]);
	}
	aa[n+1]=a;
	sort(aa,aa+1+n);
	for(i=0;i<=n;i++)aa[i]=aa[i+1]-aa[i];
	for(i=1;i<=m;i++){
		scanf("%lld",&bb[i]);
	}
	bb[m+1]=b;
	sort(bb,bb+1+m);
	for(i=0;i<=m;i++)bb[i]=bb[i+1]-bb[i];
	ll cnt=0;
	for(i=0;i<=n;i++){
		e[++cnt].num=aa[i];
		e[cnt].lab=1;
	}
	for(i=0;i<=m;i++){
		e[++cnt].num=bb[i];
		e[cnt].lab=0;
	}
	sort(e+1,e+1+n+m+2);
	ll ans=0;
	ll xx=0,yy=0,res=(n+1)*(m+1)-1;
	for(i=1;i<=n+m+2;i++){
		if(e[i].lab){
			if(xx==0||yy==0)ans+=e[i].num*m;
			else ans+=e[i].num*(m+1-yy);
			xx++;
		}
		else{
			if(xx==0||yy==0)ans+=e[i].num*n;
			else ans+=e[i].num*(n+1-xx);
			yy++;
		}
	}
	cout<<ans<<endl;
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值