Yifenfei very like play a number game in the n*n Matrix. A positive integer number is put in each area of the Matrix.
Every time yifenfei should to do is that choose a detour which frome the top left point to the bottom right point and than back to the top left point with the maximal values of sum integers that area of Matrix yifenfei choose. But from the top to the bottom can only choose right and down, from the bottom to the top can only choose left and up. And yifenfei can not pass the same area of the Matrix except the start and end.
Every time yifenfei should to do is that choose a detour which frome the top left point to the bottom right point and than back to the top left point with the maximal values of sum integers that area of Matrix yifenfei choose. But from the top to the bottom can only choose right and down, from the bottom to the top can only choose left and up. And yifenfei can not pass the same area of the Matrix except the start and end.
Each case first line given the integer n (2<n<30)
Than n lines,each line include n positive integers.(<100)
2 10 3 5 10 3 10 3 3 2 5 3 6 7 10 5 1 2 3 4 5 2 3 4 5 6 3 4 5 6 7 4 5 6 7 8 5 6 7 8 9
28 46 80
- 最大费用流,在图上做spfa时求最长路径即可
- //用挑战程序设计上的第一个模板+spfa即vector实现的邻接表 + spfa会超时,改成用数组实现的邻接表就没事了。。。
- //建图时,因为每个点只能走一次,所以要拆点,容量为1,起点和终点要走两次,容量为2,然后各点之间容量为1,最后套模板就好了
- //总结;图中点只走一次时,一定记得拆点且容量为1,边只能走一次则边容量为1
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
const int N = 1e6+10;
const int inf = 0x3f3f3f3f;
typedef pair<int,int>P;
struct edge
{
int from, to, cap, cost, next;
}p[N];
int cnt, head[N];
void add(int u,int v,int cap,int w)
{
p[cnt].from=u, p[cnt].to=v, p[cnt].cost=w,p[cnt].cap=cap, p[cnt].next=head[u], head[u]=cnt++;
p[cnt].from=v, p[cnt].to=u, p[cnt].cost=-w,p[cnt].cap=0, p[cnt].next=head[v], head[v]=cnt++;
return ;
}
int d[N], used[N], prev1[N];
int min_cost_flow(int s,int t,int f)
{
int rev=0;
while(f>0)
{
memset(d,-1,sizeof(d));
memset(used,0,sizeof(used));
memset(prev1,-1,sizeof(prev1));
queue<int>q;
q.push(s);
d[s]=0, used[s]=1;
while(!q.empty())
{
int u=q.front();q.pop();
for(int i=head[u];i!=-1;i=p[i].next)
{
int v=p[i].to;
if(p[i].cap>0&&d[v]<d[u]+p[i].cost)
{
d[v]=d[u]+p[i].cost;
prev1[v]=i;
if(!used[v])
{
q.push(v), used[v]=1;
}
}
}
used[u]=0;
}
if(d[t]==-1) return -1;
int flow=f;
for(int i=prev1[t];i!=-1;i=prev1[p[i].from])
{
flow=min(flow,p[i].cap);
}
rev+=d[t]*flow;
f-=flow;
for(int i=prev1[t];i!=-1;i=prev1[p[i].from])
p[i].cap-=flow, p[i^1].cap+=flow;
}
return rev;
}
int a[2000][2000];
int main()
{
int n;
while(scanf("%d", &n)!=EOF)
{
cnt=0;
memset(head,-1,sizeof(head));
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
scanf("%d", &a[i][j]);
}
}
int x=n*n;
int s=0, e=2*x+1;
add(s,n*n+1,2,0),add(n*n,e,2,0);
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
add(n*(i-1)+j,x+n*(i-1)+j,1,a[i][j]);
if(i<=n-1) add(x+n*(i-1)+j,i*n+j,1,0);
if(j<=n-1) add(x+n*(i-1)+j,n*(i-1)+j+1,1,0);
}
}
x=min_cost_flow(s,e,2);
printf("%d\n",x+a[1][1]+a[n][n]);
}
return 0;
}
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
int a[40][40], dp[100][40][40];
int max1(int x,int y,int z,int o)
{
int s1=max(x,y), s2=max(z,o);
return max(s1,s2);
}
int main()
{
int n;
while(scanf("%d", &n)!=EOF)
{
memset(dp,0,sizeof(dp));
memset(a,0,sizeof(a));
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
scanf("%d", &a[i][j]);
}
}
int k;
for(k=1;k<2*n-2;k++)
{
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(i==j) continue;
dp[k][i][j]=max1(dp[k-1][i][j],dp[k-1][i-1][j],dp[k-1][i][j-1],dp[k-1][i-1][j-1]);
dp[k][i][j]+=a[i][k-i]+a[j][k-j];
}
}
}
dp[k][n-1][n-1]=max(dp[k-1][n-1][n-2],dp[k-1][n-2][n-1]);
dp[k][n-1][n-1]+=a[0][0]+a[n-1][n-1];
printf("%d\n",dp[k][n-1][n-1]);
}
return 0;
}