Poj(2195),最小费用流,SPFA
题目链接:http://poj.org/problem?id=2195
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 21530 | Accepted: 10871 |
Description
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
Output
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
#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的更多相关文章
- POJ 2195 Going Home 最小费用最大流 尼玛,心累
D - Going Home Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Subm ...
- poj 2195 二分图带权匹配+最小费用最大流
题意:有一个矩阵,某些格有人,某些格有房子,每个人可以上下左右移动,问给每个人进一个房子,所有人需要走的距离之和最小是多少. 貌似以前见过很多这样类似的题,都不会,现在知道是用KM算法做了 KM算法目 ...
- POJ 2195 Going Home / HDU 1533(最小费用最大流模板)
题目大意: 有一个最大是100 * 100 的网格图,上面有 s 个 房子和人,人每移动一个格子花费1的代价,求最小代价让所有的人都进入一个房子.每个房子只能进入一个人. 算法讨论: 注意是KM 和 ...
- POJ 2195 Going Home (带权二分图匹配)
POJ 2195 Going Home (带权二分图匹配) Description On a grid map there are n little men and n houses. In each ...
- 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 ...
- 【POJ 2195】 Going Home(KM算法求最小权匹配)
[POJ 2195] Going Home(KM算法求最小权匹配) Going Home Time Limit: 1000MS Memory Limit: 65536K Total Submiss ...
- poj 2195 Going Home(最小费用流)
题目链接:http://poj.org/problem?id=2195 题目大意是给一张网格,网格中m代表人,h代表房子,网格中的房子和人数量相等,人可以向上向下走,每走1步花费加1,每个房子只能住一 ...
- POJ 2195 Going Home 最小费用流 裸题
给出一个n*m的图,其中m是人,H是房子,.是空地,满足人的个数等于房子数. 现在让每个人都选择一个房子住,每个人只能住一间,每一间只能住一个人. 每个人可以向4个方向移动,每移动一步需要1$,问所有 ...
- POJ 2195 Going Home【最小费用流 二分图最优匹配】
题目大意:一个n*m的地图,上面有一些人man(m)和数量相等的house(H) 图上的距离为曼哈顿距离 问所有人住进一所房子(当然一个人住一间咯)距离之和最短是多少? 思路:一个人一间房,明显是二分 ...
随机推荐
- php面向对象(OOP)编程完全教程
摘自:http://www.php-note.com/article/detail/41 面向对象编程(OOP)是我们编程的一项基本技能,PHP5对OOP提供了良好的支持.如何使用OOP的思想来进行P ...
- 超炫的3D翻转模板
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- csuoj 1335: 高桥和低桥
http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1335 1335: 高桥和低桥 Time Limit: 1 Sec Memory Limit: 1 ...
- c++中的传参问题
从概念上讲.指针从本质上讲就是存放变量地址的一个变量,在逻辑上是独立的,它可以被改变,包括其所指向的地址的改变和其指向的地址中所存放的数据的改变. 而引用是一个别名,它在逻辑上不是独立的,它的存在具有 ...
- PHP的几个常用加密函数(转载 https://jellybool.com/post/php-encrypt-functions)
PHP的几个常用加密函数 在网站的开发过程中,常常需要对部分数据(如用户密码)进行加密,本文主要介绍PHP的几个常见的加密函数 MD5加密: string md5 ( string $str [, b ...
- 夺命雷公狗jquery---4内容选择器
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- Axis2、Axis1 以及其他接口的调用方式
在请求的时候出现问题,使用下面的方式请求就不会出现问题. package webservice.client.utils; import java.util.Iterator; import java ...
- ThinkPHP讲解(二)控制器
在这一节,具体讲解控制器,以Jiaowu应用目录为例. 1.如何写控制器,如何写操作方法? 在模块控制器目录Controller下新建一个控制器文件MainController.class.php,写 ...
- ANT命令总结(转载)
1 Ant是什么? Apache Ant 是一个基于 Java的生成工具.生成工具在软件开发中用来将源代码和其他输入文件转换为可执行文件的形式(也有可能转换为可安装的产品映像形式).随着应用程序的生成 ...
- zw版【转发·台湾nvp系列Delphi例程】HALCON FillUp1
zw版[转发·台湾nvp系列Delphi例程]HALCON FillUp1 procedure TForm1.Button1Click(Sender: TObject);var img : HImag ...