洛谷P2045 方格取数加强版(费用流)
程序员文章站
2022-06-16 10:41:03
题意 "题目链接" Sol 这题能想到费用流就不难做了 从S向(1, 1)连费用为0,流量为K的边 从(n, n)向T连费用为0,流量为K的边 对于每个点我们可以拆点限流,同时为了保证每个点只被经过一次,需要拆点。 对于拆出来的每个点,在其中连两条边,一条为费用为点权,流量为1,另一条费用为0,流量 ......
题意
sol
这题能想到费用流就不难做了
从s向(1, 1)连费用为0,流量为k的边
从(n, n)向t连费用为0,流量为k的边
对于每个点我们可以拆点限流,同时为了保证每个点只被经过一次,需要拆点。
对于拆出来的每个点,在其中连两条边,一条为费用为点权,流量为1,另一条费用为0,流量为inf
相邻两个点之间连费用为0,流量为inf的边。
跑最大费用最大流即可
#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 fin(x) {freopen(#x".in","r",stdin);} #define fout(x) {freopen(#x".out","w",stdout);} //#define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1<<22, stdin), p1 == p2) ? eof : *p1++) //char buf[(1 << 22)], *p1 = buf, *p2 = buf; using namespace std; const int maxn = 51, max = 1e5 + 10, mod = 1e9 + 7, inf = 1e9 + 10; const double eps = 1e-9, pi = acos(-1); 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;} 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') % mod, c = getchar(); return x * f; } int n, k, s = 0, t = 1e5 - 1, a[maxn][maxn], dis[max], vis[max], pre[max], id[maxn][maxn][2], cnt, maxcost; struct edge { int u, v, w, f, nxt; }e[max]; int head[max], num; inline void add_edge(int x, int y, int w, int f) { e[num] = (edge) {x, y, w, f, head[x]}; head[x] = num++; } inline void ae(int x, int y, int w, int f) { add_edge(x, y, w, f); add_edge(y, x, -w, 0); } bool spfa() { queue<int> q; q.push(s); memset(dis, -0x3f, sizeof(dis)); memset(vis, 0, sizeof(vis)); 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; if(e[i].f && dis[to] < dis[p] + e[i].w) { dis[to] = dis[p] + e[i].w; pre[to] = i; if(!vis[to]) vis[to] = 1, q.push(to); } } } return dis[t] > -inf; } void f() { int canflow = inf; for(int i = t; i != s; i = e[pre[i]].u) chmin(canflow, e[pre[i]].f); for(int i = t; i != s; i = e[pre[i]].u) e[pre[i]].f -= canflow, e[pre[i] ^ 1].f += canflow; maxcost += canflow * dis[t]; } void mcmf() { while(spfa()) f(); } signed main() { // freopen("a.in", "r", stdin); memset(head, -1, sizeof(head)); n = read(); k = read(); for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++) a[i][j] = read(), id[i][j][0] = ++cnt, id[i][j][1] = ++cnt; ae(s, id[1][1][0], 0, k); ae(id[n][n][1], t, 0, k); for(int i = 1; i <= n; i++) { for(int j = 1; j <= n; j++) { ae(id[i][j][0], id[i][j][1], a[i][j], 1); ae(id[i][j][0], id[i][j][1], 0, inf); if(i + 1 <= n) ae(id[i][j][1], id[i + 1][j][0], 0, inf); if(j + 1 <= n) ae(id[i][j][1], id[i][j + 1][0], 0, inf); } } mcmf(); printf("%d", maxcost); return 0; } /* 3 2 1 2 3 0 2 1 1 4 2 */