[POJ 2195]Going Home[费用流]

题目链接: [POJ 2195]Going Home[费用流]

题意分析:

m代表人,H代表房子,两者数量一致,现在问:让所有人都找到一个家待下来,最少花费多少钱?(一个点可有多个人,但一个家只能待一个人)

解题思路:

人与源点连一条费用为0,容量为1的边,房子与汇点连一条容量为1, 费用为0的边,人与房子连一条容量为0,费用为两者之间的距离。

个人感受:

蛮裸的费用流。

具体代码如下:

#include<algorithm>
#include<cctype>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iomanip>
#include<iostream>
#include<map>
#include<queue>
#include<set>
#include<sstream>
#include<stack>
#include<string>
#define pii pair<int, int>
#define ll long long
#define pr(x) cout << #x << " = " << (x) << '\n';
using namespace std;

const int MAXN = 310;
const int MAXM = 4e4 + 100;
const int INF = 0x3f3f3f3f;
struct Edge
{
    int to,next,cap,flow,cost;
}edge[MAXM];
int head[MAXN],tol;
int pre[MAXN],dis[MAXN];
bool vis[MAXN];
int N;//节点总个数,节点编号从0~N-1
void init(int n)
{
    N = n;
    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, m;
    char s[200];
    while (~scanf("%d%d", &n, &m) && (m | n)) {
        vector<pii> man, house;
        for (int i = 0; i < n; ++i) {
            scanf("%s", s);
            for (int j = 0; j < m; ++j) {
                if (s[j] == 'H') house.push_back(make_pair(i, j));
                if (s[j] == 'm') man.push_back(make_pair(i, j));
            }
        }

        int s = 0, t = house.size() + man.size() + 1;
        init(t + 1);
        for (int i = 0; i < house.size(); ++i)
            addedge(i + 1 + man.size(), t, 1, 0);

        for (int i = 0; i < man.size(); ++i) {
            pii a = man[i];
            addedge(s, i + 1, 1, 0);
            for (int j = 0; j < house.size(); ++j) {
                pii b = house[j];
                int cost = abs(a.first - b.first) + abs(a.second - b.second);
                addedge(i + 1, j + 1 + man.size(), 1, cost);
            }
        }

        int ans;
        minCostMaxflow(s,t,ans);
        printf("%d\n", ans);
    }
    return 0;
}


你可能感兴趣的:(费用流)