您的位置:首页 > 编程语言 > Go语言

hdoj 1533 Going Home 【最小费用最大流】【KM入门题】

2015-08-24 14:41 573 查看

Going Home

Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 3443 Accepted Submission(s): 1763



Problem 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



第一道费用流。 O(∩_∩)O~~

题意:一个N*M地图上有相同数量的字符H和字符m,m代表一个 人,H代表一个房子。人到房子的花销是它们在图中的曼哈顿距离,问你让所有人回到房子所需要的最小费用(一个房子只能容纳一个人)。

思路:设置超级源点source 连接所有字符H,容量为1,费用为0。每个字符H向所有字符m连边,容量为1,费用为它们的曼哈顿距离。最后每个字符m向超级汇点sink连边,容量为1,费用为0。最后source到sink跑一遍最小费用最大流就ok了。(我建边时把字符H看做人了,所以建的反向边,对这道题没有影响)

AC代码:

#include <cstdio>
#include <cstring>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#define MAXN 200+10
#define MAXM 80000+100
#define INF 0x3f3f3f3f
using namespace std;
struct Edge
{
    int from, to, cap, flow, cost, next;
};
Edge edge[MAXM];
int head[MAXN], edgenum;
int pre[MAXN], dist[MAXN];
bool vis[MAXN];
int N, M;
int source, sink;//超级源点 超级汇点
void init()
{
    edgenum = 0;
    memset(head, -1, sizeof(head));
}
void addEdge(int u, int v, int w, int c)
{
    Edge E1 = {u, v, w, 0, c, head[u]};
    edge[edgenum] = E1;
    head[u] = edgenum++;
    Edge E2 = {v, u, 0, 0, -c, head[v]};
    edge[edgenum] = E2;
    head[v] = edgenum++;
}
int dis(int x1, int y1, int x2, int y2)
{
    return abs(x1 - x2) + abs(y1 - y2);
}
struct Node
{
    int x, y;
};
Node m[110], H[110];//存储字符坐标
int m_cnt;//m字符计数器
int H_cnt;//H字符计数器
void getMap()
{
    m_cnt = H_cnt = 0;
    char str[110][110];
    for(int i = 0; i < N; i++)
    {
        scanf("%s", str[i]);
        for(int j = 0; j < M; j++)
        {
            if(str[i][j] == 'm')
            {
                ++m_cnt;
                m[m_cnt].x = i;
                m[m_cnt].y = j;
            }
            if(str[i][j] == 'H')
            {
                ++H_cnt;
                H[H_cnt].x = i;
                H[H_cnt].y = j;
            }
        }
    }
    int k = m_cnt;//人数 或者 房子数
    source = 0;
    sink = 2*k+1;
    for(int i = 1; i <= k; i++)
    {
        addEdge(source, i, 1, 0);
        addEdge(i + k, sink, 1, 0);
        for(int j = 1; j <= k; j++)
        {
            int d = dis(H[i].x, H[i].y, m[j].x, m[j].y);
            addEdge(i, j + k, 1, d);
        }
    }
}
bool SPFA(int s, int t)//寻找花销最少的路径
{
    queue<int> Q;
    memset(dist, INF, sizeof(dist));
    memset(vis, false, sizeof(vis));
    memset(pre, -1, sizeof(pre));
    dist[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)
        {
            Edge E = edge[i];
            if(dist[E.to] > dist[u] + E.cost && E.cap > E.flow)//可以松弛 且 没有满流
            {
                dist[E.to] = dist[u] + E.cost;
                pre[E.to] = i;//记录前驱边 的编号
                if(!vis[E.to])
                {
                    vis[E.to] = true;
                    Q.push(E.to);
                }
            }
        }
    }
    return pre[t] != -1;//可达返回true
}
void MCMF(int s, int t, int &cost, int &flow)
{
    flow = 0;//总流量
    cost = 0;//总费用
    while(SPFA(s, t))//每次寻找花销最小的路径
    {
        int Min = INF;
        //通过反向弧 在源点到汇点的最少花费路径 找最小增广流
        for(int i = pre[t]; i != -1; i = pre[edge[i^1].to])
        {
            Edge E = edge[i];
            Min = min(Min, E.cap - E.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;//流量累加
    }
}
int main()
{
    while(scanf("%d%d", &N, &M), N||M)
    {
        init();
        getMap();
        int cost, flow;
        MCMF(source, sink, cost, flow);
        printf("%d\n", cost);
    }
    return 0;
}


KM算法: 重刷

求最大流,用负权,最后结果取反。

#include <cstdio>
#include <cstring>
#include <algorithm>
#define INF 10000000
using namespace std;
int lx[110], ly[110];
int Map[110][110];
bool visx[110], visy[110];
int slack[110];
int match[110];
int nx, ny;
int N, M;
char str[110][110];
int dis(int x1, int y1, int x2, int y2)
{
    return abs(x1 - x2) + abs(y1 - y2);
}
struct Node
{
    int x, y;
};
Node m[110], H[110];//存储字符坐标
int m_cnt;//m字符计数器
int H_cnt;//H字符计数器
void getMap()
{
    m_cnt = H_cnt = 0;
    char str[110][110];
    for(int i = 0; i < N; i++)
    {
        scanf("%s", str[i]);
        for(int j = 0; j < M; j++)
        {
            if(str[i][j] == 'm')
            {
                ++m_cnt;
                m[m_cnt].x = i;
                m[m_cnt].y = j;
            }
            if(str[i][j] == 'H')
            {
                ++H_cnt;
                H[H_cnt].x = i;
                H[H_cnt].y = j;
            }
        }
    }
    int k = m_cnt;//人数 或者 房子数
    nx = ny = k;
    for(int i = 1; i <= k; i++)
    {
        for(int j = 1; j <= k; j++)
        {
            int d = dis(H[i].x, H[i].y, m[j].x, m[j].y);
            Map[i][j] = -d;
        }
    }
}
int DFS(int x)
{
    visx[x] = true;
    for(int y = 1; y <= ny; y++)
    {
        if(visy[y]) continue;
        int t = lx[x] + ly[y] - Map[x][y];
        if(t == 0)
        {
            visy[y] = true;
            if(match[y] == -1 || DFS(match[y]))
            {
                match[y] = x;
                return 1;
            }
        }
        else if(slack[y] > t)
            slack[y] = t;
    }
    return 0;
}
void KM()
{
    memset(match, -1, sizeof(match));
    memset(ly, 0, sizeof(ly));
    for(int x = 1; x <= nx; x++)
    {
        lx[x] = -INF;
        for(int y = 1; y <= ny; y++)
            lx[x] = max(lx[x], Map[x][y]);
    }
    for(int x = 1; x <= nx; x++)
    {
        for(int i = 1; i <= ny; i++)
            slack[i] = INF;
        while(1)
        {
            memset(visx, false, sizeof(visx));
            memset(visy, false, sizeof(visy));
            if(DFS(x)) break;
            int d = INF;
            for(int i = 1; i <= ny; i++)
            {
                if(!visy[i] && slack[i] < d)
                    d = slack[i];
            }
            for(int i = 1; i <= nx; i++)
            {
                if(visx[i])
                   lx[i] -= d;
            }
            for(int i = 1; i <= ny; i++)
            {
                if(visy[i])
                    ly[i] += d;
                else
                    slack[i] -= d;
            }
        }
    }
    int ans = 0;
    for(int i = 1; i <= ny; i++)
        ans += Map[match[i]][i];
    printf("%d\n", -ans);
}
int main()
{
    while(scanf("%d%d", &N, &M), N||M)
    {
        getMap();
        KM();
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: