HDU3376 最小费用最大流 模板2

本文介绍了一个基于最小费用最大流算法解决的问题MatrixAgain。该问题是关于在一个矩阵中寻找两条路径(一条从左上到右下,再从右下返回左上)以获得最大的数值总和,同时避免重复经过同一元素(除了起点和终点)。文章提供了完整的C++代码实现,并针对大范围的数据输入进行了优化。

Matrix Again

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


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
 代码:
//和HDU2686一样,只是数据变大了,上一个模板会超内存,这个板不会。
/***********************最小费用最大流模板2*************************/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int maxn=610*610*2+2;
const int maxm=4*maxn;//!边数要够
const int inf=0x7fffffff;
struct Edge
{
    int to,next,cap,flow,cost;
}edges[maxm];
int head[maxn],tol,pre[maxn],dis[maxn];
bool vis[maxn];
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)
{
    edges[tol].to=v;
    edges[tol].cap=cap;
    edges[tol].cost=cost;
    edges[tol].flow=0;
    edges[tol].next=head[u];
    head[u]=tol++;
    edges[tol].to=u;
    edges[tol].cap=0;
    edges[tol].cost=-cost;
    edges[tol].flow=0;
    edges[tol].next=head[v];
    head[v]=tol++;
}
bool spfa(int s,int t)
{
    queue<int>q;
    for(int i=0;i<=N;i++){
        dis[i]=inf;
        vis[i]=0;
        pre[i]=-1;
    }
    dis[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=edges[i].next){
            int v=edges[i].to;
            if(edges[i].cap>edges[i].flow&&dis[v]>dis[u]+edges[i].cost){
                dis[v]=dis[u]+edges[i].cost;
                pre[v]=i;
                if(!vis[v]) {vis[v]=1;q.push(v);}
            }
        }
    }
    if(pre[t]==-1) return 0;
    return 1;
}
int MinCostFlow(int s,int t)
{
    int flow=0,cost=0;
    while(spfa(s,t)){
        int Min=inf;
        for(int i=pre[t];i!=-1;i=pre[edges[i^1].to])
            Min=min(Min,edges[i].cap-edges[i].flow);
        for(int i=pre[t];i!=-1;i=pre[edges[i^1].to]){
            edges[i].flow+=Min;
            edges[i^1].flow-=Min;
            cost+=edges[i].cost*Min;
        }
        flow+=Min;
    }
    return cost;//返回最小费用,flow存最大流
}
/*********************************************************************/
int main()
{
    int n,mp[605][605];
    while(scanf("%d",&n)==1){
        for(int i=1;i<=n;i++)
            for(int j=1;j<=n;j++) scanf("%d",&mp[i][j]);
        int s=0,t=n*n*2+1;
        init(n*n*2+2);
        for(int i=1;i<=n;i++){
            for(int j=1;j<=n;j++){
                int id=(i-1)*n+j;
                if(id==1){
                    AddEdge(id,id+n*n,2,-mp[i][j]);
                    AddEdge(s,id,2,0);
                }
                else if(id==n*n){
                    AddEdge(id,id+n*n,2,-mp[i][j]);
                    AddEdge(id+n*n,t,2,0);
                }
                else AddEdge(id,id+n*n,1,-mp[i][j]);
                if(i<n) AddEdge(id+n*n,id+n,1,0);
                if(j<n) AddEdge(id+n*n,id+1,1,0);
            }
        }
        int ans=-(MinCostFlow(s,t)+mp[1][1]+mp[n][n]);
        printf("%d\n",ans);
    }
    return 0;
}

 

 

转载于:https://www.cnblogs.com/--ZHIYUAN/p/6496876.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值