题意
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 %dn", 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; }
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/c-cdevelopment/604514.html