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;
}
上一篇:一个不安装Oracle客户端的方法------未经试验


下一篇:go微服务框架go-micro深度学习(四) rpc方法调用过程详解