# Matrix Again

Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 102400/102400 K (Java/Others)
Total Submission(s): 4196    Accepted Submission(s): 1212

Problem Description
Starvae 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 starvae should to do is that choose a detour which from 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 starvae 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 starvae can not pass the same area of the Matrix except the start and end..
Do you know why call this problem as “Matrix Again”? AS it is like the problem 2686 of HDU.

Input
The input contains multiple test cases.
Each case first line given the integer n (2<=n<=600)
Then n lines, each line include n positive integers. (<100)

Output
For each test case output the maximal values starvae can get.

Sample Input
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

Sample Output
28
46
80

### 题意

10 3 3
2 5 3
6 7 10

• 第一条路径： 10→3→5→3→10=31
• 第二条路径： 10→2→6→7→10=35

### AC代码：

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<vector>
#include<queue>
using namespace std;

const int MAXX = 800010;
const int INF = 0x3f3f3f3f;

struct edge
{
int to;     //边终点
int next;   //下一个兄弟位置
int cap;    //容量
int flow;   //流量
int cost;   //费用
} edge[MAXX<<2];

int head[MAXX],tol;
int pre[MAXX],dis[MAXX];
int N;  //节点总个数

void init(int n)
{
N=n;
tol=0;
memset(head,-1,sizeof(head));
}

void addedge(int u,int v,int cap,int cost)  //同时增加原边与反向边
{
edge[tol].to=v;
edge[tol].cap=cap;
edge[tol].cost=cost;
edge[tol].flow=0;
edge[tol].next=head[u];
head[u]=tol++;
edge[tol].to=u;
edge[tol].cap=0;
edge[tol].cost=-cost;
edge[tol].flow=0;
edge[tol].next=head[v];
head[v]=tol++;
}

/*
* SPFA 算法判断是否存在s到t的通路
*/
bool spfa(int s,int t)
{
queue<int>q;
bool vis[MAXX];
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=edge[i].next)   //遍历所有与u临接的点
{
int v=edge[i].to;
if(edge[i].cap>edge[i].flow&&dis[v]>dis[u]+edge[i].cost)    //如果可以松弛该点
{
dis[v]=dis[u]+edge[i].cost;
pre[v]=i;
if(!vis[v]) //如果该点不在队列中，入队
{
vis[v]=true;
q.push(v);
}
}
}
}
return (pre[t]!=-1);    //返回是否s到t是否有路径
}

/*
* int s 起点
* int t 终点
* 返回费用cost
*/
int minCostMaxFlow(int s,int t)
{
int cost=0;
while(spfa(s,t))
{
int minn=INF;   //当前路径可增广值
for(int i=pre[t]; i!=-1; i=pre[edge[i^1].to])   //因为建图时每增加一条边会同时加入它的反向边，因此i^1为找出与i刚好相反的部分
{
if(minn>edge[i].cap-edge[i].flow)
minn=edge[i].cap-edge[i].flow;
}
for(int i=pre[t]; i!=-1; i=pre[edge[i^1].to])   //修改图，计算花费
{
edge[i].flow+=minn;
edge[i^1].flow-=minn;
cost+=edge[i].cost*minn;
}
}
return cost;
}

int main()
{
int n;
int mapp[605][605];
while(~scanf("%d",&n))
{
int k=n*n;
int t=2*k+1;
init(t+1);
for(int i=1; i<=n; i++)
for(int j=1; j<=n; j++)
{
scanf("%d",&mapp[i][j]);
addedge(j+(i-1)*n,j+(i-1)*n+k,1,-mapp[i][j]);
if(j!=n)addedge(k+j+(i-1)*n,j+1+(i-1)*n,INF,0);
if(i!=n)addedge(k+j+(i-1)*n,i*n+j,INF,0);
}
addedge(0,1,2,0);
addedge(1,k+1,1,0);
addedge(t-1,t,2,0);
addedge(k,t-1,1,0);
printf("%d\n",-minCostMaxFlow(0,t));
}
return 0;
}