hdu2686 Matrix 最大费用最大流

这道题目和poj3422差不多  ---->>>>>点击打开链接

给出一个矩阵,每个点都有一个权值。找出一条路径:从最左上角的点到最右下角的点,再从最右下角的点到最左上角的点,使途中经过的点的权值总和最大。并且除了最左上角和最右下角的点,每个点只能经过一次,最左上角的点和最右下角的点的权值只算一次。

思路:

拆点,把矩阵里的每一个点拆成两个点为x和x'。每个x连一条边到x',费用为该点的权值,容量为1。

每个点的x'向右面和下面的点y各连一条边,费用为0,容量为1。

源点到1的容量inf,费用为0,n×n的点到汇点容量为inf,费用为0;

1到1'和2×n×n到汇点 再连一条费用为0,容量为inf的边

 


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
 

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;
#define INF 0x3f3f3f3f
struct node
{
    int u,v,w,f,next;
} edge[50000];
int s,T,cnt;
int head[55000],pre[55000],vis[50500],dis[55000];
    int map[1000][1000];
void add(int u,int v,int w,int f)
{
    edge[cnt].u=u;
    edge[cnt].v=v;
    edge[cnt].w=w;
    edge[cnt].f=f;
    edge[cnt].next=head[u];
    head[u]=cnt++;
    edge[cnt].u=v;
    edge[cnt].v=u;
    edge[cnt].w=0;
    edge[cnt].f=-f;
    edge[cnt].next=head[v];
    head[v]=cnt++;
}
int SPFA()
{
    int i;
    memset(pre,-1,sizeof(pre));
    memset(vis,0,sizeof(vis));
    for(i=0; i<=cnt; i++)
        dis[i]=-1;
    queue<int>q;
    dis[s]=0;
    vis[s]=1;
    q.push(s);
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        i=head[u];
        vis[u]=0;
        while(i!=-1)
        {
            if(edge[i].w>0&&dis[edge[i].v]<dis[u]+edge[i].f)
            {
                dis[edge[i].v]=dis[u]+edge[i].f;
                pre[edge[i].v]=i;
                if(!vis[edge[i].v])
                {
                    vis[edge[i].v]=1;
                    q.push(edge[i].v);
                }
            }
            i=edge[i].next;
        }
    }
    if(pre[T]==-1)
        return 0;
    return 1;
}
int MincostMaxFlow()
{
    int ans=0;
    while(SPFA())
    {
        int maxl=INF;
        int p=pre[T];
        while(p!=-1)
        {
            maxl=min(maxl,edge[p].w);
            p=pre[edge[p].u];
        }
        p=pre[T];
        while(p!=-1)
        {
            edge[p].w-=maxl;
            edge[p^1].w+=maxl;
            ans+=maxl*edge[p].f;
            p=pre[edge[p].u];
        }
    }
    return ans;
}
int main()
{
    int n;

    int a,b;
    while(~scanf("%d",&n))
    {
        cnt=0;
        memset(head,-1,sizeof(head));
        for(int i=1; i<=n; i++)
            for(int j=1; j<=n; j++)
                scanf("%d",&map[i][j]);
        for(int i=1; i<=n; i++)
            for(int j=1; j<=n; j++)
            {
                a=(i-1)*n+j;
                b=a+n*n;
                add(a,b,1,map[i][j]);
                add(a,b,INF,0);
                if(i<n)
                    add(b,a+n,2,0);
                if(j<n)
                    add(b,a+1,2,0);
            }
        s=0;
        T=2*n*n+1;
        add(s,1,2,0);
        add(2*n*n,T,2,0);
        int ans=0;
        ans+=MincostMaxFlow();
        printf("%d\n",ans);
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值