考试题 T2

题意分析
首先 要求起点终点不连通
再结合数据范围 就是最小割了
首先我们可以建一个图出来
如果\(x\)可以到\(y\)的话
那么我们就从\(x\)向\(y\)连一条代价为\(h[x]-h[y]+1\)的边 代表不联通的代价
可是如果存在以下情况呢

如果我们选择切断\(c\)到\(d\)的边的话
实际上我们也切断了\(a\)到\(c\)以及\(b\)到\(c\)
所以我们可以这么建

然后跑最大流就可以了
由于起点以及终点不可以被修改
所以我忽视了别的点到起点的连边
同时别的点到终点的连边边权都是\(inf\)
CODE:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<cstdlib>
#include<string>
#include<queue>
#include<map>
#include<stack>
#include<list>
#include<set>
#include<deque>
#include<vector>
#include<ctime>
#define ll long long
#define inf 0x7fffffff
#define N 88
#define IL inline
#define M 5008611
#define D double
#define ull unsigned long long
#define R register
using namespace std;
template<typename T>IL void read(T &_)
{
T __=0,___=1;char ____=getchar();
while(!isdigit(____)) {if(____=='-') ___=0;____=getchar();}
while(isdigit(____)) {__=(__<<1)+(__<<3)+____-'0';____=getchar();}
_=___ ? __:-__;
}
/*-------------OI使我快乐-------------*/
int T;ll ans;
int n,m,tot=1,sx,sy,tx,ty;
int hei[N][N],to[M],nex[M],head[N*N*5],w[M];
int dep[5*N*N],cur[5*N*N];
vector<pair<int,int> > G[N*N];
queue<int> Q;
bool vis[N][N];
int tox[6]={0,0,0,1,-1},toy[6]={0,1,-1,0,0};
IL int id(int x,int y){return (x-1)*m+y;}
IL bool safe(int x,int y){return x>=1&&x<=n&&y>=1&&y<=m;}
IL void add(int x,int y,int z)
{to[++tot]=y;nex[tot]=head[x];head[x]=tot;w[tot]=z;
swap(x,y);to[++tot]=y;nex[tot]=head[x];head[x]=tot;w[tot]=0;}
IL bool bfs()
{
for(R int i=1;i<=5*n*m;++i) dep[i]=0;
Q.push(id(sx,sy));dep[id(sx,sy)]=1;
for(;!Q.empty();)
{
int u=Q.front();Q.pop();
for(R int i=head[u];i;i=nex[i])
{
int v=to[i];
if(w[i]>0&&dep[v]==0)
{
dep[v]=dep[u]+1;Q.push(v);
}
}
}
return dep[id(tx,ty)]!=0;
}
IL int dfs(int now,int res)
{
if(now==id(tx,ty)||res==0) return res;
for(R int &i=cur[now];i;i=nex[i])
{
int v=to[i];
if(w[i]>0&&dep[v]==dep[now]+1)
{
int have=dfs(v,min(w[i],res));
if(have>0)
{
w[i]-=have;w[i^1]+=have;
return have;
}
}
}
return 0;
}
IL void Dinic()
{
while(bfs())
{
for(R int i=1;i<=5*n*m;++i) cur[i]=head[i];
int d=dfs(id(sx,sy),inf);
while(d) ans+=d,d=dfs(id(sx,sy),inf);
}
}
int main()
{
freopen("ski.in","r",stdin);
freopen("ski.out","w",stdout);
read(T);
while(T--)
{
read(n);read(m);read(sx);read(sy);read(tx);read(ty);
tot=1;ans=0;memset(head,0,sizeof head);
for(R int i=1;i<=n;++i)
for(R int j=1;j<=m;++j)
read(hei[i][j]);
if(abs(sx-tx)+abs(sy-ty)==1) {puts("-1");continue;}
for(R int i=1;i<=n;++i)
{
for(R int j=1;j<=m;++j)
for(R int k=1;k<=4;++k)
{
int nowx=i+tox[k],nowy=j+toy[k];
if(nowx==sx&&nowy==sy) continue;
if(safe(nowx,nowy)&&hei[nowx][nowy]<=hei[i][j])
G[id(nowx,nowy)].push_back(make_pair(hei[i][j]-hei[nowx][nowy]+1,id(i,j)));
}
}
for(R int i=1;i<=n*m;++i)
{
if(G[i].size())
sort(G[i].begin(),G[i].end());
}
// for(R int i=1;i<=n*m;++i)
// {
// if(G[i].size())
// {
// for(R int j=0;j<(int)G[i].size();++j)
// printf("%d -- %d --> %d\n",G[i][j].second,G[i][j].first,i);
// }
// }
for(R int i=1;i<=n*m;++i)
{
int now=0;
if(id(tx,ty)==i)
{
for(R int j=(int)G[i].size()-1;j>=0;--j)
{
// printf("%d -- %d --> %d\n",G[i][j].second,G[i][j].first,i);
++now;
add(G[i][j].second,i+now*n*m,inf);
add(i+now*n*m,i+(now-1)*n*m,inf);
}
}
else
{
for(R int j=(int)G[i].size()-1;j>=0;--j)
{
// printf("%d -- %d --> %d\n",G[i][j].second,G[i][j].first,i);
++now;
add(G[i][j].second,i+now*n*m,inf);
add(i+now*n*m,i+(now-1)*n*m,G[i][j].first);
}
}
}
Dinic();
printf("%lld\n",ans);
for(R int i=1;i<=n*m;++i) G[i].clear();
}
fclose(stdin);
fclose(stdout);
return 0;
}
HEOI 2019 RP++
考试题 T2的更多相关文章
- hdu3089 Josephus again|快速约瑟夫环
题目链接:戳我 貌似是高一昨天的考试题T2?????感觉挺好玩的就搞了搞qwqwq 其实是HDU上面的题啦.... 对于普通的约瑟夫问题,大概是n个人围成一个环,从1开始报数,数到k,那个人出队,最后 ...
- [Noip2016]蚯蚓 D2 T2 队列
[Noip2016]蚯蚓 D2 T2 Description 本题中,我们将用符号[c]表示对c向下取整,例如:[3.0」= [3.1」=[3.9」=3.蛐蛐国最近蚯蚓成灾了!隔壁跳 蚤国的跳蚤也拿蚯 ...
- T2 Func<in T1,out T2>(T1 arg)
委托调用方法的4种方式. using System; using System.Collections.Generic; namespace ConsoleApplication1 { delegat ...
- Hotelling T2检验和多元方差分析
1.1 Hotelling T2检验 Hotelling T2检验是一种常用多变量检验方法,是单变量检验的自然推广,常用于两组均向量的比较. 设两个含量分析为n,m的样本来自具有公共协方差阵的q维正态 ...
- bzoj4034: [HAOI2015]T2
4034: [HAOI2015]T2 Time Limit: 10 Sec Memory Limit: 256 MB Submit: 2684 Solved: 843 Description 有一 ...
- 【BZOJ 4517】【SDOI 2016 Round1 Day2 T2】排列计数
本蒟蒻第一次没看题解A的题竟然是省选$Round1$ $Day2$ $T2$ 这道组合数学题. 考试时一开始以为是莫队,后来想到自己不会组合数的一些公式,便弃疗了去做第三题,,, 做完第三题后再回来看 ...
- NOIP欢乐模拟赛 T2 解题报告
小澳的坐标系 (coordinate.cpp/c/pas) [题目描述] 小澳者表也,数学者景也,表动则景随矣. 小澳不喜欢数学,可数学却待小澳如初恋,小澳睡觉的时候也不放过. 小澳的梦境中出现了一个 ...
- Action<T1, T2>委托
封装包含两个参数的方法委托,没有返回值. 语法 public delegate void Action<in T1, in T2>( T1 arg1, T2 arg2 ) 类型参数 in ...
- NOIP2013普及组 T2 表达式求值
OJ地址:洛谷P1981 CODEVS 3292 正常写法是用栈 #include<iostream> #include<algorithm> #include<cmat ...
随机推荐
- Resolving multicopy duplications de novo using polyploid phasing 用多倍体相位法解决多拷贝复制的新问题
抽象.虽然单分子测序系统的兴起已经实现组装复杂地区的能力空前提高在基因组中,基因组中的长节段重复仍然是装配中具有挑战性的前沿. 分段重复同时具有丰富的基因并且倾向于大的结构重排,使得它们的序列的分辨率 ...
- C++11中std::unordered_map的使用
unordered map is an associative container that contains key-value pairs with unique keys. Search, in ...
- TCP端口状态说明ESTABLISHED、TIME_WAIT
TCP状态转移要点 TCP协议规定,对于已经建立的连接,网络双方要进行四次握手才能成功断开连接,如果缺少了其中某个步骤,将会使连接处于假死状态,连接本身占用的资源不 会被释放.网络服务器程序要同时管理 ...
- handsontable 概述
很像excel的jquery插件.关于它的中文资料不多,自己只能看看英文的了.记录如下: developer guide //主要是这八部分数据绑定:renderer, afterchange, se ...
- DI延伸
延迟初始化Bean 延迟初始化也叫做惰性初始化,指不提前初始化Bean,而是只有在真正使用时才创建及初始化Bean. 配置方式很简单只需在<bean>标签上指定 “lazy-init” 属 ...
- Linux下source文件两种方法
1.直接source命令加文件 source /etc/rc.d/init.d/functions 2.点(.)加文件 . /etc/rc.d/init.d/functions
- 基于pscp批量分发文件
用法: pscp -h 目标ip文件 本地文件路径 远程路径 pscp -h hosts.ip file.txt ~/
- 一起学习《C#高级编程》3--运算符重载
运算符的重载.C++的开发人员应该很熟悉这个概念,但这对Java 和 VB 开发人员确实全新的. 对于一些数值间的运算,如果通过方法来指定运算规则的话,不免会繁琐,这时就可以利用运算符的重载. 例: ...
- MVC中用jQuery加BootStrap实现动态增加删除文本输入框!
http://www.freejs.net/article_biaodan_278.html 这是在网上找到方法,我修改了一下实合我的项目,发博只为收藏记录并加深记忆. 修改后效果如下 @model ...
- C#基础笔记(第十九天)
1.CSS 层叠样式表 对HTML的补充实现网页内容和页面效果的彻底分离1.内联样式表(在标签内设置元素的样式)<p style="background:red; font-size: ...