首页 > 代码库 > poj 3422 Kaka's Matrix Travels (费用流)

poj 3422 Kaka's Matrix Travels (费用流)

Kaka‘s Matrix Travels
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 7743 Accepted: 3111

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

无力吐槽了,数组开大点吧!!

这个题建图的时候需要拆点。每个格子拆成两个点,一个记为出点u,一个记为入点v,u->v 两条路。

一个容量为1,权值为那个格子的钱数;另一条路容量为k-1,权值为0。因为走了一次钱捡起来之后就没钱了。

然后由V指向下一个格子(右、下)有一条路,容量为K,费用为零。

超级源点是0, 超级汇点是n*n*2+1.

#include"stdio.h"
#include"string.h"
#include"queue"
using namespace std;
#define N 50000
const int inf=1<<20;
struct node
{
    int u,v,c,f,next;
}e[N*2];
int pre[N],dis[N],vis[N],head[N],t;
void add1(int u,int v,int c,int f)
{
    e[t].u=u;
    e[t].v=v;
    e[t].c=c;
    e[t].f=f;
    e[t].next=head[u];
    head[u]=t++;
}
void add(int u,int v,int c,int f)
{
    add1(u,v,c,f);
    add1(v,u,-c,0);
}
int spfa(int s,int n)
{
    int i,u,v;
    for(i=s;i<=n;i++)
        dis[i]=inf;
    dis[s]=0;
    memset(pre,-1,sizeof(pre));
    memset(vis,0,sizeof(vis));
    queue<int>q;
    q.push(s);
    while(!q.empty())
    {
        u=q.front();
        q.pop();
        vis[u]=0;
        for(i=head[u];i!=-1;i=e[i].next)
        {
            v=e[i].v;
            if(e[i].f&&dis[v]>dis[u]+e[i].c)
            {
                dis[v]=dis[u]+e[i].c;
                pre[v]=i;
                if(!vis[v])
                {
                    vis[v]=1;
                    q.push(v);
                }
            }
        }
    }
    if(pre[n]!=-1)
        return 1;
    return 0;
}
void solve(int s,int n)
{
    int i,j,cost,minf;
    cost=0;
    while(spfa(s,n))
    {
        minf=inf;
        for(i=pre[n];i!=-1;i=pre[e[i].u])
        {
            if(minf>e[i].f)
                minf=e[i].f;
        }
        for(i=pre[n];i!=-1;i=pre[e[i].u])
        {
            j=i^1;
            e[i].f-=minf;
            e[j].f+=minf;
        }
        cost+=minf*dis[n];
    }
    printf("%d\n",-cost);
}
int main()
{
    int i,j,n,k,u,c;
    while(scanf("%d%d",&n,&k)!=-1)
    {
        t=0;
        memset(head,-1,sizeof(head));
        for(i=1;i<=n;i++)
        {
            for(j=1;j<=n;j++)
            {
                scanf("%d",&c);
                u=(i-1)*n+j;
                add(u,u+n*n,-c,1);
                add(u,u+n*n,0,k-1);
                if(i+1<=n)  
                    add(u+n*n,u+n,0,k);
                if(j+1<=n)
                    add(u+n*n,u+1,0,k);
            }
        }
        add(0,1,0,k);     
        add(2*n*n,2*n*n+1,0,k);
        solve(0,2*n*n+1);
    }
    return 0;
}