hdu 3376 Matrix Again(最小费用流)

Matrix Again

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



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
 

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3376

题目大意:给你一个矩阵,规定从左上往右下走,只能往右或往下走,到右下之后再返回左上,不能经过相同的点,问路程中所有数的和的最大值。

思路分析:如果只走一遍,或者可以走相同的格子,那么就是一个简单的记忆化搜索的事情,但这里不能经过相同的点,增加了负担。联系到这道题为流量的题目是在建图中可以很好的表示每个点只走一次(只需要流量为1),这里还有一个问题需要转化:
     从左上到右下再返回左上,再建图中如何表示?? 这里只需要看做是从左上往右下走两遍就好,在图上表示变为源点和汇点流量为2。

心路历程: 前面的都一口气分析到了,但是写下了就是wa,不知为何。查看大神博客,原来他们都拆过点,体现为建图方式的不同。
我的建图方式:
     直接将点看做边,构建一个虚拟起点,与第一个点流量为2,其余个点与其能到达的点连边,流量为1,费用都是连的末端点的数值,以此建图,跑最大费用流。

大神的建图方式:
    1, 把每个点拆成两个点(左右点),起点末点的流量为2,其他为1,费用为其本身数值。
     2,能到达的点之间建边,右点连能到达的点的左点,流量为1,花费为0。
     3. 以起点的左点为源点,末点的右点为汇点跑最大费用流。

不得不说大神的建图方式简单明了,但个人觉得和我的建图没有本质区别,但是我的就是wa,改了建图方式就对了,说实话,并不明白,先留这个问题在这。


ac代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<vector>
#include<queue>
#define inf 0x7fffffff
using namespace std;
const int maxn=(100000+100)*46;
int n,m,from,to;
int num[1000][1000];
struct node
{
    int v,flow,cost;
    int next;
}edge[maxn];
int head[maxn],edgenum;
int dis[maxn],pre[maxn],pid[maxn],vis[maxn];

void add(int u,int v,int flow,int cost)
{
    edge[edgenum].v=v ;edge[edgenum].flow=flow ;
    edge[edgenum].cost=cost ;edge[edgenum].next=head[u];
    head[u]=edgenum++;

    edge[edgenum].v=u ;edge[edgenum].flow=0;
    edge[edgenum].cost=-cost ;edge[edgenum].next=head[v];
    head[v]=edgenum++;
}

int spfa()
{
    for (int i=0 ;i<=to ;i++)
    {
        dis[i]=0;  //转成求最大费用最大流
        vis[i]=0;
    }
    queue<int> Q;
    Q.push(from);
    dis[from]=0;
    vis[from]=1;
    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].v;
            if (edge[i].flow>0 && dis[v]<dis[u]+edge[i].cost)//求最大费用
            {
                dis[v]=dis[u]+edge[i].cost;
                pre[v]=u;
                pid[v]=i;
                if (!vis[v])
                {
                    vis[v]=1;
                    Q.push(v);
                }
            }
        }
    }
    return dis[to];
}

int mincost()
{
    int aug=0,maxflow=0;
    int ans=0,tmp=0;
    while (1)
    {
        tmp=spfa();
        if (tmp==0) break;
        aug=inf;
        for (int i=to ;i!=from ;i=pre[i])
        {
            if (edge[pid[i] ].flow<aug)
                aug=edge[pid[i] ].flow;
        }
        for (int i=to ;i!=from ;i=pre[i])
        {
            edge[pid[i] ].flow -= aug;
            edge[pid[i]^1 ].flow += aug;
        }
        maxflow += aug;
        ans += tmp;
    }
    return ans;
}

int main()
{
    while (scanf("%d",&n)!=EOF)
    {
        memset(head,-1,sizeof(head));
        edgenum=0;
        m=n;
        for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
           scanf("%d",&num[i][j]);
        from=1;
        to=n*m*2;
        int t=n*m;
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                if(i==0&&j==0||i==n-1&&j==n-1)
                    add(i*m+j+1,i*m+j+1+t,2,num[i][j]);
                else
                    add(i*m+j+1,i*m+j+1+t,1,num[i][j]);
                if(i<n-1)
                    add(i*m+j+1+t,(i+1)*m+j+1,1,0);
                if(j<m-1)
                    add(i*m+j+1+t,i*m+j+2,1,0);
            }
        }
        printf("%d\n",mincost()-num[0][0]-num[n-1][n-1]);
    }
    return 0;
}

wa代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<vector>
#include<queue>
#define inf 0x7fffffff
using namespace std;
const int maxn=(100000+100)*46;
int n,m,from,to;
int num[1000][1000];
struct node
{
    int v,flow,cost;
    int next;
}edge[maxn];
int head[maxn],edgenum;
int dis[maxn],pre[maxn],pid[maxn],vis[maxn];

void add(int u,int v,int flow,int cost)
{
    edge[edgenum].v=v ;edge[edgenum].flow=flow ;
    edge[edgenum].cost=cost ;edge[edgenum].next=head[u];
    head[u]=edgenum++;

    edge[edgenum].v=u ;edge[edgenum].flow=0;
    edge[edgenum].cost=-cost ;edge[edgenum].next=head[v];
    head[v]=edgenum++;
}

int spfa()
{
    for (int i=0 ;i<=to ;i++)
    {
        dis[i]=0;  //转成求最大费用最大流
        vis[i]=0;
    }
    queue<int> Q;
    Q.push(from);
    dis[from]=0;
    vis[from]=1;
    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].v;
            if (edge[i].flow>0 && dis[v]<dis[u]+edge[i].cost)//求最大费用
            {
                dis[v]=dis[u]+edge[i].cost;
                pre[v]=u;
                pid[v]=i;
                if (!vis[v])
                {
                    vis[v]=1;
                    Q.push(v);
                }
            }
        }
    }
    return dis[to];
}

int mincost()
{
    int aug=0,maxflow=0;
    int ans=0,tmp=0;
    while (1)
    {
        tmp=spfa();
        if (tmp==0) break;
        aug=inf;
        for (int i=to ;i!=from ;i=pre[i])
        {
            if (edge[pid[i] ].flow<aug)
                aug=edge[pid[i] ].flow;
        }
        for (int i=to ;i!=from ;i=pre[i])
        {
            edge[pid[i] ].flow -= aug;
            edge[pid[i]^1 ].flow += aug;
        }
        maxflow += aug;
        ans += tmp;
    }
    return ans;
}

int main()
{
    while (scanf("%d",&n)!=EOF)
    {
        memset(head,-1,sizeof(head));
        edgenum=0;
        m=n;
        for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
           scanf("%d",&num[i][j]);
        from=0;
        to=n*m;
        add(0,1,2,num[0][0]);
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                if(i<n-1)
                    add(i*m+j+1,(i+1)*m+j+1,1,num[i+1][j]);
                if(j<m-1)
                    add(i*m+j+1,i*m+j+2,1,num[i][j+1]);
            }
        }
        printf("%d\n",mincost()-num[0][0]-num[n-1][n-1]);
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值