题目链接: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. codevs 1204 寻找子串位置

    http://codevs.cn/problem/1204/ 1204 寻找子串位置  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 青铜 Bronze 题解  查看运行结果 ...

  2. [原创]java WEB学习笔记83:Hibernate学习之路---双向 1-n介绍,关键点解释,代码实现,set属性介绍(inverse,cascade ,order-by )

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

  3. drop,delete,truncate区别

    drop,delete,truncate区别 drop-->删除少量信息   eg:drop table 表名: delete-->删除某些数据   eg:delete from 表名: ...

  4. 从一个小项目看return 引用 重载运算符

    #ifndef _ARRAY_H_ #define _ARRAY_H_ class Array { private: int mLength; int* mSpace; public: Array(i ...

  5. event对象的属性

    事件类型: bubbles:布尔值,表示事件是否通过DOM以冒泡形式触发. 事件发生时,反应当前环境信息的属性: button: 表示(如果有)鼠标所按下的按钮 ctrlKey: 布尔值,表示Ctrl ...

  6. SIFT算法详解(转)

    http://blog.csdn.net/zddblog/article/details/7521424 目录(?)[-] 尺度不变特征变换匹配算法详解 Scale Invariant Feature ...

  7. 【fedora】强制解除yum锁定

    运行yum makecache时出现yum update时候出现Another app is currently holding the yum lock解决方法yum被锁定了. 可以通过执行 rm ...

  8. andriod之应用内置浏览器 webview

    参考:http://my.eoe.cn/694183/archive/10476.html http://blog.csdn.net/it_ladeng/article/details/8136534 ...

  9. OpenStack 界面开发中response.body的中文编码问题

    Contents [hide] 1 问题的引入= 1.1 解决办法 2 用户限制输入中文 3 不限制用户输入,呈现上修改 问题的引入= G在我们创建虚拟机的时候,会设置虚拟机的名称,描述,如果没有限制 ...

  10. 如何在图像处理工具包ImagXpress中对图像进行捕捉、复制和粘贴

    如何在在ImagXpress中进行图像的捕捉. 复制和粘贴呢?下面详细来看一下,在多种情况下,图和实现这些操作. 捕捉屏幕图像 捕捉通过ImageXView窗口绑定的屏幕范围,以及保存到一个Image ...