POJ2195 Going Home[费用流|二分图最大权匹配]

时间:2021-11-26 19:42:13
Going Home
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 22088   Accepted: 11155

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. 
POJ2195 Going Home[费用流|二分图最大权匹配]
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

Source


每个人和每个房子连边,二分图最大权匹配
用spfa费用流求解(或者KM)
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long ll;
const int N=,M=1e6+,INF=1e9;
int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-; c=getchar();}
while(c>=''&&c<=''){x=x*+c-''; c=getchar();}
return x*f;
}
int n,m,s,t,n1,n2;
char ss[];
struct data{
int x,y;
}a[N],b[N];
inline int dis(data &a,data &b){return abs(a.x-b.x)+abs(a.y-b.y);} struct edge{
int v,ne,c,f,w;
}e[M<<];
int cnt,h[N];
inline void ins(int u,int v,int c,int w){
cnt++;
e[cnt].v=v;e[cnt].c=c;e[cnt].f=;e[cnt].w=w;
e[cnt].ne=h[u];h[u]=cnt;
cnt++;
e[cnt].v=u;e[cnt].c=;e[cnt].f=;e[cnt].w=-w;
e[cnt].ne=h[v];h[v]=cnt;
}
void build(){
cnt=;
memset(h,,sizeof(h));
s=;t=n1+n2+;
for(int i=;i<=n1;i++)
for(int j=;j<=n2;j++) ins(i,n1+j,,dis(a[i],b[j]));
for(int i=;i<=n1;i++) ins(s,i,,);
for(int i=;i<=n2;i++) ins(n1+i,t,,);
}
int d[N],q[N],head,tail,inq[N],pre[N],pos[N];
inline void lop(int &x){if(x==N)x=;}
bool spfa(){
memset(d,,sizeof(d));
memset(inq,,sizeof(inq));
head=tail=;
d[s]=;inq[s]=;q[tail++]=s;
pre[t]=-;
while(head!=tail){
int u=q[head++];inq[u]=;lop(head);
for(int i=h[u];i;i=e[i].ne){
int v=e[i].v,w=e[i].w;
if(d[v]>d[u]+w&&e[i].c>e[i].f){
d[v]=d[u]+w;
pre[v]=u;pos[v]=i;
if(!inq[v])q[tail++]=v,inq[v]=,lop(tail);
}
}
}
return pre[t]!=-;
}
int mcmf(){
int flow=,cost=;
while(spfa()){
int f=INF;
for(int i=t;i!=s;i=pre[i]) f=min(f,e[pos[i]].c-e[pos[i]].f);
flow+=f;cost+=d[t]*f;
for(int i=t;i!=s;i=pre[i]){
e[pos[i]].f+=f;
e[((pos[i]-)^)+].f-=f;
}
}
return cost;
}
int main(int argc, const char * argv[]){
while(true){
n1=n2=;
n=read();m=read();
if(n==&&m==) break;
for(int i=;i<=n;i++){
scanf("%s",ss+);
for(int j=;j<=m;j++){
if(ss[j]=='H') a[++n1]=(data){i,j};
if(ss[j]=='m') b[++n2]=(data){i,j};
}
}
build();
printf("%d\n",mcmf());
}
}
 
 
 
 

POJ2195 Going Home[费用流|二分图最大权匹配]的更多相关文章

  1. 【BZOJ 3308】 3308&colon; 九月的咖啡店 (费用流&vert;二分图最大权匹配)

    3308: 九月的咖啡店 Time Limit: 30 Sec  Memory Limit: 128 MBSubmit: 244  Solved: 86 Description 深绘里在九份开了一家咖 ...

  2. POJ2195 Going Home (最小费最大流&vert;&vert;二分图最大权匹配) 2017-02-12 12&colon;14 131人阅读 评论&lpar;0&rpar; 收藏

    Going Home Description On a grid map there are n little men and n houses. In each unit time, every l ...

  3. &lbrack;hdu1533&rsqb;二分图最大权匹配 &vert;&vert; 最小费用最大流

    题意:给一个n*m的地图,'m'表示人,'H'表示房子,求所有人都回到房子所走的距离之和的最小值(距离为曼哈顿距离). 思路:比较明显的二分图最大权匹配模型,将每个人向房子连一条边,边权为曼哈顿距离的 ...

  4. 【bzoj3291】Alice与能源计划 模拟费用流&plus;二分图最大匹配

    题目描述 在梦境中,Alice来到了火星.不知为何,转眼间Alice被任命为火星能源部长,并立刻面临着一个严峻的考验. 为了方便,我们可以将火星抽象成平面,并建立平面直角坐标系.火星上一共有N个居民点 ...

  5. &commat;noi&period;ac - 507&commat; 二分图最大权匹配

    目录 @description@ @solution@ @accepted code@ @details@ @description@ 有一天你学了一个能解决二分图最大权匹配的算法,你决定将这个算法应 ...

  6. Hdu2255 奔小康赚大钱(二分图最大权匹配KM算法)

    奔小康赚大钱 Problem Description 传说在遥远的地方有一个非常富裕的村落,有一天,村长决定进行制度改革:重新分配房子. 这可是一件大事,关系到人民的住房问题啊.村里共有n间房间,刚好 ...

  7. &lbrack;ACM&rsqb; HDU 2255 奔小康赚大钱 &lpar;二分图最大权匹配,KM算法)

    奔小康赚大钱 Problem Description 传说在遥远的地方有一个很富裕的村落,有一天,村长决定进行制度改革:又一次分配房子. 这但是一件大事,关系到人民的住房问题啊. 村里共同拥有n间房间 ...

  8. HDU2255 奔小康赚大钱 —— 二分图最大权匹配 KM算法

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2255 奔小康赚大钱 Time Limit: 1000/1000 MS (Java/Others)    ...

  9. 二分图最大权匹配——KM算法

    前言 这东西虽然我早就学过了,但是最近才发现我以前学的是假的,心中感慨万千(雾),故作此篇. 简介 带权二分图:每条边都有权值的二分图 最大权匹配:使所选边权和最大的匹配 KM算法,全称Kuhn-Mu ...

随机推荐

  1. Qt model和tableview的使用

    QT中的model和tableview都是采用index索引   index含有两个成员变量一个是row   一个是column  对应该索引的行号.列号 model提供数据    view提供视图  ...

  2. Java反射机制可以动态修改实例中final修饰的成员变量吗?

    问题:Java反射机制可以动态修改实例中final修饰的成员变量吗? 回答是分两种情况的. 1. 当final修饰的成员变量在定义的时候就初始化了值,那么java反射机制就已经不能动态修改它的值了. ...

  3. &lbrack;Tools&rsqb;迁移Confluence&comma; JIRA&comma; Fisheye

    [背景] 原先的Confluence, JIRA, Fisheye都部署在一台服务器(192.168.200.203)上,导致这台机器太卡,公司又分配了两台虚拟机来分开这几个应用(192.168.20 ...

  4. HDOJ2021发工资咯:)

    发工资咯:) Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  5. Python 接口测试(十)

    这里对接口测试9 进行优化升级,前端进行重构后的代码,源码已经开源 经过将近一个月的编写 , TIAPTest 接口测试平台 , 已经部署到服务器,开始运行了. http://60.205.187.1 ...

  6. Python基础篇&lpar;九&rpar;

    Key Words: 文件迭代器,标准输入,GUI工具包,数据库操作SQLlite,socket编程 文件迭代器 >>> f= open("some.txt",& ...

  7. 巩固java&lpar;五&rpar;----通过实例理解java多态

    package duotai; class A{ public String show(){ return "A"; } } class B extends A{ public S ...

  8. Flask上下文管理源码--亲自解析一下

    前戏 偏函数 def index(a,b): return a+b # 原来的调用方法 # ret=index(1,2) # print(ret) # 偏函数--帮助开发者自动传递参数 import ...

  9. Windows Server 2016-Windows Server Backup功能

    一.Windows Server Backup 介绍: Windows Server Backup 是一种功能,它提供了一组向导和其他工具,大概从WinSer2008开始Win Ser Backup引 ...

  10. HDU-4856 Tunnels (BFS&plus;状压DP)

    Problem Description Bob is travelling in Xi’an. He finds many secret tunnels beneath the city. In hi ...