首页 > 其他 > 详细

POJ 3422 Kaka's Matrix Travels

时间:2014-02-10 20:54:13      阅读:500      评论:0      收藏:0      [点我收藏+]
Kaka‘s Matrix Travels
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 7191   Accepted: 2874

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

Source

POJ Monthly--2007.10.06, Huang, Jinsong

   第一想法是dp+贪心,结果不对,错误举例如下:
4 2
0 0 0 0
0 0 0 0
0 2 0 1
1 0 2 0
如果用贪心,结果是5,显然是不对的,答案应该是6
然后想到了用最大流,结果不对,看了解题报告,原来是最大费用最大流啊
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <queue>
#define N 1000000
#define M 10000
using namespace std;
int n,k,sta,end;
struct num
{
    int sta,end,fan,next,cap,w;
}a[N];
int b[M],Top,pt[M],dis[M];
bool in[M];
int main()
{
    //freopen("data.in","r",stdin);
    void addeage(int x,int y,int cap,int w,int fan);
    int EK();
    while(scanf("%d %d",&n,&k)!=EOF)
    {
        sta = 0;
        end = n*n*2+1;
        Top = 0;
        memset(b,-1,sizeof(b));
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
            {
                int w;
                scanf("%d",&w);
                int x = (i-1)*n+j;
                int y = n*n+((i-1)*n+j);
                addeage(x,y,1,w,Top+1);
                addeage(y,x,0,-w,Top-1);
                addeage(x,y,k-1,0,Top+1);
                addeage(y,x,0,0,Top-1);
            }
        }
        for(int i=1;i<=n-1;i++)
        {
            for(int j=1;j<=n-1;j++)
            {
                int x = (i-1)*n+j+n*n;
                int y = (i-1)*n+j+1;
                addeage(x,y,k,0,Top+1);
                addeage(y,x,0,0,Top-1);
                y = i*n+j;
                addeage(x,y,k,0,Top+1);
                addeage(y,x,0,0,Top-1);
            }
        }
        for(int i=1;i<=n-1;i++)
        {
            int x = (i-1)*n+n+n*n;
            int y = i*n+n;
            addeage(x,y,k,0,Top+1);
            addeage(y,x,0,0,Top-1);
        }
        for(int j=1;j<=n-1;j++)
        {
            int x = (n-1)*n+j+n*n;
            int y = (n-1)*n+j+1;
            addeage(x,y,k,0,Top+1);
            addeage(y,x,0,0,Top-1);
        }
        addeage(0,1,k,0,Top+1);
        addeage(1,0,0,0,Top-1);
        addeage(2*n*n,end,k,0,Top+1);
        addeage(end,2*n*n,0,0,Top-1);
        int ans = EK();
        printf("%d\n",ans);
    }
    return 0;
}
void addeage(int x,int y,int cap,int w,int fan)
{
    a[Top].sta = x;
    a[Top].end = y;
    a[Top].cap = cap;
    a[Top].w = w;
    a[Top].fan = fan;
    a[Top].next = b[x];
    b[x] = Top++;
}
int spfa()
{
    pt[sta] = 0;
    queue<int>que;
    memset(dis,-1,sizeof(dis));
    memset(in,false,sizeof(in));
    que.push(sta);
    in[sta] = true;
    dis[sta] = 0;
    while(!que.empty())
    {
        int x= que.front();
        in[x] = false;
        que.pop();
        for(int i = b[x];i!=-1;i=a[i].next)
        {
            int y = a[i].end;
            int val = a[i].w;
            if(a[i].cap>0&&dis[y]<(dis[x]+val))
            {
                dis[y] = dis[x] + val;
                pt[y] = i;
                if(!in[y])
                {
                    que.push(y);
                    in[y] = true;
                }
            }
        }
    }
    if(dis[end]==-1)
    {
        return 0;
    }else
    {
        return dis[end];
    }
}
int EK()
{
    int res = 0;
    while(true)
    {
        int ans = spfa();
        if(ans==0)
        {
            break;
        }
        res +=ans;
        int x = end;
        while(x!=sta)
        {
            int pos = pt[x];
            a[pos].cap-=1;
            int fan = a[pos].fan;
            a[fan].cap+=1;
            x = a[pos].sta;
        }
    }
    return res;
}



POJ 3422 Kaka's Matrix Travels

原文:http://blog.csdn.net/yongxingao/article/details/19042635

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!