您的位置:首页 > 其它

hdu 2686 Matrix(费用流,拆点)

2016-08-03 10:02 344 查看


Matrix

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 2355    Accepted Submission(s): 1243


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. 

 

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)

 

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

题意:有一个n*n的矩阵,现在要从左上角走到右下角并且每个点只能走一次(除开起点和终点),再从终点走到起点,求最大的权值,从左上角走到右下角只能走右边和下边,右下角走到左上角只能走左边和上边

思路:很明显的费用流模型,其实就相当于(1,1)出发走到(n,n)走两次求最大权值。不过因为这个每个点只能走一次,所以我们把每个点都拆成两个点,中间连一条容量为1的边即可,注意数组开大点,30*30*2

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
using namespace std;
#define N 2010
#define INF 0x3f3f3f3f
struct Edge
{
int from,to,next,cap,cost;///起点,终点,同起点下一条边,残余流量,费用
} edge[N*N];
int cnt,head
;
int vis
,d
,pp
;
int dir[2][2]={1,0,0,1};
int ma[55][55];
int sumflow;///最大流量总和
void init()
{
cnt=0;
memset(head,-1,sizeof(head));
}
void addedge(int from,int to,int cap,int cost)
{
edge[cnt].from=from;
edge[cnt].to=to;
edge[cnt].cost=cost;
edge[cnt].cap=cap;
edge[cnt].next=head[from];
head[from]=cnt++;
edge[cnt].from=to;
edge[cnt].to=from;
edge[cnt].cost=-cost;
edge[cnt].cap=0;
edge[cnt].next=head[to];
head[to]=cnt++;///存反向边
}
int spfa(int s,int t,int n)
{
queue<int>q;
memset(vis,0,sizeof(vis));
memset(pp,-1,sizeof(pp));///pp[i]表示最短路径上以i为终点的边的编号
for(int i=0; i<=n; i++)
d[i]=-INF;
d[s]=0;
vis[s]=1;
q.push(s);
while(!q.empty())
{
int u=q.front();
q.pop();
vis[u]=0;
for(int i=head[u]; i!=-1; i=edge[i].next)
{
int v=edge[i].to;
if(edge[i].cap>0&&d[v]<d[u]+edge[i].cost)
{
d[v]=d[u]+edge[i].cost;
pp[v]=i;
if(!vis[v])
{
vis[v]=1;
q.push(v);
}
}
}
}
if(d[t]==-INF) return 0;///找不到一条到终点的路
return 1;
}
int MCMF(int s,int t,int n)
{
int mincost=0,minflow,flow=0;///最小费用,路径中最小流量,总流量
while(spfa(s,t,n))///找当前的最短路
{
minflow=INF+1;
for(int i=pp[t]; i!=-1; i=pp[edge[i].from])
minflow=min(minflow,edge[i].cap);///从路径中找最小的流量
flow+=minflow;///总流量加上最小流量
for(int i=pp[t]; i!=-1; i=pp[edge[i].from])
{
edge[i].cap-=minflow;///当前边减去最小流量
edge[i^1].cap+=minflow;///反向边加上最小流量
}
mincost+=d[t]*minflow;///最小费用等于路径和*每条路径的流量(经过多少次)
}
sumflow=flow;
return mincost;
}
int main()
{
int n;
while(~scanf("%d",&n))
{
init();
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
scanf("%d",&ma[i][j]);
int s=1,t=2*n*n;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if((i==1&&j==1)||(i==n&&j==n)) addedge((i-1)*n+j,(i-1)*n+j+n*n,2,ma[i][j]);
else addedge((i-1)*n+j,(i-1)*n+j+n*n,1,ma[i][j]);
for(int k=0;k<2;k++)
{
int x=i+dir[k][0],y=j+dir[k][1];
if(x<1||x>n||y<1||y>n) continue;
addedge((i-1)*n+j+n*n,(x-1)*n+y,1,0);
}
}
}
printf("%d\n",MCMF(s,t,t)-ma[1][1]-ma

);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: