HDU 1533 — Going Home 最小费用最大流

原题:http://acm.hdu.edu.cn/showproblem.php?pid=1533

题意:

给定一个n*m的矩阵,有x个人要到x个房子里,每个房子一人;

m表示人,H表示房子;人每走一步花费1美元;

问所有人移动到房子里的最小花费;


思路:

源点s(编号0)— 人(cap 1,cost  0);

人 — 房子 (cap  1,cost为最短路径花费,即坐标差);

房子 —  汇点t(编号n*m+1)(cap  1,cost  0);



#include<cstdio>
#include<cstring>
#include<math.h>
#include<string>
#include<queue>
#include<vector>
#include<iostream>
#include<algorithm>
#define ll int
#define inf 0x3f3f3f3f

using namespace std;
const int N = 10000+500;
int x[N], y[N], xx[N], yy[N];
int n, m;

struct Edge 
{
    ll to, cap, cost, nex;
    Edge(){}
    Edge(ll to,ll cap,ll cost,ll next):to(to),cap(cap),cost(cost),nex(next){}
} edge[1000000];

ll head[N], edgenum;
ll D[N], A[N], P[N];
bool inq[N];

void add(ll from, ll to, ll cap, ll cost) 
{
    edge[edgenum] = Edge(to,cap,cost,head[from]);
    head[from] = edgenum++;
    edge[edgenum] = Edge(from,0,-cost,head[to]);
    head[to] = edgenum++;
}

bool spfa(ll s, ll t, ll &flow, ll &cost) 
{
    for(ll i = 0; i <= t; i++) 
	D[i] = inf;
    memset(inq, 0, sizeof inq);
    queue<ll>q;
    q.push(s);
    D[s] = 0; 
	A[s] = inf;
    while(!q.empty()) 
	{
        ll u = q.front(); 
		q.pop();
        inq[u] = 0;
        for(ll i = head[u]; i!=-1; i = edge[i].nex)
        {
            Edge &e = edge[i];
            if(e.cap && D[e.to] > D[u] + e.cost)
            {
                D[e.to] = D[u] + e.cost;
                P[e.to] = i;
                A[e.to] = min(A[u], e.cap);
                if(!inq[e.to])
                {
					inq[e.to]=1; 
					q.push(e.to);
				}
            }
        }
    }
    if(D[t] == inf) 
	return false;
    cost += D[t] * A[t];
    flow += A[t];
    ll u = t;
    while(u != s) 
	{
        edge[ P[u] ].cap -= A[t];
        edge[P[u]^1].cap += A[t];
        u = edge[P[u]^1].to;
    }
    return true;
}

ll Mincost(ll s,ll t)
{
    ll flow = 0, cost = 0;
    while(spfa(s, t, flow, cost));
    return cost;
}

void init()
{
	memset(head,-1,sizeof head); 
	edgenum = 0;
}

int main()
{
	while(scanf("%d%d", &n, &m)!=EOF)
	{
		if(n == 0 && m == 0)
		break;
		char str[150][150];
		int cnt1 = 0, cnt2 = 0;
		int s = 0, t = n*m+1;
		init();
		for(int i = 1;i<=n;i++)
		scanf("%s", str[i]+1);
		for(int i = 1;i<=n;i++)
		{
			for(int j = 1;j<=m;j++)
			{
				if(str[i][j] == 'm')
				{
					add(s, (i-1)*m+j, 1, 0);
					x[cnt1] = i;
					y[cnt1] = j;
					cnt1++;
				}
				else if(str[i][j] == 'H')
				{
					add((i-1)*m+j, t, 1, 0);
					xx[cnt2] = i;
					yy[cnt2] = j;
					cnt2++;
				}
			}
		}
		for(int i = 0;i<cnt1;i++)
		{
			for(int j = 0;j<cnt2;j++)
			add((x[i]-1)*m+y[i], (xx[j]-1)*m+yy[j], 1, abs(x[i]-xx[j])+abs(y[i]-yy[j]));
		}
		printf("%d\n", Mincost(s, t));
	}
	return 0;
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值