POJ 3422 Kaka's Matrix Travels

本文探讨了一个关于在带有非负数的N×N棋盘上进行旅行的问题,旨在通过多次旅行最大化累计SUM值。利用最小费用最大流算法解决该问题,并提供了具体的实现代码。
       Kaka's Matrix Travels
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 6448 Accepted: 2554

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 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

Source

POJ Monthly--2007.10.06, Huang, Jinsong
 
  神奇的建图,考察点: 最小费用最大流
#include <stdio.h>
#include <string.h>
#include <math.h>
struct num
{
    int sta,end,cap,cost,next,pre;
}a[1000000];
int b[50020],Top,sta,end;
int queue[1000000],dis[50020],status[50020];
int pre[50020],INF=0x7ffffff;
int main()
{
    void add(int s,int e,int cap,int cost);
    int EK();
    int i,j,n,m,s,t,w;
    int x;
    while(scanf("%d %d",&n,&m)!=EOF)
    {
        t=n*n;
        Top=0;
        memset(b,-1,sizeof(b));
        for(i=1;i<=n;i++)
        {
            for(j=1;j<=n;j++)
            {
                scanf("%d",&w);
                x=(i-1)*n+j;
                add(x,t+x,1,w);
                add(x,t+x,m,0);
                if(j<=n-1)
                {
                    add(t+x,x+1,m,0);
                }
                if(i<=n-1)
                {
                    add(t+x,x+n,m,0);
                }
            }
        }
        add(0,1,m,0);
        add(2*t,2*t+1,m,0);
        sta=0; end=2*t+1;
        s=EK();
        printf("%d\n",s);
    }
    return 0;
}
 void add(int s,int e,int cap,int cost)
 {
     a[Top].sta=s;
     a[Top].end=e;
     a[Top].cap=cap;
     a[Top].cost=cost;
     a[Top].pre=Top+1;
     a[Top].next=b[s];
     b[s]=Top;
     Top++;

     a[Top].sta=e;
     a[Top].end=s;
     a[Top].cap=0;
     a[Top].cost=-cost;
     a[Top].next=b[e];
     a[Top].pre=Top-1;
     b[e]=Top;
     Top++;
 }
 int spfa()
 {
     int i,j,base,top,x,xend;
     base=top=0;
     memset(status,0,sizeof(status));
     for(i=0;i<=end;i++)
     {
         dis[i]=-INF;
     }
     queue[top++]=0;
     status[0]=1;
     dis[0]=0;
     while(base<top)
     {
         x=queue[base++];
         status[x]=0;
         for(i=b[x]; i!=-1; i=a[i].next)
         {
             if(a[i].cap)
             {
                 xend=a[i].end;
                 if(dis[xend]<(dis[x]+a[i].cost))
                 {
                     dis[xend]=dis[x]+a[i].cost;
                     pre[xend]=i;
                     if(!status[xend])
                     {
                         queue[top++]=xend;
                         status[xend]=1;
                     }
                 }
             }
         }
     }
     if(dis[end]<=0)
     {
         return -1;
     }else
     {
         return 1;
     }
 }
 int EK()
 {
     int i,j,k,t,s=0;
     while(1)
     {
         k=spfa();
         if(k==-1)
         {
             break;
         }
         for(i=end;i!=0;i=a[j].sta)
         {
             j=pre[i];
            a[j].cap-=1;
            t=a[j].pre;
            a[t].cap+=1;
            s+=a[j].cost;
         }
     }
     return s;
 }

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值