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

相邻的容量为inf,费用为1,S到m容量为1,费用为0 ,H到T容量为1,费用为0。

建图跑-最小费用最大流-就行了。

 #define IOS ios_base::sync_with_stdio(0); cin.tie(0);
#include <cstdio>//sprintf islower isupper
#include <cstdlib>//malloc exit strcat itoa system("cls")
#include <iostream>//pair
#include <fstream>//freopen("C:\\Users\\13606\\Desktop\\Input.txt","r",stdin);
#include <bitset>
//#include <map>
//#include<unordered_map>
#include <vector>
#include <stack>
#include <set>
#include <string.h>//strstr substr
#include <string>
#include <time.h>// srand(((unsigned)time(NULL))); Seed n=rand()%10 - 0~9;
#include <cmath>
#include <deque>
#include <queue>//priority_queue<int, vector<int>, greater<int> > q;//less
#include <vector>//emplace_back
//#include <math.h>
#include <cassert>
//#include <windows.h>//reverse(a,a+len);// ~ ! ~ ! floor
#include <algorithm>//sort + unique : sz=unique(b+1,b+n+1)-(b+1);+nth_element(first, nth, last, compare)
using namespace std;//next_permutation(a+1,a+1+n);//prev_permutation
//******************
int abss(int a);
int lowbit(int n);
int Del_bit_1(int n);
int maxx(int a,int b);
int minn(int a,int b);
double fabss(double a);
void swapp(int &a,int &b);
clock_t __STRAT,__END;
double __TOTALTIME;
void _MS(){__STRAT=clock();}
void _ME(){__END=clock();__TOTALTIME=(double)(__END-__STRAT)/CLOCKS_PER_SEC;cout<<"Time: "<<__TOTALTIME<<" s"<<endl;}
//***********************
#define rint register int
#define fo(a,b,c) for(rint a=b;a<=c;++a)
#define fr(a,b,c) for(rint a=b;a>=c;--a)
#define mem(a,b) memset(a,b,sizeof(a))
#define pr printf
#define sc scanf
#define ls rt<<1
#define rs rt<<1|1
typedef pair<int,int> PII;
typedef vector<int> VI;
typedef long long ll;
const double E=2.718281828;
const double PI=acos(-1.0);
//const ll INF=(1LL<<60);
const int inf=(<<);
const double ESP=1e-;
const int mod=(int)1e9+;
const int N=(int)1e5+; int n,m,S,T;
class MCMF
{
public:
struct node
{
int u,v,f,w,nxt;
}edge[N];
int head[N],tot;
int h[N],dis[N],PrePoint[N],PreEdge[N];
void Init(int n)
{
tot=-;
for(int i=;i<=n;++i)
head[i]=-,h[i]=;
}
void add(int x,int y,int f,int w)
{
++tot;
edge[tot]={x,y,f,w,head[x]};
head[x]=tot;
}
void Add(int x,int y,int f,int w)
{
add(x,y,f,w);
add(y,x,,-w);
}
PII Dij()
{
int max_flow=,min_cost=;
while()
{
priority_queue<PII>q;
for(int i=;i<=n;++i)
dis[i]=inf;
dis[S]=;
q.push({,S});
while(!q.empty())
{
PII now=q.top();q.pop();
if(-now.first!=dis[now.second])continue;
if(now.second==T)break;
for(int i=head[now.second];i!=-;i=edge[i].nxt)
{
int nowcost=edge[i].w+h[now.second]-h[edge[i].v];
if(edge[i].f>&&dis[edge[i].v]>dis[now.second]+nowcost)
{
dis[edge[i].v]=dis[now.second]+nowcost;
q.push({-dis[edge[i].v],edge[i].v});
PrePoint[edge[i].v]=now.second;
PreEdge[edge[i].v]=i;
}
}
}
if(dis[T]==inf)break;
for(int i=;i<=n;++i)h[i]+=dis[i];
int nowflow=inf;
for(int i=T;i!=S;i=PrePoint[i])
nowflow=min(nowflow,edge[PreEdge[i]].f);
for(int i=T;i!=S;i=PrePoint[i])
{
edge[PreEdge[i]].f-=nowflow;
edge[PreEdge[i]^].f+=nowflow;
}
max_flow+=nowflow;
min_cost+=nowflow*h[T];
}
return {max_flow,min_cost};
}
}G; char mp[][];
int id[][]; int main()
{
int h,l;
while(sc("%d%d",&h,&l),h&&l)
{
S=h*l+,T=h*l+;
int cnt=;
fo(i,,h)
fo(j,,l)
sc("%1s",&mp[i][j]),id[i][j]=++cnt;
n=h*l+;
G.Init(n);
for(int i=;i<=h;++i)
{
for(int j=;j<=l;++j)
{
if(i>)G.Add(id[i][j],id[i-][j],inf,);
if(j>)G.Add(id[i][j],id[i][j-],inf,);
if(i<h)G.Add(id[i][j],id[i+][j],inf,);
if(j<l)G.Add(id[i][j],id[i][j+],inf,);
if(mp[i][j]=='m')G.Add(S,id[i][j],,);
if(mp[i][j]=='H')G.Add(id[i][j],T,,);
}
}
PII ans=G.Dij();
pr("%d\n",ans.second);
}
return ;
} /**************************************************************************************/ int maxx(int a,int b)
{
return a>b?a:b;
} void swapp(int &a,int &b)
{
a^=b^=a^=b;
} int lowbit(int n)
{
return n&(-n);
} int Del_bit_1(int n)
{
return n&(n-);
} int abss(int a)
{
return a>?a:-a;
} double fabss(double a)
{
return a>?a:-a;
} int minn(int a,int b)
{
return a<b?a:b;
}

把人都送到房子里的最小花费--最小费用最大流MCMF的更多相关文章

  1. 今盒子里有n个小球,A、B两人轮流从盒中取球,每个人都可以看到另一个人取了多少个

    /* 题目描述 今盒子里有n个小球,A.B两人轮流从盒中取球,每个人都可以看到另一个人取了多少个,也可以看到盒中还剩下多少个,并且两人都很聪明,不会做出错误的判断. 我们约定: 每个人从盒子中取出的球 ...

  2. 99%的人都理解错了HTTP中GET与POST的区别(转载

    GET和POST是HTTP请求的两种基本方法,要说它们的区别,接触过WEB开发的人都能说出一二. 最直观的区别就是GET把参数包含在URL中,POST通过request body传递参数. 你可能自己 ...

  3. 99%的人都理解错了HTTP中GET与POST的区别

    GET和POST是HTTP请求的两种基本方法,要说它们的区别,接触过WEB开发的人都能说出一二. 最直观的区别就是GET把参数包含在URL中,POST通过request body传递参数. 你可能自己 ...

  4. 99%的人都理解错了HTTP中GET与POST的区别(转自知乎)

    作者:Larry链接:https://zhuanlan.zhihu.com/p/22536382来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. GET和POST是HTT ...

  5. 转:99%的人都理解错了HTTP中GET与POST的区别

    原贴来自:http://www.techweb.com.cn/network/system/2016-10-11/2407736.shtml GET和POST是HTTP请求的两种基本方法,要说它们的区 ...

  6. 99%的人都理解错了GET与POST的区别

    原文链接:https://mp.weixin.qq.com/s?__biz=MzI3NzIzMzg3Mw==&mid=100000054&idx=1&sn=71f6c214f3 ...

  7. 【转载】99%的人都理解错了HTTP中GET与POST的区别

    作者:Larry链接:https://zhuanlan.zhihu.com/p/22536382来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 原文链接 作者:WebT ...

  8. 上传伪技术~很多人都以为判断了后缀,判断了ContentType,判断了头文件就真的安全了。是吗?

    今天群里有人聊图片上传,简单说下自己的经验(大牛勿喷) 0.如果你的方法里面是有指定路径的,记得一定要过滤../,比如你把 aa文件夹设置了权限,一些类似于exe,asp,php之类的文件不能执行,那 ...

  9. 房子里的K2 BPM业务流程管理

    房…子这件事上,尴尬实在太多. ಥ_ಥ 职场新人,租房很尴尬: 未婚男女,婚房很尴尬: 有下一代的,学区房很尴尬: 耄耋之年,养老房很尴尬... ▽ 甭管买房.租房.装修.设计,关于房子的尴尬事,三天 ...

随机推荐

  1. 图解Linux进程间通信实现原理(1)

    为Linux应用程序的开发人员,对Linux的进程间通信方式肯定是了如指掌,平时的开发中应该会大量的使用到.当你迅速的在键盘上按下[CTRL+C]终止掉一个正在运行中的命令时,你有没有仔细的思考过背后 ...

  2. IntelliJ跳转到抽象方法的实现

    ctrl + b (等价于ctrl + 鼠标点击方法名)会调到这个类型的抽象方法中: 如果想要跳转到这个方法的具体实现可以使用 ctrl + alt + 鼠标点击方法名. IntelliJ快速查找一个 ...

  3. win10+mysql8.0安装

    一.下载 mysql8.0 windows zip包下载地址: https://dev.mysql.com/downloads/mysql/   1540951981(1).png 二.安装 1.解压 ...

  4. SSH交互式脚本StrictHostKeyChecking选项 benchmode=yes

    SSH 公钥检查是一个重要的安全机制,可以防范中间人劫持等黑客攻击.但是在特定情况下,严格的 SSH 公钥检查会破坏一些依赖 SSH 协议的自动化任务,就需要一种手段能够绕过 SSH 的公钥检查. 什 ...

  5. Linux使用iptables设置黑白名单

    使用ipset工具 1,下面我先说下iptables的基本配置规则,然后再说ipset以下使用C7 x86_64为实验环境CentOS7默认的防火墙不是iptables,而是firewalle.如果你 ...

  6. mysql自身报错、java、reids连接mysql数据库报错汇总

    1.Can't connect to local MySQL server through socket 'tmpmysql.sock' (2) 原因是mysql根本没有启动 2.Access den ...

  7. lnmp搭建-手动版

    参照链接:http://blog.csdn.net/bravemelon/article/details/64925499 centOS 下安装 Nginx yum -y groupinstall & ...

  8. CSV文件规范及其Java解析方式

    Comma-separated values(CSV)文件规范见WIKIPEDIA:https://en.wikipedia.org/wiki/Comma-separated_values#RFC_4 ...

  9. iOS-SVPullToRefresh​下拉刷新,上拉加载(转)

    https://github.com/Sephiroth87/ODRefreshControl 类似刷新控件,类似qq动画的那种刷新. 一.下载第三方库 https://github.com/samv ...

  10. swift 第十一课 结构体定义model类

    结构体是可以作为 model 类使用的不过也要 写下的创建方法 import UIKit/***创建一个model 结构,重写init 方法,结构体的属性不能出现可选类型**/ struct Mode ...