博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Poj(2195),最小费用流,SPFA
阅读量:6975 次
发布时间:2019-06-27

本文共 3869 字,大约阅读时间需要 12 分钟。

题目链接:

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.mH.5 5HH..m...............mm..H7 8...H.......H.......H....mmmHmmmm...H.......H.......H....0 0

Sample Output

21028

Source

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

 

 

转载于:https://www.cnblogs.com/TreeDream/p/5755369.html

你可能感兴趣的文章
jquery的each()详细介绍
查看>>
Tablespace for table '`pomelo`.`bag`' exists. Please DISCARD the tablespace before IMPORT.
查看>>
virt-manager install on CentOS7-mini
查看>>
预攻击 局域网 Windows 查看其它在线设备
查看>>
SPOJ QTREE4 - Query on a tree IV
查看>>
Objective-C中的字符串格式化输出
查看>>
Android开发历程_6(RadioButton和CheckBox的使用)
查看>>
基础学习笔记之opencv(23):OpenCV坐标体系的初步认识
查看>>
POJ 3087 Shuffle'm Up【模拟/map/string】
查看>>
hdu 3786 寻找直系亲属
查看>>
模仿淘宝吸顶条(定时器)
查看>>
Project Euler 29 Distinct powers( 大整数质因数分解做法 + 普通做法 )
查看>>
Eclipse优化集合,Eclipse优化速度,解决Ctrl+C、Ctrl+V卡
查看>>
JavaScript中的this基本问题<转>
查看>>
杭电2040亲和数
查看>>
深入剖析Tomcat(How Tomcat Works)
查看>>
通过月份得到本月有几天周末
查看>>
在PHP语言中使用JSON和将json还原成数组
查看>>
博客园非官方月刊-2014年12月刊
查看>>
电商仓储控制超卖的策略
查看>>