poj3422 Kaka's Matrix Travels 最大费用最大流

题意:

有个方阵,每个格子里都有一个非负数,从左上角走到右下角,每次走一步,只能往右或往下走,经过的数字拿走
每次都找可以拿到数字和最大的路径走,走k次,求最大和  这是 最大费用最大流 问题 每次spfa都找的是一条和最大的路径 s--到左上角的边流量是K限制增广次数
求最大费用最大流只需要把费用换成相反数,用最小费用最大流求解即可


构图过程:如下图
每个点拆分成两个 a a' 之间建两条边(当然还要建退边),分别是 (费用为该点相反数,流量为1) (费用为0,流量为k-1)
路过这点时,可以通过前边那条边拿到数字,
以后再从这儿过,就没有数字可拿了,走的就是第二条边

然后是 没点向 右和下 建一条边 费用0,流量k

然后s、t连接左上角和右下角即可

求最小费用最大流的方法和EK求最大流的方法类似,
都是找一条增广路径,然后把流加进去
只不过EK找的是路径最短的增广路(广搜即可得)
而最小费用最大流 找的是费用最小的路径(对费用求最短路)

Description

On an N × N chessboard with a non-negative number in each grid, Kaka starts his matrix travels with SUM = 0. For each travel, Kaka moves one rook from the left-upper grid to the right-bottom one, taking care that the rook moves only to the right or down. Kaka adds the number to SUM in each grid the rook visited, and replaces it with zero. It is not difficult to know the maximum SUM Kaka can obtain for his first travel. Now Kaka is wondering what is the maximum SUM he can obtain after his Kth travel. Note the SUM is accumulative during the K travels.

Input

The first line contains two integers N and K (1 ≤ N ≤ 50, 0 ≤ K ≤ 10) described above. The following N lines represents the matrix. You can assume the numbers in the matrix are no more than 1000.

Output

The maximum SUM Kaka can obtain after his Kth travel.

Sample Input

3 2
1 2 3
0 2 1
1 4 2

Sample Output

15

这道题目有两种解法  第一种是变换一下模版让他直接求最大值     第二种是把权值全部变为负值直接求最小值,输出的时候乘以-1就行

我用了第一种

#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];
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;                  //求最小费用是要全部初始化为INF
    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];
        }
        //printf("%d\n",maxl);
        p=pre[T];
        while(p!=-1)
        {
            edge[p].w-=maxl;
            edge[p^1].w+=maxl;
            //printf("%d\n",edge[p].f);
            ans+=maxl*edge[p].f;
            p=pre[edge[p].u];
        }
    }
    return ans;
}
int main()
{
    int n,k;
    int map[55][55];
    int a,b;
    while(~scanf("%d %d",&n,&k))
    {
        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++)
                printf("%d ",map[i][j]);
            printf("\n");
        }*/
        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,k,0);
                if(j<n)
                    add(b,a+1,k,0);
            }
           /* for(int i=0;i<cnt;i++)
                printf("%d ",edge[i].f);
            printf("\n");*/
        s=0;
        T=2*n*n+1;
        add(s,1,k,0);
        add(2*n*n,T,k,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、付费专栏及课程。

余额充值