Going Home

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 3443    Accepted Submission(s): 1763

Problem 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
 

第一道费用流。 O(∩_∩)O~~

题意:一个N*M地图上有同样数量的字符H和字符m。m代表一个 人,H代表一个房子。人到房子的花销是它们在图中的曼哈顿距离,问你让全部人回到房子所须要的最小费用(一个房子仅仅能容纳一个人)。

思路:设置超级源点source 连接全部字符H,容量为1。费用为0。每一个字符H向全部字符m连边,容量为1,费用为它们的曼哈顿距离。最后每一个字符m向超级汇点sink连边。容量为1,费用为0。

最后source到sink跑一遍最小费用最大流就ok了。

(我建边时把字符H看做人了,所以建的反向边,对这道题没有影响)

AC代码:

#include <cstdio>
#include <cstring>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#define MAXN 200+10
#define MAXM 80000+100
#define INF 0x3f3f3f3f
using namespace std;
struct Edge
{
int from, to, cap, flow, cost, next;
};
Edge edge[MAXM];
int head[MAXN], edgenum;
int pre[MAXN], dist[MAXN];
bool vis[MAXN];
int N, M;
int source, sink;//超级源点 超级汇点
void init()
{
edgenum = 0;
memset(head, -1, sizeof(head));
}
void addEdge(int u, int v, int w, int c)
{
Edge E1 = {u, v, w, 0, c, head[u]};
edge[edgenum] = E1;
head[u] = edgenum++;
Edge E2 = {v, u, 0, 0, -c, head[v]};
edge[edgenum] = E2;
head[v] = edgenum++;
}
int dis(int x1, int y1, int x2, int y2)
{
return abs(x1 - x2) + abs(y1 - y2);
}
struct Node
{
int x, y;
};
Node m[110], H[110];//存储字符坐标
int m_cnt;//m字符计数器
int H_cnt;//H字符计数器
void getMap()
{
m_cnt = H_cnt = 0;
char str[110][110];
for(int i = 0; i < N; i++)
{
scanf("%s", str[i]);
for(int j = 0; j < M; j++)
{
if(str[i][j] == 'm')
{
++m_cnt;
m[m_cnt].x = i;
m[m_cnt].y = j;
}
if(str[i][j] == 'H')
{
++H_cnt;
H[H_cnt].x = i;
H[H_cnt].y = j;
}
}
}
int k = m_cnt;//人数 或者 房子数
source = 0;
sink = 2*k+1;
for(int i = 1; i <= k; i++)
{
addEdge(source, i, 1, 0);
addEdge(i + k, sink, 1, 0);
for(int j = 1; j <= k; j++)
{
int d = dis(H[i].x, H[i].y, m[j].x, m[j].y);
addEdge(i, j + k, 1, d);
}
}
}
bool SPFA(int s, int t)//寻找花销最少的路径
{
queue<int> Q;
memset(dist, INF, sizeof(dist));
memset(vis, false, sizeof(vis));
memset(pre, -1, sizeof(pre));
dist[s] = 0;
vis[s] = true;
Q.push(s);
while(!Q.empty())
{
int u = Q.front();
Q.pop();
vis[u] = false;
for(int i = head[u]; i != -1; i = edge[i].next)
{
Edge E = edge[i];
if(dist[E.to] > dist[u] + E.cost && E.cap > E.flow)//能够松弛 且 没有满流
{
dist[E.to] = dist[u] + E.cost;
pre[E.to] = i;//记录前驱边 的编号
if(!vis[E.to])
{
vis[E.to] = true;
Q.push(E.to);
}
}
}
}
return pre[t] != -1;//可达返回true
}
void MCMF(int s, int t, int &cost, int &flow)
{
flow = 0;//总流量
cost = 0;//总费用
while(SPFA(s, t))//每次寻找花销最小的路径
{
int Min = INF;
//通过反向弧 在源点到汇点的最少花费路径 找最小增广流
for(int i = pre[t]; i != -1; i = pre[edge[i^1].to])
{
Edge E = edge[i];
Min = min(Min, E.cap - E.flow);
}
//增广
for(int i = pre[t]; i != -1; i = pre[edge[i^1].to])
{
edge[i].flow += Min;
edge[i^1].flow -= Min;
cost += edge[i].cost * Min;//增广流的花销
}
flow += Min;//流量累加
}
}
int main()
{
while(scanf("%d%d", &N, &M), N||M)
{
init();
getMap();
int cost, flow;
MCMF(source, sink, cost, flow);
printf("%d\n", cost);
}
return 0;
}

KM算法: 重刷

求最大流。用负权,最后结果取反。

#include <cstdio>
#include <cstring>
#include <algorithm>
#define INF 10000000
using namespace std;
int lx[110], ly[110];
int Map[110][110];
bool visx[110], visy[110];
int slack[110];
int match[110];
int nx, ny;
int N, M;
char str[110][110];
int dis(int x1, int y1, int x2, int y2)
{
return abs(x1 - x2) + abs(y1 - y2);
}
struct Node
{
int x, y;
};
Node m[110], H[110];//存储字符坐标
int m_cnt;//m字符计数器
int H_cnt;//H字符计数器
void getMap()
{
m_cnt = H_cnt = 0;
char str[110][110];
for(int i = 0; i < N; i++)
{
scanf("%s", str[i]);
for(int j = 0; j < M; j++)
{
if(str[i][j] == 'm')
{
++m_cnt;
m[m_cnt].x = i;
m[m_cnt].y = j;
}
if(str[i][j] == 'H')
{
++H_cnt;
H[H_cnt].x = i;
H[H_cnt].y = j;
}
}
}
int k = m_cnt;//人数 或者 房子数
nx = ny = k;
for(int i = 1; i <= k; i++)
{
for(int j = 1; j <= k; j++)
{
int d = dis(H[i].x, H[i].y, m[j].x, m[j].y);
Map[i][j] = -d;
}
}
}
int DFS(int x)
{
visx[x] = true;
for(int y = 1; y <= ny; y++)
{
if(visy[y]) continue;
int t = lx[x] + ly[y] - Map[x][y];
if(t == 0)
{
visy[y] = true;
if(match[y] == -1 || DFS(match[y]))
{
match[y] = x;
return 1;
}
}
else if(slack[y] > t)
slack[y] = t;
}
return 0;
}
void KM()
{
memset(match, -1, sizeof(match));
memset(ly, 0, sizeof(ly));
for(int x = 1; x <= nx; x++)
{
lx[x] = -INF;
for(int y = 1; y <= ny; y++)
lx[x] = max(lx[x], Map[x][y]);
}
for(int x = 1; x <= nx; x++)
{
for(int i = 1; i <= ny; i++)
slack[i] = INF;
while(1)
{
memset(visx, false, sizeof(visx));
memset(visy, false, sizeof(visy));
if(DFS(x)) break;
int d = INF;
for(int i = 1; i <= ny; i++)
{
if(!visy[i] && slack[i] < d)
d = slack[i];
}
for(int i = 1; i <= nx; i++)
{
if(visx[i])
lx[i] -= d;
}
for(int i = 1; i <= ny; i++)
{
if(visy[i])
ly[i] += d;
else
slack[i] -= d;
}
}
}
int ans = 0;
for(int i = 1; i <= ny; i++)
ans += Map[match[i]][i];
printf("%d\n", -ans);
}
int main()
{
while(scanf("%d%d", &N, &M), N||M)
{
getMap();
KM();
}
return 0;
}

hdoj 1533 Going Home 【最小费用最大流】【KM入门题】的更多相关文章

  1. hdu 1533 Going Home 最小费用最大流 (模板题)

    Going Home Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  2. hdu 1533 Going Home 最小费用最大流

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1533 On a grid map there are n little men and n house ...

  3. hdu 1533 Going Home 最小费用最大流 入门题

    Going Home Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tota ...

  4. POJ 2195 & HDU 1533 Going Home(最小费用最大流)

    这就是一道最小费用最大流问题 最大流就体现到每一个'm'都能找到一个'H',但是要在这个基础上面加一个费用,按照题意费用就是(横坐标之差的绝对值加上纵坐标之差的绝对值) 然后最小费用最大流模板就是再用 ...

  5. poj2135最小费用最大流经典模板题

    Farm Tour Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13509   Accepted: 5125 Descri ...

  6. hdoj 3488 Tour 【最小费用最大流】【KM算法】

    Tour Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Total Submi ...

  7. Minimum Cost(最小费用最大流,好题)

    Minimum Cost http://poj.org/problem?id=2516 Time Limit: 4000MS   Memory Limit: 65536K Total Submissi ...

  8. POJ--2516--Minimum Cost【最小费用最大流】

    链接:http://poj.org/problem?id=2516 题意:有k种货物,n个客户对每种货物有一定需求量,有m个仓库.每一个仓库里有一定数量的k种货物.然后k个n*m的矩阵,告诉从各个仓库 ...

  9. POJ 3422 Kaka's Matrix Travels 【最小费用最大流】

    题意: 卡卡有一个矩阵,从左上角走到右下角,卡卡每次只能向右或者向下.矩阵里边都是不超过1000的正整数,卡卡走过的元素会变成0,问卡卡可以走k次,问卡卡最多能积累多少和. 思路: 最小费用最大流的题 ...

  10. POJ2135 最小费用最大流模板题

    练练最小费用最大流 此外此题也是一经典图论题 题意:找出两条从s到t的不同的路径,距离最短. 要注意:这里是无向边,要变成两条有向边 #include <cstdio> #include ...

随机推荐

  1. STL || Gym 101653U Top 25

    一组字符串给出两种排列方式, 求最小分成多少组 如 A     A B     C C    D D    B E    E 则分成3组 A B C D E 即为1 3 1 #include < ...

  2. 最短路 || POJ 1511 Invitation Cards

    已知图中从一点到另一点的距离,从1号点到另一点再从这一点返回1号点,求去到所有点的距离之和最小值 *解法:正着反着分别建图,把到每个点的距离加起来 spfa跑完之后dist数组就是从起点到每一点的最短 ...

  3. P1357 花园 (矩阵快速幂+ DP)

    题意:一个只含字母C和P的环形串 求长度为n且每m个连续字符不含有超过k个C的方案数 m <= 5  n <= 1e15 题解:用一个m位二进制表示状态 转移很好想 但是这个题是用矩阵快速 ...

  4. 4.关于while循环的基础小练习

    1)使用while.if循环输入123456 8910 count = 0 while count < 10: count += 1 if count == 7: print('') else: ...

  5. ubuntu下操作Hadoop、hdfs、hbase、zookeeper时产生的一些问题及解决办法

    2019/05/29 1.在终端输入jps时,没有显示Hdfs的DataNode 在文件夹中分别找到DataNode 和Namenode的version,将Datanode的version改为与nam ...

  6. codevs 2853 方格游戏--棋盘dp

    方格游戏:http://codevs.cn/problem/2853/ 这和传纸条和noip方格取数这两个题有一定的相似性,当第一眼看到的时候我们就会想到设计$dp[i][j][k][l]$(i,j表 ...

  7. 万能的搜索--之DFS(二)

    (一)深度优先搜索(DFS) 我们先给出深度优先的解决办法,所谓深度优先搜索,在迷宫问题里就是不撞南墙不回头,能走得深一点就尽量深一点.如果碰到了墙壁就返回前一个位置尝试其他的方向.在<啊哈!算 ...

  8. Django中的Cookie、Session、Token

    Cookie : 指望着为了辨别用户身份.进行会话跟踪而存储在用户本地的数据(通常经过加密),是由服务端生成,发送给客户端浏览器,浏览器会将Cookie以key/value保存,下一请求同一网站是就发 ...

  9. Wall Treatment

    * wall treatment You can combine the turbulent flow interfaces with different types of wall treatmen ...

  10. zzuli 1905 小火山的跳子游戏

    Description   小火山和火山火山在一块玩跳子游戏.规则如下:   1:跳子的起始位置为0,棋盘大小从1到N   2:每次跳子跳k步. 例如当前位置为i, 那么下一步为i + k   3:跳 ...