题意
题目链接
Sol
这题能想到费用流就不难做了
从S向(1, 1)连费用为0,流量为K的边
从(n, n)向T连费用为0,流量为K的边
对于每个点我们可以拆点限流,同时为了保证每个点只被经过一次,需要拆点。
对于拆出来的每个点,在其中连两条边,一条为费用为点权,流量为1,另一条费用为0,流量为INF
相邻两个点之间连费用为0,流量为INF的边。
跑最大费用最大流即可
代码语言:javascript复制#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
*/