Matrix
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2485 Accepted Submission(s): 1314
Problem Description
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.
Input
The 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)
Each case first line given the integer n (2<n<30)
Than n lines,each line include n positive integers.(<100)
Output
For each test case output the maximal values yifenfei 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
Author
yifenfei
Source
题意:
从n*n方阵的左上角走到右下角(只往下或右走)再从右下角回到左上角(只往上或左走)的最大费用。
代码:
//费用流模板题,因为题目中的表述就是一个网络流模型,拆点建图,没点只经过一次,起点终点容量为2,
//其他点容量为1,要求最大费用费用为负权值,套模板。起点和终点经历了两次,最后要减去。
/****************最小费用最大流模板,白书363页*******************************/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<vector>
using namespace std;
const int maxn=**+,inf=0x7fffffff;//本体拆点,数组多开两倍
struct Edge
{
int from,to,cap,flow,cost;
Edge(int u,int v,int c,int f,int cs):from(u),to(v),cap(c),flow(f),cost(cs){}
};
struct MCMF
{
int n,m,s,t;
vector<Edge>edges;
vector<int>g[maxn];
int inq[maxn],d[maxn],p[maxn],a[maxn];
void init(int n)
{
this->n=n;
for(int i=;i<n;i++) g[i].clear();
edges.clear();
}
void AddEdge(int from,int to,int cap,int cost)
{
edges.push_back((Edge){from,to,cap,,cost});
edges.push_back((Edge){to,from,,,-cost});
m=edges.size();
g[from].push_back(m-);
g[to].push_back(m-);
}
bool BellmanFord(int s,int t,int &flow,int &cost)
{
for(int i=;i<n;i++) d[i]=inf;
memset(inq,,sizeof(inq));
d[s]=;inq[s]=;p[s]=;a[s]=inf;
queue<int>q;
q.push(s);
while(!q.empty()){
int u=q.front();q.pop();
inq[u]=;
for(int i=;i<(int)g[u].size();i++){
Edge &e=edges[g[u][i]];
if(e.cap>e.flow&&d[e.to]>d[u]+e.cost){
d[e.to]=d[u]+e.cost;
p[e.to]=g[u][i];
a[e.to]=min(a[u],e.cap-e.flow);
if(!inq[e.to]) {q.push(e.to);inq[e.to]=;}
}
}
}
if(d[t]==inf) return false;
flow+=a[t];
cost+=d[t]*a[t];
int u=t;
while(u!=s){
edges[p[u]].flow+=a[t];
edges[p[u]^].flow-=a[t];
u=edges[p[u]].from;
}
return true;
}
int Mincost(int s,int t)
{
int flow=,cost=;
while(BellmanFord(s,t,flow,cost));
return cost;//返回最小费用,flow存最大流
}
}MC;
/**********************************************************************************/
int main()
{
int n,mp[][];
while(scanf("%d",&n)==){
int s=,t=n*n*+;
MC.init(n*n*+);
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
scanf("%d",&mp[i][j]);
for(int i=;i<=n;i++){
for(int j=;j<=n;j++){
int id=(i-)*n+j;
if(id==){
MC.AddEdge(id,id+n*n,,-mp[i][j]);
MC.AddEdge(s,id,,);
}
else if(id==n*n){
MC.AddEdge(id,id+n*n,,-mp[i][j]);
MC.AddEdge(id+n*n,t,,);
}
else MC.AddEdge(id,id+n*n,,-mp[i][j]);
if(i<n){
int nid=id+n;
MC.AddEdge(id+n*n,nid,,);
}
if(j<n){
int nid=id+;
MC.AddEdge(id+n*n,nid,,);
}
}
}
int ans=-(MC.Mincost(,n*n*+)+mp[][]+mp[n][n]);
printf("%d\n",ans);
}
return ;
}