POJ 3422 Kaka's Matrix Travels

来源:互联网 发布:网络劫持广告怎么解决 编辑:程序博客网 时间:2024/05/05 11:29
       Kaka's Matrix Travels
Time Limit: 1000MS Memory Limit: 65536KTotal 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 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 21 2 30 2 11 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; }

 
原创粉丝点击