「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\)个好点,我们让它们一定没有用的.要满足这个条件就要使它只能和坏 ...
随机推荐
- idea的svn安装
https://blog.csdn.net/qq_27093465/article/details/74898489
- java 字符集 Charset
字符集就是为每个字符编个号码.如ASCII编码中,字符 'A' 的号码为 65 (或二进制01000001):GBK编码中,字符 '国' 对应的号码为47610 . 编码:将字符序列转换成二进制序列. ...
- jquery遍历table的tr获取td的值
方法一: var siginArray = []; $("#tbody").children("tr").each(function () { var sigi ...
- Github使用说明 --整理者米米
打开百度搜索Git官网下载对应的windows版本 傻瓜式默认安装,点击完成 PS:安装的过程比较慢 安装完成后打开命令行窗口(cmd) 查看版本号------git --version 安装成功 ...
- 大数据离线分析平台 JSSDK数据收集引擎编写
JsSDK设计规则在js sdk中我们需要收集launch.pageview.chargeRequest和eventDuration四种数据,所以我们需要在js中写入四个方法来分别收集这些数据,另外我 ...
- nginx基于目录的映射:
nginx基于目录的映射: location /wxchat/ { #proxy_redirect off; proxy_set_header Host $host; proxy_set_header ...
- linux zip删除指定文件和追加文件
使用zip命令的-d参数即可删除zip包中的特定文件. 示例:假设有test.zip,包含_code(目录)._code.zip.readme.txt三个文件,现在要删除test.zip中的_code ...
- jQuery模态框实现 后台添加删除修改Ip端口
主要用到,$('#i1').each(),标签里绑定函数可传参数this <!DOCTYPE html> <html lang="en"> <head ...
- 我的less学习之路
less注释 可以在代码中使用块样式(/* */)和行内注释(//),但是当编译LESS代码时,单行注释不会显示在CSS文件中.开发中主要维护的是less文件,所以可以使用行内注释,最终编译的css文 ...
- 【AMQ】之概述
消息中间件的主要作用: 基本功能,将信息以消息的形式在多个应用之间传递 特点: 消息异步接受,发送者不需要等待接收者的响应,减少系统之间的耦合性. 消息可靠性接收,确保消息在中间件可靠保存,只有接收方 ...