poj3422 Kaka's Matrix Travels [最小费用最大流]

in 题目 with 0 comment

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 \le N \le 50, 0 \le K \le 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


Solution

本题可以转化为最小费用最大流求解,建图方法:

  1. 指定超级源s=0,超级汇t=n^2+1,连接$s\rightarrow 1$ , $n^2\rightarrow t$ 容量为k,费用为0,表示一共要走k次
  2. 拆点,把每个点i分成 $i$ 与 $i+n^2$ ,连接$i\rightarrow i+n^2$,容量为1,费用为该点在矩阵上的数值的相反数(为了使求出的费用最小)
  3. 对于可连通的点$i,j$,为了确保其在经过一次增广后依然连通,我们还要分别连接$i\rightarrow j$ , $i\rightarrow j+n^2$ , $i+n^2 \rightarrow j$ , $i+n^2 \rightarrow j+n^2$ , 容量为$\infty$,费用为0

Code

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#define maxn 10100
#define maxe 100000
#define INF 0x3f3f3f3f
//#define DEBUG
using namespace std;
int n,k,s,t,cnt=1;
int head[maxn],dis[maxn];
int prev[maxn],path[maxn];

struct edge
{
    int to,next,cap,val;
}e[maxe];

inline int read()
{
    char ch;
    int read=0,sign=1;
    do
        ch=getchar();
    while((ch<'0'||ch>'9')&&ch!='-');
    if(ch=='-') sign=-1,ch=getchar();
    while(ch>='0'&&ch<='9')
    {
        read=read*10+ch-'0';
        ch=getchar();
    }
    return sign*read;
}

inline void ins(int u,int v,int w,int val)
{
    cnt++;
    e[cnt].to=v;
    e[cnt].cap=w;
    e[cnt].val=val;
    e[cnt].next=head[u];
    head[u]=cnt;
}

inline void insert(int u,int v,int w=INF,int val=0)
{ ins(u,v,w,val);ins(v,u,0,-val); } 

void init()
{
    n=read(),k=read(),s=0,t=n*n*2+1;
    int num=0,val;
    for(int i=1;i<=n;++i)
        for(int j=1;j<=n;++j)
        {
            num++;val=read();
            insert(num,num+n*n,1,-val);
            if(i!=n)
            {
                if(j!=n) 
                {
                    insert(num,num+1);insert(num,num+1+n*n);insert(num+n*n,num+1);insert(num+n*n,num+1+n*n);
                    insert(num,num+n);insert(num,num+n+n*n);insert(num+n*n,num+n);insert(num+n*n,num+n+n*n);
                }
                else 
                {
                    insert(num,num+n);insert(num,num+n+n*n);insert(num+n*n,num+n);insert(num+n*n,num+n+n*n);
                }
                
            }
            else
                if(j!=n) 
                {
                    insert(num,num+1);insert(num,num+1+n*n);insert(num+n*n,num+1);insert(num+n*n,num+1+n*n);
                }
        }
    insert(s,1,k,0);
    insert(n*n*2,t,k,0);
}

bool SPFA()
{
    memset(dis,0x3f,sizeof(dis));
    queue<int> q;
    q.push(s);
    dis[s]=0;
    prev[s]=-1;
    while(!q.empty())
    {
        int u=q.front();q.pop();
        int i=head[u];
        while(i)
        {
            if(e[i].cap && dis[u]+e[i].val<dis[e[i].to])
            {
                dis[e[i].to]=dis[u]+e[i].val;
                prev[e[i].to]=u;
                path[e[i].to]=i;
                q.push(e[i].to);
            }
            i=e[i].next;
        }
    }
    if(dis[t]==INF) return 0;
    return 1;
}

int argument()
{
    int w=INF,cost=0;
    for(int i=t;prev[i]!=-1;i=prev[i])
        w=min(w,e[path[i]].cap);
    for(int i=t;prev[i]!=-1;i=prev[i])
    {
        e[path[i]].cap-=w;
        e[path[i]^1].cap+=w;
        cost+=e[path[i]].val*w;
    #ifdef DEBUG
        cout<<"i="<<i<<" val="<<e[path[i]].val<<" w="<<w<<endl;
    #endif
    }
    return cost;
} 

void MCMF()
{
    int ans=0;
    while(SPFA()) ans+=argument();
    printf("%d\n",-ans); 
}

int main()
{
//  freopen("out.txt","w",stdout);
    init();
#ifdef DEBUG 
    for(int i=1;i<=cnt;++i)
        printf("%d-->to=%d,cap=%d,val=%d\n",i,e[i].to,e[i].cap,e[i].val);
#endif
    MCMF();
    return 0;
}

扫描二维码,在手机上阅读!
Responses