题目链接: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. Swift实战-豆瓣电台(三)获取网络数据

    观看地址:http://v.youku.com/v_show/id_XNzMwMzQxMzky.html 这节内容,我们先说了怎么将storyboard中的组件在类中进行绑定.然后写了一个类用来获取网 ...

  2. Servlet 基础认识 使用

    参考书<JSP Web 开发案例教程> 在jsp项目中的src文件下面创建HelloServletTest.java,代码如下,注意src的包名,我的包名是zhbit.com packag ...

  3. [转] asp.net <%%>&<%#%>&<%=%>&<%@%>&<%$%>用法区别

    转自  参考 1.<% %>用来绑定后台代码 如: < % for(int i=0;i<100;i++) { Reaponse.Write(i.ToString()); } % ...

  4. 代码实现sql数据库的附加(通常在安装的时候)

    判断数据库是否已经存在 SqlConnection judgeConn = new SqlConnection("server=.;database=master;uid="+us ...

  5. 动画--过渡属性 transition-property

    早期在Web中要实现动画效果,都是依赖于JavaScript或Flash来完成.但在CSS3中新增加了一个新的模块transition,它可以通过一些简单的CSS事件来触发元素的外观变化,让效果显得更 ...

  6. android xutils

    http://blog.csdn.net/rishengcsdn/article/details/47279851/

  7. android EditText长按屏蔽ActionMode context菜单但保留选择工具功能

    最近项目要求屏蔽EditText 长按出来的ActionMode菜单,但是要保留选择文本功能.这个屏蔽百度会出现各种方法,这里说一下我的思路: 1.屏蔽百度可知setCustomSelectionAc ...

  8. 13---Net基础加强

    更新中,敬请期待............ 作业-复习 程序集1 程序集2 反射1 反射2 反射3 反射4-记事本插件1 反射5-记事本插件2

  9. Install the 64bit library in Ubuntu13.10

    After installed Ubuntu13.10, and i want to run a 32bit software, in the pass, you just run sudo apt- ...

  10. UBoot讲解和实践-----------讲解(一)

    Boot Loader之ARM Uboot移植 阶段一 理论篇 1.boot loader需要完成的任务:               1>设计程序的入口地址               2&g ...