loj#6073. 「2017 山東一輪集訓 Day5」距離(費用流)

自為風月馬前卒發表於2019-03-29

題意

題目連結

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;
}

相關文章