以前用KM写过,现在再用费用流写。

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
#include <utility>
#define abs(a) ((a)<0?-(a):(a))
#define maxn 210
#define oo 0x3f3f3f3f
using namespace std; struct Edge {
int u, v, c, f;
Edge( int u, int v, int c, int f ):u(u),v(v),c(c),f(f){}
};
struct Mcmf {
int n, src, dst;
vector<Edge> edge;
vector<int> g[maxn];
int dis[maxn], pth[maxn], ext[maxn]; void init( int n, int src, int dst ) {
this->n = n;
this->src = src;
this->dst = dst;
for( int i=; i<=n; i++ )
g[i].clear();
edge.clear();
}
void add_edge( int u, int v, int c, int f ) {
g[u].push_back( edge.size() );
edge.push_back( Edge(u,v,c,f) );
g[v].push_back( edge.size() );
edge.push_back( Edge(v,u,-c,) );
}
bool spfa( int &flow, int &cost ) {
queue<int> qu;
memset( dis, 0x3f, sizeof(dis) );
qu.push( src );
ext[src] = true;
dis[src] = ;
pth[src] = -;
while( !qu.empty() ) {
int u=qu.front();
qu.pop();
ext[u] = false;
for( int t=; t<g[u].size(); t++ ) {
Edge &e = edge[g[u][t]];
if( e.f && dis[e.v]>dis[e.u]+e.c ) {
dis[e.v] = dis[e.u]+e.c;
pth[e.v] = g[u][t];
if( !ext[e.v] ) {
ext[e.v] = true;
qu.push( e.v );
}
}
}
}
if( dis[dst]==oo ) return false;
int flw = oo;
for( int eid=pth[dst]; eid!=-; eid=pth[edge[eid].u] )
flw = min( flw, edge[eid].f );
for( int eid=pth[dst]; eid!=-; eid=pth[edge[eid].u] ) {
edge[eid].f -= flw;
edge[eid^].f += flw;
}
flow += flw;
cost += flw*dis[dst];
return true;
}
void mcmf( int &flow, int &cost ) {
flow = cost = ;
while( spfa(flow,cost) );
}
}; int n, m;
int aa[maxn][], bb[maxn][], ta, tb;
Mcmf M; int main() {
ios::sync_with_stdio( false );
while() {
cin>>n>>m;
if( n== && m== ) return ;
ta = tb = ;
for( int i=; i<=n; i++ )
for( int j=; j<=m; j++ ) {
char ch;
cin>>ch;
if( ch=='m' ) {
ta++;
aa[ta][] = i;
aa[ta][] = j;
} else if( ch=='H' ) {
tb++;
bb[tb][] = i;
bb[tb][] = j;
}
}
M.init( ta+tb+, ta+tb+, ta+tb+ );
for( int i=; i<=ta; i++ )
M.add_edge( M.src, i, , );
for( int j=ta+; j<=ta+tb; j++ )
M.add_edge( j, M.dst, , );
for( int i=; i<=ta; i++ )
for( int j=ta+; j<=ta+tb; j++ ) {
int dis = abs(aa[i][]-bb[j-ta][])+abs(aa[i][]-bb[j-ta][]);
M.add_edge( i, j, dis, );
}
int flow, cost;
M.mcmf( flow, cost );
cout<<cost<<endl;
}
}

hdu 1533 KM或费用流的更多相关文章

  1. HDU 3488--Tour(KM or 费用流)

    因为每个点只能经过一次 所以考虑拆点 这题有坑,有重边.. KM算法 把一个点拆成入点和出点 入点在X部,出点在Y步. 如果u,v之间有路径,就在X部的u点连接Y部的v点 求完美匹配. 当完美匹配的时 ...

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

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

  3. HDU 5988 Coding Contest(费用流+浮点数)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5988 题目大意: 给定n个点,m条有向边,每个点是一个吃饭的地方,每个人一盒饭.每个点有S个人,有B盒 ...

  4. POJ 2135 Farm Tour &amp;&amp; HDU 2686 Matrix &amp;&amp; HDU 3376 Matrix Again 费用流求来回最短路

    累了就要写题解,近期总是被虐到没脾气. 来回最短路问题貌似也能够用DP来搞.只是拿费用流还是非常方便的. 能够转化成求满流为2 的最小花费.一般做法为拆点,对于 i 拆为2*i 和 2*i+1.然后连 ...

  5. Going Home HDU - 1533(最大费用最小流)

    On a grid map there are n little men and n houses. In each unit time, every little man can move one ...

  6. HDU 1533 KM算法(权值最小的最佳匹配)

    Going Home Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  7. HDU 3315 My Brute(费用流)

    职务地址:HDU 3315 这个题的思路全然是自己想出来的,自我感觉挺巧妙的. . .(大牛勿喷.. . )对大胆建图又多了一份信心. 详细思路是构造一个二分图,Si连源点.Xi连汇点,流量都是1,费 ...

  8. HDU 2686 Matrix(最大费用流)

    Matrix Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Sub ...

  9. HDU 1533 & KM模板

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

随机推荐

  1. React Native DEMO for Android

    Demo1: 主要知识:navigator,fecth 地址:https://github.com/hongguangKim/ReactNativeDEMO1 Demo2: 主要知识:navigato ...

  2. Mysql中的primary key 与auto_increment

    mysql> create table cc(id int auto_increment); ERROR (): Incorrect table definition; there can be ...

  3. python基础之常用内置函数

    前言 python有许多内置的函数,它们定义在python的builtins模块,在python的代码中可以直接使用它们. 常用的内置函数 类型转换 int python的整数类型都是int类型的实例 ...

  4. 如何在Linux下用C/C++语言操作数据库sqlite3(很不错!设计编译链接等很多问题!)

    from : http://blog.chinaunix.NET/uid-21556133-id-118208.html 安装Sqlite3: 从www.sqlite.org上下载Sqlite3.2. ...

  5. static作用(修饰函数、局部变量、全局变量)转自http://www.cnblogs.com/stoneJin/archive/2011/09/21/2183313.html

    static作用(修饰函数.局部变量.全局变量) 在C语言中,static的字面意思很容易把我们导入歧途,其实它的作用有三条. (1)先来介绍它的第一条也是最重要的一条:隐藏. 当我们同时编译多个文件 ...

  6. 做Mysql主从时,注意使用replicate_wild_do_table和replicate-wild-ignore-table【转】

    做Mysql主从时,注意使用replicate_wild_do_table和replicate-wild-ignore-table 浓缩版: 使用replicate_do_db和replicate_i ...

  7. yum和head一起用,报错“由于管道被破坏而退出”

    当要打印 [yum list ]时, 加上了管道符 以及 head 会出现报错 “由于管道被破坏而退出” 是因为 yum 与 head 连用 存在bug ,如果使用tail 则没有出现 具体什么bug ...

  8. 初探Nginx架构

    参考链接:http://tengine.taobao.org/book/chapter_02.html nginx在启动后,在unix系统中会以daemon的方式在后台运行,后台进程包含一个maste ...

  9. 2017百度春招<不等式排列>

    题目: 度度熊最近对全排列特别感兴趣,对于1到n的一个排列,度度熊发现可以在中间根据大小关系插入合适的大于和小于符号(即 '>' 和 '<' )使其成为一个合法的不等式数列.但是现在度度熊 ...

  10. POJ 2349 Arctic Network(最小生成树+求第k大边)

    题目链接:http://poj.org/problem?id=2349 题目大意:有n个前哨,和s个卫星通讯装置,任何两个装了卫星通讯装置的前哨都可以通过卫星进行通信,而不管他们的位置. 否则,只有两 ...