POJ 3422 Kaka's Matrix Travels(最大费用最大流 + 拆点)

来源:互联网 发布:ubuntu mysql自启动 编辑:程序博客网 时间:2024/04/30 03:42

题目链接:http://poj.org/problem?id=3422


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 21 2 30 2 11 4 2

Sample Output

15

Source

POJ Monthly--2007.10.06, Huang, Jinsong

题意:
在一个矩阵中,每个格子里都有一个非负数,把象棋里的车从左上角走到右下角,走 k 次,每次只能走一步,每次只能向右或向下,经过的数字即为价值(费用),并且走过某个点后,再次走这个点,那么它的费用为零,求最大和 !

PS:
一道最大费用最大流问题,
1、拆点建边:把每个点拆为两个点再在这两个点之间分别建立两条边:
第一条:他们之间的费用即为所拆点上的那个正数的相反数(这样就可以用最小费用最大流求解了,最终的答案再去一个相反数即为最大费用了),流量为 1 ;
第二条:费用为 0 ,流量为 k-1;
2、再分别向右,向下建一条费用为 0 ,流量为 k 的边!

//最大费用最小流只要在添加边的时候换一下位置就好了
//求最大费用最大流只需要把费用换成相反数,用最小费用最大流求解即可


代码如下:

#include <cstdio>#include <cstring>#include <algorithm>#include <queue>#include <cmath>using namespace std;const int MAXN = 10000;const int MAXM = 100000;const int INF = 0x3f3f3f3f;struct Edge{    int to, next, cap, flow, cost;    int x, y;} edge[MAXM];int head[MAXN],tol;int pre[MAXN],dis[MAXN];bool vis[MAXN];int N, M;int map[MAXN][MAXN];void init(){    N = MAXN;    tol = 0;    memset(head, -1, sizeof(head));}void addedge(int u, int v, int cap, int cost)//左端点,右端点,容量,花费{    edge[tol]. to = v;    edge[tol]. cap = cap;    edge[tol]. cost = cost;    edge[tol]. flow = 0;    edge[tol]. next = head[u];    head[u] = tol++;    edge[tol]. to = u;    edge[tol]. cap = 0;    edge[tol]. cost = -cost;    edge[tol]. flow = 0;    edge[tol]. next = head[v];    head[v] = tol++;}bool spfa(int s, int t){    queue<int>q;    for(int i = 0; i < N; i++)    {        dis[i] = INF;        vis[i] = false;        pre[i] = -1;    }    dis[s] = 0;    vis[s] = true;    q.push(s);    while(!q.empty())    {        int u = q.front();        q.pop();        vis[u] = false;        for(int i = head[u]; i != -1; i = edge[i]. next)        {            int v = edge[i]. to;            if(edge[i]. cap > edge[i]. flow &&                    dis[v] > dis[u] + edge[i]. cost )            {                dis[v] = dis[u] + edge[i]. cost;                pre[v] = i;                if(!vis[v])                {                    vis[v] = true;                    q.push(v);                }            }        }    }    if(pre[t] == -1) return false;    else return true;}//返回的是最大流, cost存的是最小费用int minCostMaxflow(int s, int t, int &cost){    int flow = 0;    cost = 0;    while(spfa(s,t))    {        int Min = INF;        for(int i = pre[t]; i != -1; i = pre[edge[i^1]. to])        {            if(Min > edge[i]. cap - edge[i]. flow)                Min = edge[i]. cap - edge[i]. flow;        }        for(int i = pre[t]; i != -1; i = pre[edge[i^1]. to])        {            edge[i]. flow += Min;            edge[i^1]. flow -= Min;            cost += edge[i]. cost * Min;        }        flow += Min;    }    return flow;}int main(){    int n, k;    while(~scanf("%d%d",&n,&k))    {        init();//注意        for(int i = 1; i <= n; i++)        {            for(int j = 1; j <= n; j++)            {                scanf("%d",&map[i][j]);            }        }        for(int i = 1; i <= n; i++)        {            for(int j = 1; j <= n; j++)            {                int tt = (i-1)*n+j;                addedge(tt*2,tt*2+1,1,-map[i][j]);//拆点,之间建一条负的价值的边,流量为1                addedge(tt*2,tt*2+1,k-1,0);//再建一条价值为0,流量为k-1的边                //printf("tt:%d tt*2:%d tt*2+1:%d ",tt,tt*2,tt*2+1);            }            //printf("\n");        }        for(int i = 1; i <= n; i++)//向右        {            for(int j = 1; j < n; j++)            {                int tt = (i-1)*n+j;//编号从2开始                addedge(tt*2+1,(tt+1)*2,k,0);                //printf("tt:%d tt*2:%d (tt+1)*2:%d\n",tt,tt*2,(tt+1)*2);            }        }        for(int i = 1; i < n; i++)//向下        {            for(int j = 1; j <= n; j++)            {                int tt = (i-1)*n+j;                addedge(tt*2+1,(tt+n)*2,k,0);                //printf("tt:%d tt*2:%d (tt+n)*2:%d\n",tt,tt*2,(tt+n)*2);            }        }        int beg = 1;//超级起点        int end = 2*n*n+2;//超级汇点        addedge(beg,2,k,0);//超级起点,容量为1,花费为0        addedge(2*n*n+1,end,k,0);//超级汇点容量为1,花费为0        int ans = 0;        minCostMaxflow(beg,end,ans);        //minCostMaxflow(2,2*n*n+1,ans);        printf("%d\n",-ans);//相反数即为最大的价值    }    return 0;}


1 0
原创粉丝点击