HDU 1533
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的更多相关文章
- 【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, ...
- POJ 2195 Going Home / HDU 1533(最小费用最大流模板)
题目大意: 有一个最大是100 * 100 的网格图,上面有 s 个 房子和人,人每移动一个格子花费1的代价,求最小代价让所有的人都进入一个房子.每个房子只能进入一个人. 算法讨论: 注意是KM 和 ...
- HDU 1533 Going Home(KM完美匹配)
HDU 1533 Going Home 题目链接 题意:就是一个H要相应一个m,使得总曼哈顿距离最小 思路:KM完美匹配,因为是要最小.所以边权建负数来处理就可以 代码: #include <c ...
- HDU 1533 最小费用最大流(模板)
http://acm.hdu.edu.cn/showproblem.php?pid=1533 这道题直接用了模板 题意:要构建一个二分图,家对应人,连线的权值就是最短距离,求最小费用 要注意void ...
- 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 ...
- Going Home HDU - 1533 费用流
http://acm.hdu.edu.cn/showproblem.php?pid=1533 给一个网格图,每两个点之间的匹配花费为其曼哈顿距离,问给每个的"$m$"匹配到一个&q ...
- 【hdu 1533】Going Home
[链接]http://acm.hdu.edu.cn/showproblem.php?pid=1533 [题意] 一个N*M地图上有相同数量的字符H和字符m,m代表一个 人,H代表一个房子.人到房子的花 ...
- HDU 1533:Going Home(KM算法求二分图最小权匹配)
http://acm.hdu.edu.cn/showproblem.php?pid=1533 Going Home Problem Description On a grid map there ...
- HDU 1533 & KM模板
题意 求二分图最小完备匹配. SOL 建个图那么方便的事情是吧...然后边权都是正的(好像根边权也没什么关系),既然要求最小那么把边权取个相反数跑个KM就好了.. CODE: /*========== ...
随机推荐
- Virtualbox中win7虚拟机中U盘不可用问题的解决
Virtualbox版本是5.0.0,主机运行多是Ubuntu12.04 LTS,虚拟机是Win7 X64.起初Win7正常运行,Virtualbox的增强功能已安装.下面是如何一步一步解决U盘不可用 ...
- MySQL5.7多源复制
MySQL5.7开始支持多源复制,也就是多主一从的复制架构: 使用多源复制的考虑: 1.灾备作用:将各个库汇总在一起,就算是其他库都挂了(整个机房都无法连接了),还有最后一个救命稻草: 2.备份:直接 ...
- java中string.trim()函数的作用
trim /[trɪm] / 英文意思:整理,修理,修剪,整齐的 trim()的作用:去掉字符串首尾的空格. public static void main(String arg[]){ Strin ...
- 1.2、Django 视图与网址__进阶
Django 视图与网址进阶 1.1.简单使用: 把我们新定义的app加到settings.py中的INSTALL_APPS中 修改 HelloDjango/HelloDjango/settings. ...
- 泰德激光打标软件 包含 #include "Main.h" 时 原本正确的单元却报错
问题:泰德激光打标软件 ,当新增单元需要包含 #include "Main.h" 时, 原本正确的单元却报错. 办法:包含 #include "Main.h" ...
- (9)SpriteFrameCache和TextureCache
简介 SpriteFrameCache 主要服务于多张碎图合并出来的纹理图片.这种纹理在一张大图中包含了多张小图,直接通过TextureCache引用会有诸多不便,因而衍生出来精灵框帧的处理方式,即把 ...
- in `connect': SSL_connect returned=1 errno=0 state=SSLv3 read server certificate B: certificate verify failed (OpenSSL::SSL::SSLError)
最近在用ruby的一些库的时候,总是出现这个错误. 在使用net/imap库的时候,或者net/http库(主要是用到了https,https是用了ssl) 的时候,具体如下: 错误提示:E:/Rub ...
- word 杂记
45.error和exception有什么区别? 答案:Error表示系统级的错误和程序不必处理的异常,我们无法处理它. Exception表示是可以捕捉或者需要程序进行处理的异常. 47.abstr ...
- python使用zipfile解压中文乱码问题
在zipfile.ZipFile中获得的filename有中日文则很大可能是乱码,这是因为 在zip标准中,对文件名的 encoding 用的不是 unicode,而可能是各种软件根据系统的默认字符集 ...
- ImportError: libcublas.so.9.0: cannot open shared object file: No such file or directory 【学习笔记】【原创】
作者:庄泽彬(欢迎转载,请注明作者) 说明:千辛万苦终于在ubuntu18.04上安装好cuda9.1与cudnn7.0.5,但是导入import tensorflow as tf却报了这个错误. 上 ...