Lightoj1071:Baker Vai(最小费用最大流)

1071 - Baker Vai
Time Limit: 2 second(s)Memory Limit: 32 MB

All of you must have heard the name of Baker Vai. Yes, he rides a bike and likes to help people. That's why he is popular amongst general people.

Baker Vai lives in a city which can be modeled as a 2D m x n matrix. Where the north-west corner is cell 1, 1 and the south-east corner is cell m, n. In each cell there are certain amount of people who needs help which is already known to Baker Vai.

Each day Baker Vai starts his journey from the north-west corner and he can only go to east or south. This way he reaches the south-east corner of the city. After that he returns back to the north-west, but this time he can only move to west or north. He doesn't want a cell to be visited twice other than the two corners. And if he visits a cell, he helps all the people in the cell.

Now you are given the map of the city and the number of people who need help in all cells for a particular day. You have to help Baker Vai finding the maximum number of people he can help in that day.

Input

Input starts with an integer T (≤ 25), denoting the number of test cases.

Each case contains a blank line and two integers, m, n (2 ≤ m, n ≤ 100). Each of the next m lines will contain n integers, denoting the number of people who are in need. In a cell there will be no more than 20 people and a cell can be empty, too.

Output

For each test case, print the case number and the maximum number of people Baker Vai can help considering the above conditions.

Sample Input

Output for Sample Input

2

 

3 3

1 1 1

1 0 1

1 1 1

 

3 4

1 1 0 1

1 1 1 1

0 1 10 1

Case 1: 8

Case 2: 18


题意:有个n*m的矩阵,有个人从左上角走到右下角,然后再回到左上角,前者只能向右向下走,后者只能向左向上走,要求除左上右下两个格子其余的格子只能经过一次,求覆盖路径的最大权值和。

思路:相当于起点放两个人同时走,一种做法是dp+记忆化搜索(最优解两个人路径绝对没有重合),这里用费用流,先拆点,费用为权值的负数,容量为1,相邻点建边权值为0,容量为2。左上和右下点各多建一条边模拟两个人同时出发,最后减回左上和右下多算的权值即可。

# include <iostream>
# include <cstdio>
# include <queue>
# include <cstring>
using namespace std;
int a[103][103],cnt, Next[20008], vis[20008], dis[20008],  pre[20008];
int source, sink, n, m;
struct node
{
    int u, v, w, c, next;
}edge[100000];
void add(int u, int v, int w, int c)
{
    edge[cnt] = node{u,v,w,c,Next[u]};
    Next[u] = cnt++;
    edge[cnt] = node{v,u,0,-c,Next[v]};
    Next[v] = cnt++;
}
bool spfa()
{
    memset(pre, 0, sizeof(pre));
    memset(vis, 0, sizeof(vis));
    memset(dis, 0x3f, sizeof(dis));
    int l=0, r=0;
    dis[source] = 0;
    vis[source] = 1;
    queue<int>q;
    q.push(source);
    while(!q.empty())
    {
        int u = q.front();
        q.pop();
        vis[u] = 0;
        for(int i=Next[u]; i!=-1; i=edge[i].next)
        {
            int v = edge[i].v, w=edge[i].w, c=edge[i].c;
            if(w>0&&dis[v]>dis[u]+c)
            {
                dis[v] = dis[u]+c;
                pre[v] = i;
                if(!vis[v])
                {
                    vis[v] = 1;
                    q.push(v);
                }
            }
        }
    }
    return dis[sink] != 0x3f3f3f3f;
}

int mcmf()
{
    int full=0, cost = 0;
    while(spfa())
    {
        int imin = 0x3f3f3f3f;
        for(int i=sink; i!=source; i=edge[pre[i]].u)
            imin = min(imin, edge[pre[i]].w);
        for(int i=sink; i!=source; i=edge[pre[i]].u)
        {
            edge[pre[i]].w -= imin;
            edge[pre[i]^1].w += imin;
        }
        cost += imin*dis[sink];
        full += imin;
    }
    return cost;
}
int main()
{
    int t,cas=1;
    scanf("%d",&t);
    while(t--)
    {
        cnt = 0;
        memset(Next, -1, sizeof(Next));
        scanf("%d%d",&n,&m);
        source = 0;
        sink = n*m*2-1;
        for(int i=0; i<n; ++i)
            for(int j=0; j<m; ++j)
            scanf("%d",&a[i][j]);
        for(int i=0; i<n; ++i)
            for(int j=0; j<m; ++j)
                add(i*m+j, i*m+j+n*m, 1, -a[i][j]);
        add(0, n*m, 1, -a[0][0]);
        add(n*m-1, n*m*2-1, 1, -a[n-1][m-1]);
        for(int i=0; i<n; ++i)
        {
            for(int j=0; j<m; ++j)
            {
                if(i<n-1)
                    add(i*m+j+n*m, (i+1)*m+j, 2, 0);
                if(j<m-1)
                    add(i*m+j+n*m, i*m+j+1, 2, 0);
            }
        }
        int ans = -mcmf();
        ans = ans - a[0][0] - a[n-1][m-1];
        printf("Case %d: %d\n",cas++, ans);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值