传送门:点击打开链接
n太大了所以不能使用O(n^4)的dp来做了,仅仅能考虑更复杂的费用流
基本的问题还是难在怎样建图
将点拆分成两个,分别用i和i+n*n来表示
对于n*n个点,从i到i+n*n建一条边费用是-A[i][j]容量是1。来表示路过(i,j)获取的值
然后从第二层建两条边。连向右边和下边的两个点的第一层,让点再次回到第一层。费用是0
这样下来。点从第一层到第二层,表示获取了这个点的值,并且假设不获取这个值,就无法到达第二层,就无法继续扩展了。
所以就限制了每一个点仅仅能走一次了~
最后在考虑怎样让(1,1)和(n,n)的费用仅仅算一次,从源点出来后一个连向(1,1)的第一层。一个连向(1,1)的第二层。这样(1,1)的值就仅仅会被算一次
汇点也是一样。从(n,n)的第一层和(n,n)的第二层都连向汇点即可了
然后再加上读入挂,。用G++提交,就不会TLE了
#include<map>
#include<set>
#include<cmath>
#include<stack>
#include<queue>
#include<cctype>
#include<cstdio>
#include<string>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
#define FIN freopen("input.txt","r",stdin)
#define FOUT freopen("output.txt","w+",stdout)
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const int MX = 1e6 + 5;//都开4倍把..
const int MM = 3e6 + 5;
const int MS = 1000 + 5;
const int INF = 0x3f3f3f3f;
struct Edge {
int to, next, cap, flow, cost;
Edge() {}
Edge(int _to, int _next, int _cap, int _flow, int _cost) {
to = _to; next = _next; cap = _cap; flow = _flow; cost = _cost;
}
} E[MM];
int Head[MX], tol;
int pre[MX]; //储存前驱顶点
int dis[MX]; //储存到源点s的距离
bool vis[MX];
int N;//节点总个数,节点编号从0~N-1
void init(int n) {
tol = 0;
N = n + 2;
memset(Head, -1, sizeof(Head));
}
void edge_add(int u, int v, int cap, int cost) {
E[tol] = Edge(v, Head[u], cap, 0, cost);
Head[u] = tol++;
E[tol] = Edge(u, Head[v], 0, 0, -cost);
Head[v] = tol++;
}
bool spfa(int s, int t) {
queue<int>q;
for (int i = 0; i < N; i++) {
dis[i] = INF;
vis[i] = false;
pre[i] = -1;
}
dis[s] = 0;
vis[s] = true;
q.push(s);
while (!q.empty()) {
int u = q.front();
q.pop();
vis[u] = false;
for (int i = Head[u]; i != -1; i = E[i].next) {
int v = E[i].to;
if (E[i].cap > E[i].flow && dis[v] > dis[u] + E[i].cost) {
dis[v] = dis[u] + E[i].cost;
pre[v] = i;
if (!vis[v]) {
vis[v] = true;
q.push(v);
}
}
}
}
if (pre[t] == -1) return false;
else return true;
}
//返回的是最大流, cost存的是最小费用
int minCostMaxflow(int s, int t, int &cost) {
int flow = 0;
cost = 0;
while (spfa(s, t)) {
int Min = INF;
for (int i = pre[t]; i != -1; i = pre[E[i ^ 1].to]) {
if (Min > E[i].cap - E[i].flow)
Min = E[i].cap - E[i].flow;
}
for (int i = pre[t]; i != -1; i = pre[E[i ^ 1].to]) {
E[i].flow += Min;
E[i ^ 1].flow -= Min;
cost += E[i].cost * Min;
}
flow += Min;
}
return flow;
}
int n, A[MS][MS];
int dist[][2] = {{0, 1}, {1, 0}};
inline int ID(int x, int y, int p = 0) {
return (x - 1) * n + y + p * n * n;
}
inline int read() {
char c = getchar();
while(!isdigit(c)) c = getchar();
int x = 0;
while(isdigit(c)) {
x = x * 10 + c - '0';
c = getchar();
}
return x;
}
int main() {
//FIN;
while(~scanf("%d", &n)) {
int s = 0, t = 2 * n * n; init(t);
edge_add(s, ID(1, 1), 1, 0);
edge_add(s, ID(1, 1, 1), 1, 0);
edge_add(ID(n, n), t, 1, 0);
edge_add(ID(n, n, 1), t, 1, 0);
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= n; j++) {
A[i][j] = read();
edge_add(ID(i, j), ID(i, j, 1), 1, -A[i][j]);
for(int k = 0; k < 2; k++) {
int nx = i + dist[k][0];
int ny = j + dist[k][1];
if(nx <= n && ny <= n) {
edge_add(ID(i, j, 1), ID(nx, ny), 1, 0);
}
}
}
}
int ans = 0;
minCostMaxflow(s, t, ans);
printf("%d\n", -ans);
}
return 0;
}