POJ 3422 Kaka's Matrix Travels 最小费用最大流

46 篇文章 0 订阅
19 篇文章 0 订阅

这道题目,题目意思虽然很好理解,但是建图不是很好建。

题意:有个方阵,每个格子里都有一个非负数,从左上角走到右下角,每次走一步,只能往右或往下走,经过的数字拿走 每次都找可以拿到数字和最大的路径走,走k次,求最大和。

可能是自己做的题目太少的原因,这道题目的建图,感觉不太好建。这道题要拆点,把每个点拆成两个点:

一个v1->v1',在这里网络流的流量是1,费用就是这个点所有代表的数字,另一个v1->v1'这里的网络流的流量是k-1,费用是0.因为第一次走就把数字带走了,所以不会再有费用了啊。然后在v1和v2之间建立一条流量为k,费用为0的路,然后加一个超级源点把它与第一个点联系在一起,代表开始。同理再来一个超级汇点。这样一个网络流就建立起来了,然后从源点,到汇点找一条最长路就是要输出的结果。

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

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
#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <iomanip>
#include <stdio.h>
#include <string>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
#include <set>
#define eps 1e-7
//#define M 1000100
//#define LL __int64
#define LL long long
#define INF 0x3f3f3f3f
#define PI 3.1415926535898

const int maxn = 101000;
using namespace std;

int map1[101][101];
int head[maxn], dis[maxn], vis[maxn];
int cnt;
int pre[maxn];
struct node
{
    int u, v, w;
    int next;
    int f;
} edge[maxn];
int S, T;
int n, k;
int sum;

void add(int u, int v, int w, int f)
{
    edge[cnt].u = u;
    edge[cnt].v = v;
    edge[cnt].w = w;
    edge[cnt].f = f;//正向边流量;
    edge[cnt].next = head[u];
    head[u] = cnt;
    cnt++;

    edge[cnt].u = v;
    edge[cnt].v = u;
    edge[cnt].w = -w;
    edge[cnt].f = 0;
    edge[cnt].next = head[v];
    head[v] = cnt;
    cnt++;
}

int spfa()
{
    for(int i = 0; i <= T; i++)
    {
        pre[i] = -1;
        dis[i] = -INF;
        vis[i] = 0;
    }
    queue<int>q;
    q.push(S);
    dis[S] = 0;
    vis[S] = 1;
    while(!q.empty())
    {
        int u = q.front();
        q.pop();
        vis[u] = 0;
        for(int i = head[u]; i != -1; i = edge[i].next)
        {
            if(edge[i].f > 0 && dis[edge[i].v] < dis[u]+edge[i].w)
            {
                dis[edge[i].v] = dis[u]+edge[i].w;
                pre[edge[i].v] = i;
                if(!vis[edge[i].v])
                {
                    q.push(edge[i].v);
                    vis[edge[i].v] = 1;
                }
            }
        }
    }
    if(dis[T] == -INF)
        return 0;
    return 1;
}

void find_max_road()
{
    while(spfa())
    {
        int _max = INF;
        int p = pre[T];
        while(p != -1)
        {
            _max = min(_max, edge[p].f);
            p = pre[edge[p].u];
        }
        p = pre[T];
        while(p != -1)
        {
            edge[p].f -= _max;
            edge[p^1].f += _max;
            sum += _max*edge[p].w;
            p = pre[edge[p].u];
        }
    }
}

int main()
{
    while(cin >>n>>k)
    {
        if(!n && !k)
            break;
        cnt = 0;
        sum = 0;
        memset(head , -1 , sizeof(head));
        for(int i = 1; i <= n; i++)
            for(int j = 1; j <= n; j++)
                cin >>map1[i][j];
        for(int i = 1; i <= n; i++)
        {
            for(int j = 1; j <= n; j++)
            {
                int u = (i-1)*n+j-1;
                add(2*u, 2*u+1, map1[i][j], 1);//拆点;
                add(2*u, 2*u+1, 0 , k-1);
            }
        }
        for(int i = 1; i <= n; i++)
        {
            for(int j = 1; j < n; j++)
            {
                int u = (i-1)*n+j-1;
                add(u*2+1, (u+1)*2, 0 , k);
            }
        }
        for(int i = 1; i < n; i++)
        {
            for(int j = 1; j <= n; j++)
            {
                int u = (i-1)*n+j-1;
                add(u*2+1, (u+n)*2, 0 , k);
            }
        }
        S = 2*n*n;
        T = 2*n*n+1;
        add(S, 0 , 0 , k);// 建立源点到起点
        add(n*n*2-1, T , 0 , k);//从终点到汇点;
        find_max_road();
        cout<<sum<<endl;
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值