HDU 3376 Matrix Again 费用流(不MLE的最小费用最大流模板)

点击打开链接

 

 

Matrix Again

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


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
 


 

Author
Starvae
 


 

Source
 


 

Recommend
lcy

 

 

题意:从n*n矩阵的最左上角的点走到最右下角的点,只能往右或者往下走,然后从最右下角的点走到最左上角的点,只能往上或者往左走。求走过的路最大的数。

此题属于费用流,而难在建图上,建好图之后直接套一个好的模板,就会AC。那么如何建图呢?将矩阵中的每个点都拆成 i 和 n*n+i 。费用是该点的值,容量是1,就能控制走一次。起点和终点的容量是2.源点和起点相连容量是2,费用是0.汇点和终点相连,容量是2,费用是0.

#include<iostream>
#include<algorithm>
#include<cstring>
#include<queue>
#include<cstdio>
using namespace std;
const int MAXN=610*610*2+2;
const int inf=1<<29;
int pre[MAXN];          // pre[v] = k:在增广路上,到达点v的边的编号为k
int dis[MAXN];          // dis[u] = d:从起点s到点u的路径长为d
int vis[MAXN];         // inq[u]:点u是否在队列中
int path[MAXN];
int head[MAXN];
int NE,tot,ans,max_flow,map[666][666];
struct node
{
    int u,v,cap,cost,next;
} Edge[MAXN<<2];
void addEdge(int u,int v,int cap,int cost)
{
    Edge[NE].u=u;
    Edge[NE].v=v;
    Edge[NE].cap=cap;
    Edge[NE].cost=cost;
    Edge[NE].next=head[u];
    head[u]=NE++;
    Edge[NE].v=u;
    Edge[NE].u=v;
    Edge[NE].cap=0;
    Edge[NE].cost=-cost;
    Edge[NE].next=head[v];
    head[v]=NE++;
}
int SPFA(int s,int t)                   //  源点为0,汇点为sink。
{
    int i;
    for(i=s;i<=t;i++) dis[i]=inf;
    memset(vis,0,sizeof(vis));
    memset(pre,-1,sizeof(pre));
    dis[s] = 0;
    queue<int>q;
    q.push(s);
    vis[s] =1;
 while(!q.empty())        //  这里最好用队列,有广搜的意思,堆栈像深搜。
    {
        int u =q.front();
        q.pop();
        for(i=head[u]; i!=-1;i=Edge[i].next)
        {
            int v=Edge[i].v;
            if(Edge[i].cap >0&& dis[v]>dis[u]+Edge[i].cost)
            {
                dis[v] = dis[u] + Edge[i].cost;
                pre[v] = u;
                path[v]=i;
                if(!vis[v])
                {
                    vis[v] =1;
                    q.push(v);
                }
            }
        }
        vis[u] =0;
    }
    if(pre[t]==-1)
        return 0;
    return 1;
}
void end(int s,int t)
{
    int u, sum = inf;
    for(u=t; u!=s; u=pre[u])
    {
        sum = min(sum,Edge[path[u]].cap);
    }
    max_flow+=sum;                          //记录最大流
    for(u = t; u != s; u=pre[u])
    {
        Edge[path[u]].cap -= sum;
        Edge[path[u]^1].cap += sum;
        ans += sum*Edge[path[u]].cost;     //  cost记录的为单位流量费用,必须得乘以流量。
    }
}
int main()
{
    int i,j,n,s,t;
    while(scanf("%d",&n)!=EOF)
    {
        memset(head,-1,sizeof(head));
        NE=ans=max_flow=s=0;
        for(i=1;i<=n;i++)
        {
            for(j=1;j<=n;j++)
            {
                scanf("%d",&map[i][j]);
            }
        }
        int k=n*n;
        t=2*k+1;
         for(i=1;i<=n;i++)
        {
            for(j=1;j<=n;j++)
            {
                addEdge(j+(i-1)*n,k+j+(i-1)*n,1,-map[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(s,1,2,0);
        addEdge(1,k+1,1,0);
        addEdge(2*k,t,2,0);
        addEdge(k,2*k,1,0);
        while(SPFA(s,t))
        {
            end(s,t);
        }
        printf("%d\n",-ans);
    }
    return 0;
}


 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值