棋盘模型 + 动态加边
#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」棋盘(费用流)的更多相关文章

  1. [LOJ#6068]. 「2017 山东一轮集训 Day4」棋盘[费用流]

    题意 题目链接 分析 考虑每个棋子对对应的横向纵向的极大区间的影响:记之前这个区间中的点数为 \(x\) ,那么此次多配对的数量即 \(x\) . 考虑费用流,\(S\rightarrow 横向区间 ...

  2. Loj 6068. 「2017 山东一轮集训 Day4」棋盘

    Loj 6068. 「2017 山东一轮集训 Day4」棋盘 题目描述 给定一个 $ n \times n $ 的棋盘,棋盘上每个位置要么为空要么为障碍.定义棋盘上两个位置 $ (x, y),(u, ...

  3. loj6068. 「2017 山东一轮集训 Day4」棋盘 二分图,网络流

    loj6068. 「2017 山东一轮集训 Day4」棋盘 链接 https://loj.ac/problem/6068 思路 上来没头绪,后来套算法,套了个网络流 经典二分图 左边横,右边列 先重新 ...

  4. LOJ 6068「2017 山东一轮集训 Day4」棋盘

    题意 一个 \(n\times n\) 的棋盘上面有若干障碍物. 定义两个棋子可以互相攻击当且仅当这两个棋子的横坐标或纵坐标相等而且中间不能隔着障碍物.(可以隔棋子) 有 \(q\) 次询问,每次询问 ...

  5. loj#6073. 「2017 山东一轮集训 Day5」距离(费用流)

    题意 题目链接 Sol 我们可以把图行列拆开,同时对于行/列拆成很多个联通块,然后考虑每个点所在的行联通块/列联通块的贡献. 可以这样建边 从S向每个行联通块连联通块大小条边,每条边的容量为1,费用为 ...

  6. Loj #6069. 「2017 山东一轮集训 Day4」塔

    Loj #6069. 「2017 山东一轮集训 Day4」塔 题目描述 现在有一条 $ [1, l] $ 的数轴,要在上面造 $ n $ 座塔,每座塔的坐标要两两不同,且为整点. 塔有编号,且每座塔都 ...

  7. 「2017 山东一轮集训 Day4」基因

    设置 \(\sqrt{n}\) 个关键点,维护出关键点到每个右端点之间的答案以及Pam的左指针,每次暴力向左插入元素即可,为了去重,还需要记录一下Pam上每个节点在每个关键点为左端点插入到时候到最左边 ...

  8. Loj #6073.「2017 山东一轮集训 Day5」距离

    Loj #6073.「2017 山东一轮集训 Day5」距离 Description 给定一棵 \(n\) 个点的边带权的树,以及一个排列$ p\(,有\)q $个询问,给定点 \(u, v, k\) ...

  9. 「2017 山东一轮集训 Day5」苹果树

    「2017 山东一轮集训 Day5」苹果树 \(n\leq 40\) 折半搜索+矩阵树定理. 没有想到折半搜索. 首先我们先枚举\(k\)个好点,我们让它们一定没有用的.要满足这个条件就要使它只能和坏 ...

随机推荐

  1. springboot获得应用上下文

    有的兄弟说需要在启动类中注入,但我暂时不用

  2. How to Export to Excel

    https://simpleisbetterthancomplex.com/tutorial/2016/07/29/how-to-export-to-excel.html Export data to ...

  3. kafka_2.11-0.8.2.1+java 生产消费程序demo示例

      Kafka学习8_kafka java 生产消费程序demo示例 kafka是吞吐量巨大的一个消息系统,它是用scala写的,和普通的消息的生产消费还有所不同,写了个demo程序供大家参考.kaf ...

  4. JAVA的debug入门和多断电调试

    调试的一般都是逻辑 第一步的错误双击数字旁边的蓝色地方,或者点击右键如图 断点的意思就是程序执行在哪里就停止 当找不到DEBUG中的Variables是在位置输入Variables就可以了: 再按下F ...

  5. TableLayoutPanel 行高列宽设置

    /// <summary> /// 获取TableLayoutPanel指定行的高度 /// </summary> /// <param name="layou ...

  6. HTML5 Canvas水波纹动画特效

    HTML5的Canvas特性非常实用,我们不仅可以在Canvas画布上绘制各种图形,也可以制作绚丽的动画,比如这次介绍的水波纹动画特效.以前我们也分享过一款基于HTML5 WebGL的水波荡漾动画,让 ...

  7. sysroot和prefix

    --with-sysroot用来指定系统的root.该选项主要用于新系统(比如LFS)构建或交叉编译.比如你的LFS的root在/mnt/lfs,那么configure时指定--with-sysroo ...

  8. 【java】之获取HTTP请求的值常用方法

    logger.info("销帐完成通知回调,请求URL:"+req.getRequestURI().toString()); InputStream in = req.getInp ...

  9. 无法清除cookie中的属性值之对解决问题的思考

    关于如何快速解决一个自己知识以外的问题的思考 做好任何事情都是讲究方法的,这是我健身之后的一个领悟,用正确的方式做事,自己的付出才能有价值. 首先分析问题: 比如我想清除cookie里面的token, ...

  10. Hadoop概念学习系列之pagerank的友情链接(三十八)

    博主我带大家,弄清楚一个事实. 比如,搜狐主页下方,有很多友情链接,这些友情链接,那可是一个位置就是多少钱. 有人说,一个位置多少钱,这又没有给我带来点击量,那我干嘛还每年花上几十万,给搜狐,就那么放 ...