题意

题目链接

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」距离(费用流)的更多相关文章

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

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

  2. loj#6073. 「2017 山东一轮集训 Day5」距离(树链剖分 主席树)

    题意 题目链接 Sol 首先对询问差分一下,我们就只需要统计\(u, v, lca(u, v), fa[lca(u, v)]\)到根的路径的贡献. 再把每个点与\(k\)的lca的距离差分一下,则只需 ...

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

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

  4. 「2017 山东一轮集训 Day5」距离

    /* 写完开店再写这个题目顿时神清气爽, 腰也不疼了, 眼也不花了 首先考虑将询问拆开, 就是查询一些到根的链和点k的关系 根据我们开店的结论, 一个点集到一个定点的距离和可以分三部分算 那么就很简单 ...

  5. 「2017 山东一轮集训 Day4」棋盘(费用流)

    棋盘模型 + 动态加边 #include<cstdio> #include<algorithm> #include<iostream> #include<cs ...

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

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

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

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

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

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

  9. LOJ #6074. 「2017 山东一轮集训 Day6」子序列

    #6074. 「2017 山东一轮集训 Day6」子序列 链接 分析: 首先设f[i][j]为到第i个点,结尾字符是j的方案数,这个j一定是从i往前走,第一个出现的j,因为这个j可以代替掉前面所有j. ...

随机推荐

  1. SSIS - 4.使用表达式任务和脚本任务实现更改变量和输出变量值

    一.脚本任务 脚本任务是SSIS包中功能很强大的组件,尤其当内置的任务无法实现我们需要的功能的时候,我们都可以使用脚本任务来实现.脚本任务使用VSTA(Microsoft Visual Studio ...

  2. 对JS闭包和函数作用域的问题的深入讨论,如何理解JS闭包和函数作用域链?

    首先先引用<JavaScript权威指南>里面的一句话来开始我的博客:函数的执行依赖于变量作用域,这个作用域是在函数定义时决定的,而不是函数调用时决定的. 因此,就出现了如下的几串代码: ...

  3. 【安富莱专题教程第6期】SEGGER的J-Scope波形上位机软件,RTT模式波形上传速度可狂飙到500KB/S左右

    说明:1.在实际项目中,很多时候,我们需要将传感器或者ADC的数值以波形的形式显示.通常的解决办法是用串口上位机,USB接口上位机或者MDK的逻辑分析仪功能,使用这三种方式都比较繁琐.本期专题为大家讲 ...

  4. [SQL]LeetCode177. 第N高的薪水 | Nth Highest Salary

    Write a SQL query to get the nth highest salary from the Employee table. +----+--------+ | Id | Sala ...

  5. [Swift]LeetCode218. 天际线问题 | The Skyline Problem

    A city's skyline is the outer contour of the silhouette formed by all the buildings in that city whe ...

  6. [Swift]LeetCode266.回文全排列 $ Palindrome Permutation

    Given a string, determine if a permutation of the string could form a palindrome. For example," ...

  7. [Swift]LeetCode346. 从数据流中移动平均值 $ Moving Average from Data Stream

    Given a stream of integers and a window size, calculate the moving average of all integers in the sl ...

  8. [Swift]LeetCode746. 使用最小花费爬楼梯 | Min Cost Climbing Stairs

    On a staircase, the i-th step has some non-negative cost cost[i] assigned (0 indexed). Once you pay ...

  9. [Swift]LeetCode778. 水位上升的泳池中游泳 | Swim in Rising Water

    On an N x N grid, each square grid[i][j]represents the elevation at that point (i,j). Now rain start ...

  10. the python challenge闯关记录(0-8)

    0 第零关 2**38 = 274877906944 下一关的url:http://www.pythonchallenge.com/pc/def/274877906944.html 1 第一关 移位计 ...