POJ 3422 卡卡的矩阵之旅 最大费用流

Kaka's Matrix Travels
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 5806 Accepted: 2266

Description

On an N × N chessboard with a non-negative number in each grid, Kaka starts his matrix travels withSUM = 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 toSUM in each grid the rook visited, and replaces it with zero. It is not difficult to know the maximumSUM Kaka can obtain for his first travel. Now Kaka is wondering what is the maximumSUM he can obtain after his Kth travel. Note the SUM is accumulative during theK travels.

Input

The first line contains two integers N and K (1 ≤ N ≤ 50, 0 ≤K ≤ 10) described above. The followingN 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

Source

POJ Monthly--2007.10.06, Huang, Jinsong
 
 
 
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;

#define INF 0x3f3f3f3f
#define MIN(a,b) ((a)<(b)?(a):(b))

int mat[55][55];
int n,k;

int head[5005];
int adj[25000];
int next[25000];
int point[25000];
int cost[25000];
int cap[25000];

int pre[5005];
int dis[5005];
bool fl[5005];

int max_flow;
int min_cost;

int edge_cnt;

void add(int u,int v,int cst,int cp)
{
    next[edge_cnt]=head[u];
    head[u]=edge_cnt;
    point[edge_cnt]=v;
    adj[edge_cnt]=u;
    cost[edge_cnt]=cst;
    cap[edge_cnt]=cp;
}

void cost_flow(int src,int tar)
{
    while(1)
    {
        memset(pre,-1,sizeof(pre));
        memset(dis,0x3f,sizeof(dis));
        memset(fl,0,sizeof(fl));

        queue<int>q;
        q.push(src);
        dis[src]=0;

        while(!q.empty())
        {
            int u=q.front();
            q.pop();
            fl[u]=0;

            for(int e=head[u];e!=-1;e=next[e])
            {
                if(cap[e]>0&&dis[u]+cost[e]<dis[point[e]])
                {
                    dis[point[e]]=dis[u]+cost[e];
                    pre[point[e]]=e;
                    if(!fl[point[e]])
                    {
                        fl[point[e]]=1;
                        q.push(point[e]);
                    }
                }
            }
        }

        if(pre[tar]==-1)break;

        int min=INF;

        for(int i=tar;pre[i]!=-1;i=adj[pre[i]])
        {
            min=MIN(min,cap[pre[i]]);
        }
        for(int i=tar;pre[i]!=-1;i=adj[pre[i]])
        {
            cap[pre[i]]-=min;
            if(pre[i]&1)cap[pre[i]+1]+=min;
            else cap[pre[i]-1]+=min;
        }

        max_flow+=min;
        min_cost+=min*dis[tar];
    }
}

int main()
{
    while(~scanf("%d%d",&n,&k))
    {
        max_flow=0;
        min_cost=0;

        memset(head,-1,sizeof(head));

        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
            {
                scanf("%d",&mat[i][j]);
            }
        }

        edge_cnt=0;

        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
            {
                int p3=(i-1)*n+j;
                int p4=(i-1)*n+j+n*n;

                edge_cnt++;
                add(p3,p4,-mat[i][j],1);
                edge_cnt++;
                add(p4,p3,mat[i][j],1);

                edge_cnt++;
                add(p3,p4,0,k);
                edge_cnt++;
                add(p4,p3,0,0);

                if(i<n)
                {
                    int p1=(i-1)*n+j;
                    int p2=i*n+j;

                    //edge_cnt++;
                    //add(p1,p2,0,INF);

                    //edge_cnt++;
                    //add(p1,p2+n*n,0,INF);

                    edge_cnt++;
                    add(p1+n*n,p2,0,k);
                    edge_cnt++;
                    add(p2,p1+n*n,0,0);

                    //edge_cnt++;
                    //add(p1+n*n,p2+n*n,0,INF);
                }
                if(j<n)
                {
                    int p1=(i-1)*n+j;
                    int p2=(i-1)*n+j+1;

                    //edge_cnt++;
                    //add(p1,p2,0,INF);

                    //edge_cnt++;
                    //add(p1,p2+n*n,0,INF);

                    edge_cnt++;
                    add(p1+n*n,p2,0,k);
                    edge_cnt++;
                    add(p2,p1+n*n,0,0);

                    //edge_cnt++;
                    //add(p1+n*n,p2+n*n,0,INF);
                }
            }
        }

        edge_cnt++;
        add(0,1,0,k);
        edge_cnt++;
        add(1,0,0,0);

        edge_cnt++;
        add(n*n*2,n*n*2+1,0,k);
        edge_cnt++;
        add(n*n*2+1,n*n*2,0,0);

        cost_flow(0,2*n*n+1);

        printf("%d\n",-min_cost);
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值