USACO 2016 Feb Fenced In 最小生成树
2017-03-14 00:41
274 查看
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 nn vertical
fences (0≤n≤25,0000≤n≤25,000)
at distinct locations a1…ana1…an (0<ai<A0<ai<A);
each fence runs from (ai,0)(ai,0) to (ai,B)(ai,B).
He also builds mm horizontal
fences (0≤m≤25,0000≤m≤25,000)
at locations b1…bmb1…bm (0<bi<B0<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.
and mm (1≤A,B≤1,000,000,0001≤A,B≤1,000,000,000).
The next nn lines
contain a1…ana1…an,
and the next mm lines
after that contain b1…bmb1…bm.
题意:求最少需要去掉多少长度的栅栏使得每一块都连通
题解:我们可以把每个块看成一个点 那么这道题就变成了最小生成树
但由于数据太大 我们不能建图
假设我们 x 轴删除了 xx 次 y轴删除了 yy 次
在开始时 xx=yy=0
那么第一次删除肯定是删除 m个最小的或者n个最小的 因为要把一列或者一行都联通
我们还可以发现 只要 xx 或者 yy 任意一个为0 那么一行或者一列的栅栏就都要删除
当都不为0时 假设我们接下来要删除 x 轴的 因为已经有 yy 列联通了 所以我们就可以少删除 yy 个 那么就是删除 m+1-yy 个
同理 如果要删除 y 轴的 我们删除 n+1-xx 个即可
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 nn vertical
fences (0≤n≤25,0000≤n≤25,000)
at distinct locations a1…ana1…an (0<ai<A0<ai<A);
each fence runs from (ai,0)(ai,0) to (ai,B)(ai,B).
He also builds mm horizontal
fences (0≤m≤25,0000≤m≤25,000)
at locations b1…bmb1…bm (0<bi<B0<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 AA, BB, nn,and mm (1≤A,B≤1,000,000,0001≤A,B≤1,000,000,000).
The next nn lines
contain a1…ana1…an,
and the next mm lines
after that contain b1…bmb1…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; }
相关文章推荐
- 【最小生成树】【并查集】[USACO2016 金组]Fenced In
- 【最小生成树】[USACO 2016 February Contest, Gold]Fenced In
- 【bzoj3943】[Usaco2015 Feb]SuperBull 最小生成树
- bzoj4410 [Usaco2016 Feb] Fence in
- uva 10816 Travel in Desert (最小生成树 + 最短路)
- CUGB图论专场:C - Tangled in Cables(最小生成树kruscal)
- BZOJ 3479: [Usaco2014 Mar]Watering the Fields(最小生成树)
- [Usaco2008 Oct]灌水 最小生成树
- 【Usaco2016 FEB】Fenced In
- UVA - 10307 Killing Aliens in Borg Maze(最小生成树kruskal+bfs)
- uva 10816 - Travel in Desert(最小瓶颈生成树+最短路)
- USACO3.1Agrinet最小生成树
- Pku acm 2075 Tangled in Cables数据结构题目解题报告(十一)最小生成树:prim算法&amp;amp;二叉查找树
- USACO-Section 3.1 Agri-Net(最小生成树[Prim])
- UVA 10816 - Travel in Desert(最小生成树+最短路)
- 2015-2016 ACM-ICPC, NEERC, Moscow Subregional Contest G题: Garden Gathering [线段树/最小曼哈顿距离生成树]
- [usaco]Agri-Net(使用最小生成树算法)
- 最小生成树变型——[Usaco2008 Oct]灌水
- [Usaco2007 Dec]Building Roads 修建道路[最小生成树]
- 【UVA 10307 Killing Aliens in Borg Maze】最小生成树, kruscal, bfs