题目链接:http://poj.org/problem?id=2195

Going Home
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 21530   Accepted: 10871

Description

On a grid map there are n little men and n houses. In each unit time, every little man can move one unit step, either horizontally, or vertically, to an adjacent point. For each little man, you need to pay a $1 travel fee for every step he moves, until he enters a house. The task is complicated with the restriction that each house can accommodate only one little man.

Your task is to compute the minimum amount of money you need to pay in order to send these n little men into those n different houses. The input is a map of the scenario, a '.' means an empty space, an 'H' represents a house on that point, and am 'm' indicates there is a little man on that point. 

You can think of each point on the grid map as a quite large square, so it can hold n little men at the same time; also, it is okay if a little man steps on a grid with a house without entering that house.

Input

There are one or more test cases in the input. Each case starts with a line giving two integers N and M, where N is the number of rows of the map, and M is the number of columns. The rest of the input will be N lines describing the map. You may assume both N and M are between 2 and 100, inclusive. There will be the same number of 'H's and 'm's on the map; and there will be at most 100 houses. Input will terminate with 0 0 for N and M.

Output

For each test case, output one line with the single integer, which is the minimum amount, in dollars, you need to pay.

Sample Input

2 2
.m
H.
5 5
HH..m
.....
.....
.....
mm..H
7 8
...H....
...H....
...H....
mmmHmmmm
...H....
...H....
...H....
0 0

Sample Output

2
10
28

Source

 
听阳哥说,可以用二分图得最大匹配就可以出来,还没学,用的是书上的SPFA,然后改了半天没改好。2333
思路:构造一个源点,一个汇点01,下面的点是2,3... ... 把每条路的花费作为最短路,用SPFA,找到一条最短路,更新残余网络。
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <queue>
#include <algorithm> using namespace std; #define MAXN 205
#define INF 20000 bool vis[MAXN];
int cnt, cnt_h, cnt_m, result;
int d[MAXN], pre[MAXN], cost[MAXN][MAXN], cap[MAXN][MAXN]; struct node
{
int x, y;
} hos[MAXN], man[MAXN]; int step(int i, int j)
{
return (int)fabs((man[i].x-hos[j].x)*1.0) + fabs((man[i].y-hos[j].y)*1.0);
}
void make_map()
{
int i, j;
memset(cap, , sizeof(cap));
for (i = ; i < cnt_m; i++)
{
cap[][i+] = ;
cost[][i+] = ;
}
for (i = ; i < cnt_h; i++)
{
cap[cnt_m+i+][] = ;
cost[cnt_m+i+][] = ;
}
for (i = ; i < cnt_m; i++)
for (j = ; j < cnt_h; j++)
{
cap[i+][cnt_m+j+] = ;
cost[i+][cnt_m+j+] = step(i, j);
cost[cnt_m+j+][i+] = -cost[i+][cnt_m+j+];
}
} bool spfa()
{
int i, u;
for (i = ; i <= cnt; i++)
{
d[i] = INF;
vis[i] = false;
}
d[] = ;
queue <int> q;
q.push();
while (!q.empty())
{
u = q.front();
q.pop();
vis[u] = true;
for (i = ; i <= cnt; i++)
if (cap[u][i] && d[i] > d[u] + cost[u][i])
{
d[i] = d[u] + cost[u][i];
pre[i] = u;
if (!vis[i])
{
vis[i] = true;
q.push(i);
}
}
vis[u] = false;
}
if (d[] < INF)
return true;
return false;
} int main()
{
char c;
int i, j, n, m;
while (scanf("%d%d", &n, &m), n && m)
{
cnt_h = cnt_m = ;
for (i = ; i < n; i++)
for (j = ; j < m; j++)
{
scanf(" %c", &c);
if (c == 'H')
{
hos[cnt_h].x = i;
hos[cnt_h].y = j;
cnt_h++;
}
else if (c == 'm')
{
man[cnt_m].x = i;
man[cnt_m].y = j;
cnt_m++;
}
}
cnt = cnt_h + cnt_m + ;
make_map();
result = ;
while (spfa())
{
int i, cf;
cf = INF;
for (i = ; i != ; i = pre[i])
cf = min(cf, cap[pre[i]][i]);
for (i = ; i != ; i = pre[i])
{
cap[pre[i]][i] -= cf;
cap[i][pre[i]] += cf;
result += cost[pre[i]][i] * cf;
}
}
printf("%d\n", result);
}
return ;
}
 

Poj(2195),最小费用流,SPFA的更多相关文章

  1. POJ 2195 Going Home 最小费用最大流 尼玛,心累

    D - Going Home Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Subm ...

  2. poj 2195 二分图带权匹配+最小费用最大流

    题意:有一个矩阵,某些格有人,某些格有房子,每个人可以上下左右移动,问给每个人进一个房子,所有人需要走的距离之和最小是多少. 貌似以前见过很多这样类似的题,都不会,现在知道是用KM算法做了 KM算法目 ...

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

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

  4. POJ 2195 Going Home (带权二分图匹配)

    POJ 2195 Going Home (带权二分图匹配) Description On a grid map there are n little men and n houses. In each ...

  5. poj 2195 Going Home(最小费最大流)

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

  6. 【POJ 2195】 Going Home(KM算法求最小权匹配)

    [POJ 2195] Going Home(KM算法求最小权匹配) Going Home Time Limit: 1000MS   Memory Limit: 65536K Total Submiss ...

  7. poj 2195 Going Home(最小费用流)

    题目链接:http://poj.org/problem?id=2195 题目大意是给一张网格,网格中m代表人,h代表房子,网格中的房子和人数量相等,人可以向上向下走,每走1步花费加1,每个房子只能住一 ...

  8. POJ 2195 Going Home 最小费用流 裸题

    给出一个n*m的图,其中m是人,H是房子,.是空地,满足人的个数等于房子数. 现在让每个人都选择一个房子住,每个人只能住一间,每一间只能住一个人. 每个人可以向4个方向移动,每移动一步需要1$,问所有 ...

  9. POJ 2195 Going Home【最小费用流 二分图最优匹配】

    题目大意:一个n*m的地图,上面有一些人man(m)和数量相等的house(H) 图上的距离为曼哈顿距离 问所有人住进一所房子(当然一个人住一间咯)距离之和最短是多少? 思路:一个人一间房,明显是二分 ...

随机推荐

  1. jquery获取自身元素的html

    在开发过程中,jQuery.html() 是获取当前节点下的html代码,并不包含当前节点本身的代码,然而我们有时候的确需要,可以通过jQuery.prop("outerHTML" ...

  2. [原创]java WEB学习笔记90:Hibernate学习之路-- -HQL检索方式,分页查询,命名查询语句,投影查询,报表查询

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  3. Java基础(39):数据的四舍五入、去整、产生随机数---Math类的应用

    使用 Math 类操作数据 Math 类位于 java.lang 包中,包含用于执行基本数学运算的方法, Math 类的所有方法都是静态方法,所以使用该类中的方法时,可以直接使用类名.方法名,如: M ...

  4. linux centos5.7(32bit) oracle 10g oracle11g

    cenOS5.5安装oracle10g(傻瓜篇) http://www.cnblogs.com/fnng/archive/2012/06/19/2554159.html  (转) 在cenOS5.5上 ...

  5. oracle的簇的创建

    簇其实就是一组表,由一组共享相同数据块的多个表组成,将经常一起使用的表组合在一起成簇可以提高处理效率:在一个簇中的表就叫做簇表. 建立顺序是:簇→簇表→簇索引→数据 创建簇的格式 CREATE CLU ...

  6. PAT乙级 1001. 害死人不偿命的(3n+1)猜想 (15)

    1001. 害死人不偿命的(3n+1)猜想 (15) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 卡拉兹(Ca ...

  7. 通过restore database时重命名数据库rename database

    backup database testdb to disk='c:\testdb_ful.bak' with compression backup log testdb to disk='c:\te ...

  8. zw版【转发·台湾nvp系列Delphi例程】HALCON FillUpShape2

    zw版[转发·台湾nvp系列Delphi例程]HALCON FillUpShape2 procedure TForm1.Button1Click(Sender: TObject);var op : H ...

  9. Eclipse启动认定SDK地址,出现Error executing aapt

    我把安卓的SDK安装在D盘,但是新建项目之后,提示的错误是 就是认定在F盘了,可是那个时候我只是在F盘启动了第一次,之后就拷贝到了D盘的, 是不是第一次启动就会注册了什么的呢? 解决方案: 更改你的e ...

  10. ubuntu屏幕分辨率问题

    今天在ubuntu下工作时突然屏幕上下各出现了一个大概2厘米的黑条,感觉屏幕被横向拉长了,莫名其妙,开始以为简单的调整下分辨率就好了,在系统设置显示里面发现分辨率只有两个可选参数,并且对象为未知,由于 ...