「2017 山东一轮集训 Day4」棋盘(费用流)
棋盘模型 + 动态加边
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<queue>
#define ll long long
#define M 51
#define N 500010
#define mmp make_pair
const int inf = 0x3e3e3e3e;
using namespace std;
int read()
{
int nm = 0, f = 1;
char c = getchar();
for(; !isdigit(c); c = getchar()) if(c == '-') f = -1;
for(; isdigit(c); c = getchar()) nm = nm * 10 + c - '0';
return nm * f;
}
char S[M][M];
struct Note
{
int ver, nxt, vi, vj, cap, flow;
}edge[N];
int head[N], cnt = 1, n, s, t, cost, ans;
bool vis[5010];
int dis[5050], sd[5050 * 30], sum[5050 * 30], noteans[5050], upup;
int tota, totb, notea[M][M], noteb[M][M], suma[M * M], sumb[M * M], nowa[M * M], nowb[M * M];
void push(int vi, int vj, int flo, int ver)
{
// cout << vi << " " << vj << " " << ver << "\n";
cnt++; edge[cnt].vi = vi, edge[cnt].ver = ver, edge[cnt].vj = vj, edge[cnt].nxt = head[vi], head[vi] = cnt, edge[cnt].cap = flo;
cnt++; edge[cnt].vi = vj, edge[cnt].ver = -ver, edge[cnt].vj = vi, edge[cnt].nxt = head[vj], head[vj] = cnt;
}
bool spfa(int be, int ed)
{
queue<int> q;
for(int i = 0; i <= t; i++) vis[i] = 0, dis[i] = inf;
dis[be] = 0, sum[s] = 0x3e3e3e3e;
q.push(be);
while(!q.empty())
{
int now = q.front();
q.pop();
vis[now] = false;
for(int i = head[now]; i; i = edge[i].nxt)
{
int vj = edge[i].vj;
if(edge[i].flow >= edge[i].cap) continue;
if(dis[vj] > dis[now] + edge[i].ver)
{
dis[vj] = dis[now] + edge[i].ver;
sd[vj] = i;
sum[vj] = min(sum[now], edge[i].cap - edge[i].flow);
if(!vis[vj])
{
vis[vj] = true;
q.push(vj);
}
}
}
}
if(dis[ed] == inf) return 0;
ans += sum[ed];
cost += sum[ed] * dis[ed];
int u = ed, x;
while(u != be)
{
x = sd[u];
int vi = edge[x].vi, vj = edge[x].vj;
if(vi == s && nowa[vj] + 1< suma[vj])
{
nowa[vj]++;
push(s, vj, 1, nowa[vj]);
}
if(vj == t && nowb[vi - tota] + 1 < sumb[vi - tota])
{
nowb[vi - tota]++;
push(vi, t, 1, nowb[vi - tota]);
}
edge[x].flow += sum[ed];
edge[x ^ 1].flow -= sum[ed];
u = edge[x].vi;
}
return true;
}
int main()
{
n = read();
for(int i = 1; i <= n; i++) scanf("%s", S[i] + 1);
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= n; j++)
{
if(S[i][j] == '.') upup++;
}
}
for(int i = 1; i <= n; i ++)
{
for(int j = 1; j <= n; j++)
{
if(j == 1 || (S[i][j - 1] == '#' && S[i][j] == '.')) tota++;
if(S[i][j] == '#') continue;
suma[tota]++, notea[i][j] = tota;
}
}
for(int j = 1; j <= n; j++)
{
for(int i = 1; i <= n; i++)
{
if(i == 1 || (S[i - 1][j] == '#' && S[i][j] == '.')) totb++;
if(S[i][j] == '#') continue;
sumb[totb]++, noteb[i][j] = totb;
push(notea[i][j], tota + noteb[i][j], 1, 0);
}
}
s = tota + totb + 1, t = s + 1;
for(int i = 1; i <= tota; i++) if(suma[i]) push(s, i, 1, 0);
for(int i = 1; i <= totb; i++) if(sumb[i]) push(i + tota, t, 1, 0);
for(int tim = 1; tim <= upup; tim++)
{
spfa(s, t);
noteans[tim] = cost;
}
int t = read();
while(t--)
{
int x = read();
if(x > upup) puts("0");
else cout << noteans[x] << "\n";
}
return 0;
}
/*
3
...
...
...
5
*/
「2017 山东一轮集训 Day4」棋盘(费用流)的更多相关文章
- [LOJ#6068]. 「2017 山东一轮集训 Day4」棋盘[费用流]
题意 题目链接 分析 考虑每个棋子对对应的横向纵向的极大区间的影响:记之前这个区间中的点数为 \(x\) ,那么此次多配对的数量即 \(x\) . 考虑费用流,\(S\rightarrow 横向区间 ...
- Loj 6068. 「2017 山东一轮集训 Day4」棋盘
Loj 6068. 「2017 山东一轮集训 Day4」棋盘 题目描述 给定一个 $ n \times n $ 的棋盘,棋盘上每个位置要么为空要么为障碍.定义棋盘上两个位置 $ (x, y),(u, ...
- loj6068. 「2017 山东一轮集训 Day4」棋盘 二分图,网络流
loj6068. 「2017 山东一轮集训 Day4」棋盘 链接 https://loj.ac/problem/6068 思路 上来没头绪,后来套算法,套了个网络流 经典二分图 左边横,右边列 先重新 ...
- LOJ 6068「2017 山东一轮集训 Day4」棋盘
题意 一个 \(n\times n\) 的棋盘上面有若干障碍物. 定义两个棋子可以互相攻击当且仅当这两个棋子的横坐标或纵坐标相等而且中间不能隔着障碍物.(可以隔棋子) 有 \(q\) 次询问,每次询问 ...
- loj#6073. 「2017 山东一轮集训 Day5」距离(费用流)
题意 题目链接 Sol 我们可以把图行列拆开,同时对于行/列拆成很多个联通块,然后考虑每个点所在的行联通块/列联通块的贡献. 可以这样建边 从S向每个行联通块连联通块大小条边,每条边的容量为1,费用为 ...
- Loj #6069. 「2017 山东一轮集训 Day4」塔
Loj #6069. 「2017 山东一轮集训 Day4」塔 题目描述 现在有一条 $ [1, l] $ 的数轴,要在上面造 $ n $ 座塔,每座塔的坐标要两两不同,且为整点. 塔有编号,且每座塔都 ...
- 「2017 山东一轮集训 Day4」基因
设置 \(\sqrt{n}\) 个关键点,维护出关键点到每个右端点之间的答案以及Pam的左指针,每次暴力向左插入元素即可,为了去重,还需要记录一下Pam上每个节点在每个关键点为左端点插入到时候到最左边 ...
- Loj #6073.「2017 山东一轮集训 Day5」距离
Loj #6073.「2017 山东一轮集训 Day5」距离 Description 给定一棵 \(n\) 个点的边带权的树,以及一个排列$ p\(,有\)q $个询问,给定点 \(u, v, k\) ...
- 「2017 山东一轮集训 Day5」苹果树
「2017 山东一轮集训 Day5」苹果树 \(n\leq 40\) 折半搜索+矩阵树定理. 没有想到折半搜索. 首先我们先枚举\(k\)个好点,我们让它们一定没有用的.要满足这个条件就要使它只能和坏 ...
随机推荐
- node 学习资料
Node 学习资料: 资料名称 网址 Node.js 中文API文档 http://nodejs.cn/api/ Node 菜鸟教程 http://www.runoob.com/nodejs/node ...
- TypeScript 之 类型推导
https://m.runoob.com/manual/gitbook/TypeScript/_book/doc/handbook/Type%20Inference.html 类型推导:发生在初始化变 ...
- STM32的SPI2操作Flash
关于STM32F107的SPI标志 SPI_I2S_FLAG_BSY和SPI_I2S_FLAG_TXE的疑问 http://www.openedv.com/posts/list/23579.htm ...
- windows10中git-bash闪退的解决办法
windows10中git-bash闪退的解决办法 出现错误详情 Windows10 64位专业版安装git .18之后出现 Git闪退,报错信息:bash: /dev/null: No such d ...
- jquery位置问题
在页面中添加jquery时,一定要把jquery放在其他js文件前面!不然会出现"$ is not defined", 导致js无法正常运行.
- 代码编辑器之sublime text插件
sublimetext 学习资源:http://www.jianshu.com/p/d1b9a64e2e37 Sublime SFTP CTags – 让Sublime Text支持CTags. Si ...
- Delphi 的 Bit
我一直感觉 Delphi 下的Bit操作不是很好使, 所以一直屏蔽着这方面的学习.不过最近收集整理了一下代码. 原因是这样的. 由于某个需求被分解成 在 0~n(不定) 中,有几个数字被置换成了“tr ...
- putty删除键backspace设置
putty连接unix登陆后,编辑命令输入错误时要修改,可是习惯性的按backspace键或者Delete键都出现一堆奇怪的字符,无法删除出错的命令语句. 修正方法如下: ************** ...
- 解决 liblog4cpp.a: could not read symbols: Bad value
将 liblog4cpp.a 链接进一个 so, 编译时出现 : liblog4cpp.a(RollingFileAppender.o): relocation R_X86_64_32 against ...
- Hadoop概念学习系列之Hadoop、Spark学习路线(很值得推荐)(十八)
不多说,直接上干货! 说在前面的话 此笔,对于仅对于Hadoop和Spark初中学者.高手请忽略! 1 Java基础: 视频方面: 推荐<毕向东JAVA基础视频教程>.学 ...