「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\)个好点,我们让它们一定没有用的.要满足这个条件就要使它只能和坏 ...
随机推荐
- py-day1-4 python基本数据类型2
# replace 替换 text = 'xiaomafafafahaoyunlianlian' v = text.replace('fa','shun') x = text.replace('fa' ...
- 【设计模式】JDK源码中用到的设计模式
https://blog.csdn.net/angjunqiang/article/details/42061453 https://blog.csdn.net/baiye_xing/article/ ...
- 阅读<AXI4-Stream Infrastructure IP Suite v2.2>笔记
阅读<AXI4-Stream Infrastructure IP Suite v2.2>笔记 为了配合tpg和video_out IP核的使用,我们需要使用此组件中的AXI4-stream ...
- 转:Oracle 执行计划(Explain Plan) 说明
Oracle 执行计划(Explain Plan) 说明 原贴地址:http://blog.csdn.net/tianlesoftware/article/details/5827245 如果要分 ...
- 一文说尽 MySQL 优化原理
说起MySQL的查询优化,相信大家收藏了一堆奇技淫巧:不能使用SELECT *.不使用NULL字段.合理创建索引.为字段选择合适的数据类型..... 你是否真的理解这些优化技巧?是否理解其背后的工作原 ...
- python slave status 2
#!/usr/bin/env python import MySQLdbimport contextlib @contextlib.contextmanagerdef mysql(Host,Port, ...
- Hadoop hbase集群断电数据块被破坏无法启动
集群机器意外断电重启,导致hbase 无法正常启动,抛出reflect invocation异常,可能是正在执行的插入或合并等操作进行到一半时中断,导致部分数据文件不完整格式不正确或在hdfs上blo ...
- Django 模板语法
模板语法之变量 变量在HTML中的表示:{{var_name}} 变量取值:句点符 "." views: def index(request): import datetime s ...
- spring cloud和spring boot两个完整项目
spring cloud和spring boot两个完整项目 spring cloud 是基于Spring Cloud的云分布式后台管理系统架构,核心技术采用Eureka.Fegin.Ribbon.Z ...
- nvm环境配置
安装nvm curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash nvm insta ...