MatrixTime
limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 1792 Accepted Submission (s): 958
Problem Descriptionyifenfei 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 does 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 this area of the 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.
Inputthe input contains multiple test cases.
Each case first line given the integer n (2<n<30)
Than n Lines,each line include n positive integers. (<100)
Outputfor each test case output the maximal values Yifenfei can get.
Sample Input
210 35 10310 3 32 5 36 7 1051 2 3 4 52 3 4 5 63 4 5 6 74 5 6 7 85 6 7 8 9
Sample Output
284680
Analysis: Test instructions is to give a matrix, from (0,0) point to (n-1,n-1) point to (0,0) point to make this path node weight of the sum maximum, and from (0,0) to (n-1,n-1) point
can only go to the right or down, while the loop is the opposite, each node can only walk once. Because each node walks once, so to break the point, the capacity is 1, the cost is 0, the former part with S connection, the latter part with
T connection. There are also the rules of the direction, we can think: from S-t-s, this lap is equivalent to run two times s-t, just ran a few s node and T node, and then subtract it, if so
then we built the edge of the point, only to each node right and down, because we just from s-t, and so-called T-S can only go to the left and run, equivalent to S-t to the right and down, so it is almost solved.
run the maximum flow template can directly write the minimum cost of flow, as long as the addition of the costs to change to-cost, in the costflow changed to-costflow can be.
code Example:
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <queue>
#define Min (b) a<b?a:b
#define MAXN 40000+10
#define MAXM 20000+10
#define INF 0XFFFFFF
using namespace Std;
typedef struct
{
int from,to,next;
int val,cost;
}node;
Node E[MAXN];
int head[maxm],dis[maxm],visit[maxm],pre[maxm],pos[maxm],cnt;
void Init ()
{
memset (head,-1,sizeof (head));
cnt=0;
}
void Add (int from,int to,int val,int cost)
{
E[cnt].from=from,e[cnt].to=to,e[cnt].val=val,e[cnt].cost=cost;
e[cnt].next=head[from],head[from]=cnt++;
E[cnt].from=to,e[cnt].to=from,e[cnt].val=0,e[cnt].cost=-cost;
e[cnt].next=head[to],head[to]=cnt++;
}
BOOL SPFA (int s,int t,int N)
{
int to,val,cost;
for (int i=0;i<=n;i++)
{
Dis[i]=inf,visit[i]=0,pre[i]=-1;
}
Dis[s]=0,visit[s]=1,pre[s]=s;
queue<int>q;
Q.push (s);
while (! Q.empty ())
{
int K=q.front ();
Q.pop ();
visit[k]=0;
for (int i=head[k];i!=-1;i=e[i].next)
{
to=e[i].to;
Val=e[i].val;
Cost=e[i].cost;
if (Val>0&&dis[k]+cost<dis[to])
{
Dis[to]=dis[k]+cost;
Pre[to]=k;
Pos[to]=i;
if (Visit[to])
Continue
Visit[to]=1;
Q.push (to);
}
}
}
if (Pre[t]!=-1&&dis[t]<inf)
return true;
return false;
}
int maxcostflow (int s,int t,int N)
{
int costflow=0,netflow=0,min;
while (SPFA (s,t,n))
{
Min=inf;
for (int i=t;i!=s;i=pre[i])
{
Min=min (Min,e[pos[i]].val);
}
Netflow+=min;
COSTFLOW+=MIN*DIS[T];
for (int i=t;i!=s;i=pre[i])
{
E[pos[i]].val-=min;
E[pos[i]^1].val+=min;
}
}
Return-costflow;
}
int main ()
{
int s,t,n,x,sum;
while (~SCANF ("%d", &n))
{
Init ();
sum=0;
for (int i=0;i<n;i++)
for (int j=0;j<n;j++)
{
scanf ("%d", &x);
if ((i==0&&j==0) | | | (i==n-1&&j==n-1))
{
Add (i*n+j,i*n+j+n*n,2,-x);
Sum+=x;
}
Else
{
Add (i*n+j,i*n+j+n*n,1,-x);
}
if (i+1<n)
{
Add (I*n+j+n*n, (i+1) *n+j,1,0);
}
if (j+1<n)
{
Add (i*n+j+n*n,i*n+j+1,1,0);
}
}
s=2*n*n+1,t=2*n*n+2;
Add (s,0,2,0);
Add (2*n*n-1,t,2,0);
printf ("%d\n", Maxcostflow (s,t,t+10)-sum);
}
return 0;
}
HDU 2686 Matrix "Maximum cost flow"