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

人和房子数量相同,每个人进房子,费用是人到房子的曼哈顿距离,求最小费用

可用最小费用最大流求解,建立虚拟的源点和汇点即可

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std ;
int ABS(int x)
{
return x>?x:-x ;
}
struct point{
int x,y ;
}mm[],hh[] ;
const int INF=0xfffffff ;
struct node{
int s,t,cap,cost,nxt ;
}e[] ;
int sumflow ;
int n,m,cnt,head[],vis[],dis[],pre[] ;
char M[][] ;
void add(int s,int t,int cap,int cost)
{
e[cnt].s=s ;e[cnt].t=t ;e[cnt].cap=cap ;e[cnt].cost=cost ;e[cnt].nxt=head[s] ;head[s]=cnt++ ;
e[cnt].s=t ;e[cnt].t=s ;e[cnt].cap= ;e[cnt].cost=-cost ;e[cnt].nxt=head[t] ;head[t]=cnt++ ;
}
int spfa(int s,int t,int N)
{
for(int i= ;i<=N ;i++)
dis[i]=INF ;
dis[s]= ;
memset(vis,,sizeof(vis)) ;
memset(pre,-,sizeof(pre)) ;
vis[s]= ;
queue <int> q ;
q.push(s) ;
while(!q.empty())
{
int u=q.front() ;
q.pop() ;
vis[u]= ;
for(int i=head[u] ;i!=- ;i=e[i].nxt)
{
int tt=e[i].t ;
if(e[i].cap && dis[tt]>dis[u]+e[i].cost)
{
dis[tt]=dis[u]+e[i].cost ;
pre[tt]=i ;
if(!vis[tt])
{
vis[tt]= ;
q.push(tt) ;
}
}
}
}
if(dis[t]==INF)return ;
return ;
}
int MCMF(int s,int t,int N)
{
int flow,minflow,mincost ;
mincost=flow= ;
while(spfa(s,t,N))
{
minflow=INF ;
for(int i=pre[t] ;i!=- ;i=pre[e[i].s])
minflow=min(minflow,e[i].cap) ;
flow+=minflow ;
for(int i=pre[t] ;i!=- ;i=pre[e[i].s])
{
e[i].cap-=minflow ;
e[i^].cap+=minflow ;
}
mincost+=dis[t]*minflow ;
}
sumflow=flow ;//最大流
return mincost ;
}
int main()
{
while(scanf("%d%d",&n,&m))
{
if(!n && !m)break ;
cnt= ;
memset(head,-,sizeof(head)) ;
for(int i= ;i<n ;i++)
scanf("%s",M[i]) ;
int cm,ch ;
cm=ch= ;
for(int i= ;i<n ;i++)
{
for(int j= ;j<m ;j++)
{
if(M[i][j]=='m')
{
mm[cm].x=i ;
mm[cm++].y=j ;
}
if(M[i][j]=='H')
{
hh[ch].x=i ;
hh[ch++].y=j ;
}
}
}
int SS= ;
int ST=*ch+ ;
for(int i= ;i<ch ;i++)
{
add(,i+,,) ;
for(int j= ;j<ch ;j++)
{
int temp=ABS(mm[i].x-hh[j].x)+ABS(mm[i].y-hh[j].y) ;
add(i+,j++ch,,temp) ;
}
add(i++ch,ST,,) ;
}
printf("%d\n",MCMF(SS,ST,ST+)) ;
}
return ;
}

HDU 1533的更多相关文章

  1. 【HDU 1533】 Going Home (KM)

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

  2. POJ 2195 Going Home / HDU 1533(最小费用最大流模板)

    题目大意: 有一个最大是100 * 100 的网格图,上面有 s 个 房子和人,人每移动一个格子花费1的代价,求最小代价让所有的人都进入一个房子.每个房子只能进入一个人. 算法讨论: 注意是KM 和 ...

  3. HDU 1533 Going Home(KM完美匹配)

    HDU 1533 Going Home 题目链接 题意:就是一个H要相应一个m,使得总曼哈顿距离最小 思路:KM完美匹配,因为是要最小.所以边权建负数来处理就可以 代码: #include <c ...

  4. HDU 1533 最小费用最大流(模板)

    http://acm.hdu.edu.cn/showproblem.php?pid=1533 这道题直接用了模板 题意:要构建一个二分图,家对应人,连线的权值就是最短距离,求最小费用 要注意void ...

  5. hdu 1533 Going Home 最小费用最大流

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1533 On a grid map there are n little men and n house ...

  6. Going Home HDU - 1533 费用流

    http://acm.hdu.edu.cn/showproblem.php?pid=1533 给一个网格图,每两个点之间的匹配花费为其曼哈顿距离,问给每个的"$m$"匹配到一个&q ...

  7. 【hdu 1533】Going Home

    [链接]http://acm.hdu.edu.cn/showproblem.php?pid=1533 [题意] 一个N*M地图上有相同数量的字符H和字符m,m代表一个 人,H代表一个房子.人到房子的花 ...

  8. HDU 1533:Going Home(KM算法求二分图最小权匹配)

    http://acm.hdu.edu.cn/showproblem.php?pid=1533 Going Home Problem Description   On a grid map there ...

  9. HDU 1533 & KM模板

    题意 求二分图最小完备匹配. SOL 建个图那么方便的事情是吧...然后边权都是正的(好像根边权也没什么关系),既然要求最小那么把边权取个相反数跑个KM就好了.. CODE: /*========== ...

随机推荐

  1. CentOS7.0安装Nginx 1.10.0

    首先由于nginx的一些模块依赖一些lib库,所以在安装nginx之前,必须先安装这些lib库,这些依赖库主要有g++.gcc.openssl-devel.pcre-devel和zlib-devel ...

  2. WCF标准绑定以及传输协议与编码格式

    WCF 定义了9 种标准绑定: 基本绑定(Basic Binding) 由BasicHttpBinding类提供.基本绑定能够将WCF服务公开为旧的ASMX Web服务,使得旧的客户端能够与新的服务协 ...

  3. [one day one question] webpack 打包报错 Cannot assign to read only property 'exports' of object '#<Object>'

    问题描述: webpack 打包报错 Cannot assign to read only property 'exports' of object '#<Object>',这怎么破? 解 ...

  4. Python笔记 #12# Dictionary & Pandas: Object Creation

    Document of Dictionaries 10 Minutes to pandas tutorialspoint import pandas as pd data = [['Alex',10] ...

  5. imx6------watchdog导致不进系统

    刚上板子,把大部分驱动都停了,不过watchdog的驱动没停,当时想没应用程序所以watchdog不用管,没想到 就是watchdog卡住了,有程序open了watchdog但是没有write,结果t ...

  6. 【javascript】数据结构-集合

    <!DOCTYPE html> <html> <head> <title>集合</title> <meta charset=" ...

  7. Tomcat灵活配置多项目,多端口,多域名,多虚拟目录

    Tomcat的配置都在Tomcat的安装目录的conf文件夹下的server.xml文件 最初内容:(去掉所有注释) <?xml version="1.0" encoding ...

  8. Redis之数据备份与恢复

    Redis 数据备份与恢复 Redis SAVE 命令用于创建当前数据库的备份. 语法 redis Save 命令基本语法如下: redis 127.0.0.1:6379> SAVE 实例 re ...

  9. python 删除字典元素

    myDict = {,,,} print(myDict) if 'a' in myDict: del myDict['a'] print(myDict)

  10. angular-cli 文档

    Angular/angular-cli 原文来自:https://github.com/angular/angular-cli Angular/angular-cli 原文来自:https://git ...