POJ - 2195 Going Home

传送门: 点击打开链接

最小费用网络流模板题 主要是来看自己的模板对不对...

Description

On a grid map there are n little men and n houses. In each unit time, every little man can move one unit step, either horizontally, or vertically, to an adjacent point. For each little man, you need to pay a $1 travel fee for every step he moves, until he enters a house. The task is complicated with the restriction that each house can accommodate only one little man. 

Your task is to compute the minimum amount of money you need to pay in order to send these n little men into those n different houses. The input is a map of the scenario, a '.' means an empty space, an 'H' represents a house on that point, and am 'm' indicates there is a little man on that point. 

You can think of each point on the grid map as a quite large square, so it can hold n little men at the same time; also, it is okay if a little man steps on a grid with a house without entering that house.

Input

There are one or more test cases in the input. Each case starts with a line giving two integers N and M, where N is the number of rows of the map, and M is the number of columns. The rest of the input will be N lines describing the map. You may assume both N and M are between 2 and 100, inclusive. There will be the same number of 'H's and 'm's on the map; and there will be at most 100 houses. Input will terminate with 0 0 for N and M.

Output

For each test case, output one line with the single integer, which is the minimum amount, in dollars, you need to pay.

Sample Input

2 2
.m
H.
5 5
HH..m
.....
.....
.....
mm..H
7 8
...H....
...H....
...H....
mmmHmmmm
...H....
...H....
...H....
0 0

Sample Output

2
10
28

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<vector>
#include<cstdlib>
#include<string>
#include<map>
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int maxv = 10000;
const int maxe = 300*300 + 300 + 10;
struct node
{
    int to, cap, cost, id;
    node(){}
    node(int to, int cap, int cost, int id) // id记录的是以to为边的起点指向from的边是以to为起始点的第几条边
    :to(to), cap(cap), cost(cost), id(id){}
};
vector<node>edge[maxe];
int pre[maxv], src, des, dis[maxv], g[maxv];
bool used[maxv];
void init()
{
    for(int i = 0; i < maxv; i++)
        edge[i].clear();
    memset(g, 0, sizeof(g));
}
void addedge(int from, int to, int cap, int cost)
{
    edge[from].push_back(node(to, cap, cost, g[to]));
    edge[to].push_back(node(from, 0, -cost, g[from]));
    g[to]++; g[from]++;
}
bool spfa(int src, int des)
{
    queue<int>que;
    memset(used, false, sizeof(used));
    memset(dis, inf, sizeof(dis));
    memset(pre, -1, sizeof(pre));
    dis[src] = 0;
    que.push(src);
    used[src] = true;
    while(!que.empty())
    {
        int u = que.front();
        used[u] = false;
        que.pop();
        for(int i = 0; i < edge[u].size(); i++)
        {
            int v = edge[u][i].to;
            if(edge[u][i].cap > 0 && dis[v] > dis[u] + edge[u][i].cost)
            {
                dis[v] = dis[u] + edge[u][i].cost;
                pre[v] = edge[u][i].id;
                if(!used[v])
                {
                    used[v] = true;
                    que.push(v);
                }
            }
        }
    }
    return dis[des] < inf;
}
int MCMF(int src, int des)
{
    int sum = 0;
    while(spfa(src, des))
    {
        int minflow = inf, i;
        for(i = des; i != src; i = edge[i][pre[i]].to)
        {
            minflow = min(minflow, edge[edge[i][pre[i]].to][edge[i][pre[i]].id].cap);
        }
        for(int i = des; i != src; i = edge[i][pre[i]].to)
        {
            edge[edge[i][pre[i]].to][edge[i][pre[i]].id].cap -= minflow;
            edge[i][pre[i]].cap += minflow;
        }
        sum += dis[des] * minflow;
    }
    return sum;
}
struct point
{
    int x, y;
    point(){}
    point(int x, int y):x(x), y(y){}
}H[10000], M[10000];
int main()
{
    int n, m;
    while(~scanf("%d%d", &n, &m) && (n || m))
    {
        init();
        int cnth = 0, cntm = 0;
        string a;
        for(int i = 0; i < n; i++)
        {
            cin >> a;
            int siz = a.size();
            for(int j = 0; j < siz; j++)
            {
                if(a[j] == 'H')
                {
                    H[++cnth] = point(i, j);
                }
                else if(a[j] == 'm')
                {
                    M[++cntm] = point(i, j);
                }
            }
        }
        src = 0;
        des = cnth + cntm + 1;
        for(int i = 1; i <= cntm; i++)
        {
            addedge(src, i, 1, 0);
        }
        for(int i = 1; i <= cnth; i++)
        {
            addedge(cntm + i, des, 1, 0);
        }
        for(int i = 1; i <= cntm; i++)
        {
            for(int j = 1; j <= cnth; j++)
            {
                int cost = abs(M[i].x - H[j].x) + abs(M[i].y - H[j].y);
                addedge(i, j + cntm, 1, cost);
            }
        }
        printf("%d\n", MCMF(src, des));
    }
}

以下是MCMF模板,参考别人代码写的..

const int inf = 0x3f3f3f3f;
const int maxv = 10000; //顶点数
struct node
{
    int to, cap, cost, id;
    node(){}
    node(int to, int cap, int cost, int id) // id记录的是以to为边的起点指向from的边是以to为起始点的第几条边
    :to(to), cap(cap), cost(cost), id(id){}
};
vector<node>edge[maxe];
int pre[maxv], src, des, dis[maxv], g[maxv];
bool used[maxv];
void init()
{
    for(int i = 0; i < maxv; i++)
        edge[i].clear();
    memset(g, 0, sizeof(g));
}
void addedge(int from, int to, int cap, int cost)
{
    edge[from].push_back(node(to, cap, cost, g[to]));
    edge[to].push_back(node(from, 0, -cost, g[from]));
    g[to]++; g[from]++;
}
bool spfa(int src, int des)
{
    queue<int>que;
    memset(used, false, sizeof(used));
    memset(dis, inf, sizeof(dis));
    memset(pre, -1, sizeof(pre));
    dis[src] = 0;
    que.push(src);
    used[src] = true;
    while(!que.empty())
    {
        int u = que.front();
        used[u] = false;
        que.pop();
        for(int i = 0; i < edge[u].size(); i++)
        {
            int v = edge[u][i].to;
            if(edge[u][i].cap > 0 && dis[v] > dis[u] + edge[u][i].cost)
            {
                dis[v] = dis[u] + edge[u][i].cost;
                pre[v] = edge[u][i].id;
                if(!used[v])
                {
                    used[v] = true;
                    que.push(v);
                }
            }
        }
    }
    return dis[des] < inf;
}
int MCMF(int src, int des)
{
    int sum = 0;
    while(spfa(src, des))
    {
        int minflow = inf, i;
        for(i = des; i != src; i = edge[i][pre[i]].to)
        {
            minflow = min(minflow, edge[edge[i][pre[i]].to][edge[i][pre[i]].id].cap);
        }
        for(int i = des; i != src; i = edge[i][pre[i]].to)
        {
            edge[edge[i][pre[i]].to][edge[i][pre[i]].id].cap -= minflow;
            edge[i][pre[i]].cap += minflow;
        }
        sum += dis[des] * minflow;
    }
    return sum;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值