把人都送到房子里的最小花费--最小费用最大流MCMF
题意: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的更多相关文章
- 今盒子里有n个小球,A、B两人轮流从盒中取球,每个人都可以看到另一个人取了多少个
/* 题目描述 今盒子里有n个小球,A.B两人轮流从盒中取球,每个人都可以看到另一个人取了多少个,也可以看到盒中还剩下多少个,并且两人都很聪明,不会做出错误的判断. 我们约定: 每个人从盒子中取出的球 ...
- 99%的人都理解错了HTTP中GET与POST的区别(转载
GET和POST是HTTP请求的两种基本方法,要说它们的区别,接触过WEB开发的人都能说出一二. 最直观的区别就是GET把参数包含在URL中,POST通过request body传递参数. 你可能自己 ...
- 99%的人都理解错了HTTP中GET与POST的区别
GET和POST是HTTP请求的两种基本方法,要说它们的区别,接触过WEB开发的人都能说出一二. 最直观的区别就是GET把参数包含在URL中,POST通过request body传递参数. 你可能自己 ...
- 99%的人都理解错了HTTP中GET与POST的区别(转自知乎)
作者:Larry链接:https://zhuanlan.zhihu.com/p/22536382来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. GET和POST是HTT ...
- 转:99%的人都理解错了HTTP中GET与POST的区别
原贴来自:http://www.techweb.com.cn/network/system/2016-10-11/2407736.shtml GET和POST是HTTP请求的两种基本方法,要说它们的区 ...
- 99%的人都理解错了GET与POST的区别
原文链接:https://mp.weixin.qq.com/s?__biz=MzI3NzIzMzg3Mw==&mid=100000054&idx=1&sn=71f6c214f3 ...
- 【转载】99%的人都理解错了HTTP中GET与POST的区别
作者:Larry链接:https://zhuanlan.zhihu.com/p/22536382来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 原文链接 作者:WebT ...
- 上传伪技术~很多人都以为判断了后缀,判断了ContentType,判断了头文件就真的安全了。是吗?
今天群里有人聊图片上传,简单说下自己的经验(大牛勿喷) 0.如果你的方法里面是有指定路径的,记得一定要过滤../,比如你把 aa文件夹设置了权限,一些类似于exe,asp,php之类的文件不能执行,那 ...
- 房子里的K2 BPM业务流程管理
房…子这件事上,尴尬实在太多. ಥ_ಥ 职场新人,租房很尴尬: 未婚男女,婚房很尴尬: 有下一代的,学区房很尴尬: 耄耋之年,养老房很尴尬... ▽ 甭管买房.租房.装修.设计,关于房子的尴尬事,三天 ...
随机推荐
- DUILib学习笔记---消息处理
WIN32下窗口消息循环 MSG msg = { 0 }; while( ::GetMessage(&msg, NULL, 0, 0) ) { ::TranslateMessage(& ...
- Smali基础知识
Smali是用于Dalvik(Android虚拟机)的反汇编程序实现 汇编工具(将Smali代码汇编为dex文件)为smali.jar baksmali.jar则是反汇编程序 地址:https://b ...
- 安装fastFDS
fastdfs源码包.libfastcommon源码包.nginx模块,我存放源码包于 /usr/local/src wget https://github.com/happyfish100/fast ...
- Java连接Memcached进行CRUD
参考这篇博文在本机安装了Memcached 在 Java 中常用的memcached有三个: Memcached Client for Java SpyMemcached XMemcached 这里使 ...
- hibernate-validator校验框架学习
1.引入jar包 <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate ...
- redus - 队列
redus 写如队列 <?php $redis = new \Redis(); $redis->connect('127.0.0.1',6379); $password = '123456 ...
- busybox中memdev的使用方法
busybox中已经集成了devmem工具,你可以配置busybox即可. 在busybox的杂项中找到: CONFIG_USER_BUSYBOX_DEVMEM: devmem is a small ...
- 设计模式----外观(facade)模式
外观(facade)模式外观模式(Facade),为子系统中的一组接口提供一个一致的界面,此模式定义了一个高层接口,这个接口使得这一子系统更加容易使用.uml图
- kotlin 之单表达式函数
fun d(x:Int):Int=x* 如果函数体只有一条语句,而且是return语句,那么可以省略大括号以及return关键字,
- 使用VLC发送TS流与播放TS流
使用VLC发送TS流与播放TS流 一.如何使用VLC发送TS流 1.添加一个文件至VLC 2.选择串流,继续 3.选择UDP,点击添加 4.输入地址及端口 5.选择h.264+mp3(TS) 6.ne ...