poj 3422 Kaka's Matrix Travels(最大费用流,巧妙构图,拆点)

Kaka's Matrix Travels
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 9283 Accepted: 3758

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
题意:有一个n*n的矩阵,现在要从左上角走到右下角走k次,每一个格子走过一次权值就变成0,现在求最大的权值

思路:把一个点拆成两个点,这两个点之间连两条边,一条容量为1,费用为点权,一条容量为k,费用为0(连边首尾相接即可)

这样就能保证只有第一次会走费用大的那条边,感觉好巧妙啊

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
using namespace std;
#define N 5050
#define INF 999999999
struct Edge
{
    int u,v,next,cap,cost;
} edge[N*N];
int cnt,head[N];
int ma[55][55];
int dir[2][2]= {1,0,0,1};
int vis[N],pp[N],d[N],sumflow;
void init()
{
    cnt=0;
    memset(head,-1,sizeof(head));
}
void addedge(int u,int v,int cap,int cost)
{
    edge[cnt].u=u;
    edge[cnt].v=v;
    edge[cnt].cap=cap;
    edge[cnt].cost=cost;
    edge[cnt].next=head[u];
    head[u]=cnt++;

    edge[cnt].u=v;
    edge[cnt].v=u;
    edge[cnt].cap=0;
    edge[cnt].cost=-cost;
    edge[cnt].next=head[v];
    head[v]=cnt++;
}
int spfa(int s,int t,int n)
{
    queue<int>q;
    memset(vis,0,sizeof(vis));
    memset(pp,-1,sizeof(pp));///pp[i]表示最短路径上以i为终点的边的编号
    for(int i=0; i<=n; i++)
        d[i]=-INF;
    d[s]=0;
    vis[s]=1;
    q.push(s);
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        vis[u]=0;
        for(int i=head[u]; i!=-1; i=edge[i].next)
        {
            int v=edge[i].v;
            if(edge[i].cap>0&&d[v]<d[u]+edge[i].cost)
            {
                d[v]=d[u]+edge[i].cost;
                pp[v]=i;
                if(!vis[v])
                {
                    vis[v]=1;
                    q.push(v);
                }
            }
        }
    }
    if(d[t]==-INF) return 0;///找不到一条到终点的路
    return 1;
}
int MCMF(int s,int t,int n)
{
    int mincost=0,minflow,flow=0;///最大费用,路径中最小流量,总流量
    while(spfa(s,t,n))///找当前的最长路
    {
        minflow=INF+1;
        for(int i=pp[t]; i!=-1; i=pp[edge[i].u])
            minflow=min(minflow,edge[i].cap);///从路径中找最小的流量
        flow+=minflow;///总流量加上最小流量
        for(int i=pp[t]; i!=-1; i=pp[edge[i].u])
        {
            edge[i].cap-=minflow;///当前边减去最小流量
            edge[i^1].cap+=minflow;///反向边加上最小流量
        }
        mincost+=d[t]*minflow;///最小费用等于路径和*每条路径的流量(经过多少次)
    }
    sumflow=flow;
    return mincost;
}

int main()
{
    int n,m;
    while(~scanf("%d %d",&n,&m))
    {
        init();
        for(int i=1; i<=n; i++)
            for(int j=1; j<=n; j++)
                scanf("%d",&ma[i][j]);
        int s=0,t=n*n*2+1;
        addedge(s,1,m,0);
        for(int i=1; i<=n; i++)
        {
            for(int j=1; j<=n; j++)
            {
                addedge((i-1)*n+j,(i-1)*n+j+n*n,1,ma[i][j]);
                addedge((i-1)*n+j,(i-1)*n+j+n*n,m,0);
                for(int k=0; k<2; k++)
                {
                    int x=i+dir[k][0],y=j+dir[k][1];
                    if(x<1||x>n||y<1||y>n)  continue;
                    addedge((i-1)*n+j+n*n,(x-1)*n+y,m,0);
                }
            }
        }
        addedge(n*n*2,t,m,0);
        printf("%d\n",MCMF(s,t,t));
    }
    return 0;
}








评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值