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

【HDU - 1533】 Going Home 【 MCMF or KM 】

2017-10-08 21:12 387 查看
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

题意n*m的矩阵中,有x个人和x个房子,每个房子只能够进一个人,每个人可以到任意的房子(无视其他人) ,所以每个人到任意个房子的距离就为曼哈顿距离。

思路一

可以发现这个是一个很明显的二分图的最大权值匹配问题,套上模板就行了。

这里我们将权值取负,最后就是得到的最小权值匹配。

代码

#include<bits/stdc++.h>
using namespace std;
#define  LL long long
#define fread() freopen("in.txt","r",stdin)
#define fwrite() freopen("out.txt","w",stdout)
#define CLOSE() ios_base::sync_with_stdio(false)

const int MAXN = 300+10;
const int MAXM = 1e6;
const int mod = 1e9+7;
const int inf = 0x3f3f3f3f;

int match[MAXN];
int lx[MAXN],ly[MAXN];
int slack[MAXN];
int Map[MAXN][MAXN];
int visx[MAXN],visy[MAXN];
int nx,ny;
int n,m;
typedef pair<int,int>P;
vector<P>man;
vector<P>home;
void getmap(){
char ss[MAXN];
man.clear();home
12895
.clear();
for(int i=1;i<=n;i++){
scanf("%s",ss);
for(int j=0;ss[j];j++){
if(ss[j]=='H'){
home.push_back({i,j+1});
}else if(ss[j]=='m'){
man.push_back({i,j+1});
}
}
}
nx=man.size();ny=home.size();
for(int i=0;i<man.size();i++){
for(int j=0;j<home.size();j++){
Map[i][j]=-(abs(man[i].first-home[j].first)+abs(man[i].second-home[j].second));
}
}
}
int dfs(int x){
visx[x]=1;
for(int y=0;y<ny;y++){
if(visy[y]) continue;
int t=lx[x]+ly[y]-Map[x][y];
if(t==0){
visy[y]=1;
if(match[y]==-1||dfs(match[y])) {
match[y]=x;
return 1;
}
}else if(slack[y]>t)
slack[y]=t;
}
return 0;
}
int KM(){
memset(match,-1,sizeof(match));
memset(ly,0,sizeof(ly));
for(int x=0;x<nx;x++){
lx[x]=-inf;
for(int j=0;j<ny;j++){
if(lx[x]<Map[x][j])
lx[x]=Map[x][j];
}
}
for(int x=0;x<nx;x++){
for(int i=0;i<ny;i++) slack[i]=inf;
while(1){

memset(visx,0,sizeof(visx));
memset(visy,0,sizeof(visy));

if(dfs(x)) break;
int d=inf;
for(int i=0;i<ny;i++)
if(!visy[i]) d=min(d,slack[i]);
for(int i=0;i<nx;i++)
if(visx[i]) lx[i]-=d;
for(int i=0;i<ny;i++){
if(visy[i]) ly[i]+=d;
else slack[i]-=d;
}
}
}
int ans=0;
for(int i=0;i<ny;i++)
if(match[i]!=-1) ans+=Map[match[i]][i];
return ans;
}

int main(){
CLOSE();
//  fread();
//  fwrite();
while(scanf("%d%d",&n,&m)&&(n||m)){
getmap();
printf("%d\n",-KM());
}
return 0;
}


思路二

费用流。

x个人,x个房子。

建图,

超级源点 S与所有的人连 ,流量为1,费用为0。

超级汇点 T与所有的房子连,流量为1,费用为0。

之后遍历每个人,然后加边,每个人与每个房子连,流量为1,费用为两个点为曼哈顿距离。

代码

#include<bits/stdc++.h>
using namespace std;
#define  LL long long
#define fread() freopen("in.txt","r",stdin)
#define fwrite() freopen("out.txt","w",stdout)
#define CLOSE() ios_base::sync_with_stdio(false)

const int MAXN = 10000+10;
const int MAXM = 100+10;
const int mod = 1e9+7;
const int inf = 0x3f3f3f3f;

struct Edge {
int from,to,next,cap,flow,cost;
}edge[MAXN*10];
int head[MAXN],top;
void init(){
memset(head,-1,sizeof(head));
top=0;
}
void addedge(int a,int b,int w,int c){
edge[top].from=a,edge[top].to=b,edge[top].cap=w,edge[top].flow=0,edge[top].cost=c,edge[top].next=head[a];
head[a]=top++;
edge[top].from=b,edge[top].to=a,edge[top].cap=0,edge[top].flow=0,edge[top].cost=-c,edge[top].next=head[b];
head[b]=top++;
}
int n,m;
typedef pair<int,int>P;
vector<P>man;
vector<P>home;
int S,T;
void getmap(){
char ss[MAXN];
man.clear();home.clear();
int a,b;a=b=0;
for(int i=1;i<=n;i++){
scanf("%s",ss);
for(int j=0;ss[j];j++){
if(ss[j]=='H'){
home.push_back({i,j+1});
a++;
}else if(ss[j]=='m'){
man.push_back({i,j+1});
b++;
}
}
}
S=0;T=a+b+1;
//  printf("a=%d b=%d\n",a,b);
for(int i=0;i<man.size();i++)
addedge(S,i+1,1,0);
for(int j=0;j<home.size();j++)
addedge(a+j+1,T,1,0);
for(int i=0;i<man.size();i++){
for(int j=0;j<home.size();j++){
addedge(i+1,a+j+1,1,abs(man[i].first-home[j].first)+abs(man[i].second-home[j].second));
}
}
}
bool vis[MAXN];int dis[MAXN];
int pre[MAXN];
bool spfa(int s,int e){
queue<int>Q;
memset(vis,0,sizeof(vis));
memset(dis,0x3f,sizeof(dis));
memset(pre,-1,sizeof(pre));
dis[s]=0;vis[s]=1;Q.push(s);
while(!Q.empty()){
int now=Q.front();Q.pop();vis[now]=0;
for(int i=head[now];i!=-1;i=edge[i].next){
Edge  e=edge[i];
if(e.cap>e.flow&&dis[e.to]>dis[now]+e.cost){
dis[e.to]=dis[now]+e.cost;
pre[e.to]=i;
if(!vis[e.to]){
vis[e.to]=1;
Q.push(e.to);
}
}
}
}
return pre[e]!=-1;
}
int MCMF(int s,int t,int &cost,int &flow){
flow=cost=0;
while(spfa(s,t)){
int Min=inf;
for(int i=pre[t];i!=-1;i=pre[edge[i^1].to]){
Min=min(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;
}
}
int main(){
CLOSE();
//  fread();
//  fwrite();
while(scanf("%d%d",&n,&m)&&(n||m)){
init();
getmap();
int flow,ans;
MCMF(S,T,ans,flow);
printf("%d\n",ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: