loj#6073. 「2017 山东一轮集训 Day5」距离(费用流)
题意
Sol
我们可以把图行列拆开,同时对于行/列拆成很多个联通块,然后考虑每个点所在的行联通块/列联通块的贡献。
可以这样建边
从S向每个行联通块连联通块大小条边,每条边的容量为1,费用为\(i\)(i表示这是第几条边)。
从每个点所在的行联通块向列联通块连边,容量为1,费用为0
从每个列联通块向T连联通块大小条边,每条边的容量为1,费用为\(i\)(i表示这是第几条边)。
这样跑最小费用最大流,每增光一次的费用就是答案。预处理后O(1)回答即可
#include<bits/stdc++.h>
#define Pair pair<int, int>
#define MP(x, y) make_pair(x, y)
#define fi first
#define se second
//#define int long long
#define LL long long
#define ull unsigned long long
#define Fin(x) {freopen(#x".in","r",stdin);}
#define Fout(x) {freopen(#x".out","w",stdout);}
using namespace std;
const int MAXN = 5001, mod = 1e9 + 7, INF = 1e9 + 10;
const double eps = 1e-9;
template <typename A, typename B> inline bool chmin(A &a, B b){if(a > b) {a = b; return 1;} return 0;}
template <typename A, typename B> inline bool chmax(A &a, B b){if(a < b) {a = b; return 1;} return 0;}
template <typename A, typename B> inline LL add(A x, B y) {if(x + y < 0) return x + y + mod; return x + y >= mod ? x + y - mod : x + y;}
template <typename A, typename B> inline void add2(A &x, B y) {if(x + y < 0) x = x + y + mod; else x = (x + y >= mod ? x + y - mod : x + y);}
template <typename A, typename B> inline LL mul(A x, B y) {return 1ll * x * y % mod;}
template <typename A, typename B> inline void mul2(A &x, B y) {x = (1ll * x * y % mod + mod) % mod;}
template <typename A> inline void debug(A a){cout << a << '\n';}
template <typename A> inline LL sqr(A x){return 1ll * x * x;}
template <typename A, typename B> inline LL fp(A a, B p, int md = mod) {int b = 1;while(p) {if(p & 1) b = mul(b, a);a = mul(a, a); p >>= 1;}return b;}
template <typename A> A inv(A x) {return fp(x, mod - 2);}
inline int read() {
char c = getchar(); int x = 0, f = 1;
while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return x * f;
}
int N, S, T , TT;
char s[51][51];
int id[51][51][2], c1 = 1, c2 = 1, ans[MAXN * MAXN], tot1[20 * MAXN], tot2[20 * MAXN ], num1, num2;
struct Edge {
int u, v, w, f, nxt;
}E[2 * MAXN * MAXN];
int head[MAXN * 20 + 1], num;
void add_edge(int x, int y, int w, int f) {
E[num] = (Edge){x, y, w, f, head[x]};
head[x] = num++;
}
void AddEdge(int x, int y, int w, int f) {
//printf("%d %d %d %d\n", x, y, w, f);
add_edge(x, y, w, f);
add_edge(y, x, -w, 0);
}
int dis[MAXN * 10], vis[MAXN * 10], pre[MAXN * 10];
int SPFA() {
memset(dis, 0x3f, sizeof(dis));
memset(vis, 0, sizeof(vis));
queue<int> q; q.push(S); dis[S] = 0;
while(!q.empty()) {
int p = q.front(); q.pop(); vis[p] = 0;
for(int i = head[p]; ~i; i = E[i].nxt) {
int to = E[i].v, w = E[i].w;
if(dis[to] > dis[p] + w && E[i].f) {
dis[to] = dis[p] + w; pre[to] = i;
if(!vis[to]) vis[to] = 1, q.push(to);
}
}
}
return dis[TT];
}
int MCMF() {
int val = SPFA(), dec = INF;
for(int k = TT; k != S; k = E[pre[k]].u) chmin(dec, E[pre[k]].f);
for(int k = TT; k != S; k = E[pre[k]].u) E[pre[k]].f -= dec, E[pre[k] ^ 1].f += dec;
return dec * val;
}
signed main() {
//freopen("a.in", "r", stdin);
memset(head, -1, sizeof(head));
N = read(); S = 0; T = N * N * 10, TT = T + 1; c2 = N * N * 3 + 1;
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] == '#') tot1[c1] = num1, num1 = 0, c1++;
else id[i][j][0] = c1, num1++;
if(s[j][i] == '#') tot2[c2] = num2, num2 = 0, c2++;
else id[j][i][1] = c2, num2++;
}
if(num1) tot1[c1++] = num1, num1 = 0;
if(num2) tot2[c2++] = num2, num2 = 0;
}
for(int i = 1; i <= N; i++)
for(int j = 1; j <= N; j++)
if(id[i][j][0] && id[i][j][1])
AddEdge(id[i][j][0], id[i][j][1], 0, 1);
for(int i = 1; i <= c1; i++)
for(int j = 0; j < tot1[i]; j++)
AddEdge(S, i, j, 1);
for(int i = N * N * 3 + 1; i <= c2; i++)
for(int j = 0; j < tot2[i]; j++)
AddEdge(i, T, j, 1);
for(int i = 1; i <= 2 * N * N; i++)
AddEdge(T, TT, 0, 1);
for(int i = 1; i <= N * N; i++)
ans[i] = ans[i - 1] + MCMF();
int Q = read();
while(Q--) cout << ans[read()] << '\n';
return 0;
}
loj#6073. 「2017 山东一轮集训 Day5」距离(费用流)的更多相关文章
- Loj #6073.「2017 山东一轮集训 Day5」距离
Loj #6073.「2017 山东一轮集训 Day5」距离 Description 给定一棵 \(n\) 个点的边带权的树,以及一个排列$ p\(,有\)q $个询问,给定点 \(u, v, k\) ...
- loj#6073. 「2017 山东一轮集训 Day5」距离(树链剖分 主席树)
题意 题目链接 Sol 首先对询问差分一下,我们就只需要统计\(u, v, lca(u, v), fa[lca(u, v)]\)到根的路径的贡献. 再把每个点与\(k\)的lca的距离差分一下,则只需 ...
- [LOJ#6068]. 「2017 山东一轮集训 Day4」棋盘[费用流]
题意 题目链接 分析 考虑每个棋子对对应的横向纵向的极大区间的影响:记之前这个区间中的点数为 \(x\) ,那么此次多配对的数量即 \(x\) . 考虑费用流,\(S\rightarrow 横向区间 ...
- 「2017 山东一轮集训 Day5」距离
/* 写完开店再写这个题目顿时神清气爽, 腰也不疼了, 眼也不花了 首先考虑将询问拆开, 就是查询一些到根的链和点k的关系 根据我们开店的结论, 一个点集到一个定点的距离和可以分三部分算 那么就很简单 ...
- 「2017 山东一轮集训 Day4」棋盘(费用流)
棋盘模型 + 动态加边 #include<cstdio> #include<algorithm> #include<iostream> #include<cs ...
- Loj #6069. 「2017 山东一轮集训 Day4」塔
Loj #6069. 「2017 山东一轮集训 Day4」塔 题目描述 现在有一条 $ [1, l] $ 的数轴,要在上面造 $ n $ 座塔,每座塔的坐标要两两不同,且为整点. 塔有编号,且每座塔都 ...
- Loj 6068. 「2017 山东一轮集训 Day4」棋盘
Loj 6068. 「2017 山东一轮集训 Day4」棋盘 题目描述 给定一个 $ n \times n $ 的棋盘,棋盘上每个位置要么为空要么为障碍.定义棋盘上两个位置 $ (x, y),(u, ...
- 「2017 山东一轮集训 Day5」苹果树
「2017 山东一轮集训 Day5」苹果树 \(n\leq 40\) 折半搜索+矩阵树定理. 没有想到折半搜索. 首先我们先枚举\(k\)个好点,我们让它们一定没有用的.要满足这个条件就要使它只能和坏 ...
- LOJ #6074. 「2017 山东一轮集训 Day6」子序列
#6074. 「2017 山东一轮集训 Day6」子序列 链接 分析: 首先设f[i][j]为到第i个点,结尾字符是j的方案数,这个j一定是从i往前走,第一个出现的j,因为这个j可以代替掉前面所有j. ...
随机推荐
- 解决localdb中文智能的问题
declare @database nvarchar(100) declare tmpCur cursor for select DB_NAME() open tmpCur fetch next fr ...
- [Swift]LeetCode122. 买卖股票的最佳时机 II | Best Time to Buy and Sell Stock II
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- websocket+rabbitmq实战
1. websocket+rabbitmq实战 1.1. 前言 接到的需求是后台定向给指定web登录用户推送消息,且可能同一账号会登录多个客户端都要接收到消息 1.2. 遇坑 基于springbo ...
- 非对称加密技术中,iFace [ 爱妃链 ]人脸密钥技术排名第三,将弥补区块链现有不足
最近,区块链领域,出现了一个比较热门技术的讨论,人脸密钥技术,可能大家还对这个名词感到很陌生,但是熟悉加密技术的技术大牛可能一听就能够明白大体的意思了,但是也正是这一熟悉而陌生的技术名词,掀起了区块链 ...
- vue项目安装vux
本文章默认基于“vue init webpack myproject”已经搭好基本的项目, 而且本文是从我有道笔记拷贝稍加修改过来的 本来我私人笔记写给自己看的所以有些地方可能描述不够清晰 需要修改的 ...
- 二叉树的相关在线编程(python)
问题一: 输入一个整数数组, 判断该数组是不是某二叉搜索树的后序遍历的结果.如果是则输出Yes,否则输出No. 假设输入的数组的任意两个数字都互不相同. 正确的后序遍历结果: sequence = [ ...
- Python Matplotlib.pyplot plt 中文显示
话不多说,上代码 # -*- coding: UTF-8 -*- import matplotlib.pyplot as plt from matplotlib.font_manager import ...
- .NET Core实战项目之CMS 第八章 设计篇-内容管理极简设计全过程
写在前面 上一篇文章中我带着大家进行了权限部分的极简设计,也仅仅是一个基本的权限设计.不过你完全可以基于这套权限系统设计你的更复杂的权限系统,当然更复杂的权限系统要根据你的业务来进行,因为任何脱离实际 ...
- Spring Security构建Rest服务-1300-Spring Security OAuth开发APP认证框架之JWT实现单点登录
基于JWT实现SSO 在淘宝( https://www.taobao.com )上点击登录,已经跳到了 https://login.taobao.com,这是又一个服务器.只要在淘宝登录了,就能直接访 ...
- 《HelloGitHub月刊》第 09 期
<HelloGitHub>第 09 期 兴趣是最好的老师,<HelloGitHub>就是帮你找到兴趣! 前言 转眼就到年底了,月刊做到了第09期,感谢大家一路的支持和帮助