最小费用最大流-POJ-2195-Going Home
2016-05-17 12:26
501 查看
Going Home
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 21046 Accepted: 10629
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
题意:
给一张N*M的方格图,其中H表示房子,m表示人,现在这m个人要到房子中去,每个房子只能容纳一个人,每个格子都非常的大,以至于所有人都能同时经过一个格子。每个人可以移动到相邻的一格,每次移动花费1刀。
现在问能够让所有人回到房子中所要花的最少钱数。
题解:
因为题中说了格子够大,人与人之间移动是不影响的,所以两点间的距离就是abs(x1-x2)+abs(y1-y2)。
那么用费用流,从源点引上界为1的边到每个人,从房子引上界为1的边到汇点,cost都为0。而后跑出每个人到每间房之间的距离,从人到房引一条上界为1,cost为距离的边。
然后跑一次最小费用最大流就可以了。
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 21046 Accepted: 10629
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
题意:
给一张N*M的方格图,其中H表示房子,m表示人,现在这m个人要到房子中去,每个房子只能容纳一个人,每个格子都非常的大,以至于所有人都能同时经过一个格子。每个人可以移动到相邻的一格,每次移动花费1刀。
现在问能够让所有人回到房子中所要花的最少钱数。
题解:
因为题中说了格子够大,人与人之间移动是不影响的,所以两点间的距离就是abs(x1-x2)+abs(y1-y2)。
那么用费用流,从源点引上界为1的边到每个人,从房子引上界为1的边到汇点,cost都为0。而后跑出每个人到每间房之间的距离,从人到房引一条上界为1,cost为距离的边。
然后跑一次最小费用最大流就可以了。
#include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <cmath> #include <algorithm> #include <vector> #include <map> #include <queue> #include <set> #include <string> using namespace std; int n,m,num1,num2; char maze[105][105]; int x[2][105],y[2][105]; const int MAXN = 10000; const int MAXM = 100000; const int INF = 0x3f3f3f3f; struct Edge { int to,next,cap,flow,cost; } edge[MAXM]; int head[MAXN],tol; int pre[MAXN],dis[MAXN]; bool vis[MAXN]; int N;//节点总个数,节点编号从0~N-1 void init(int n) { N = n; tol = 0; memset(head,-1,sizeof(head)); } void addedge(int u,int v,int cap,int cost) { edge[tol].to = v; edge[tol].cap = cap; edge[tol].cost = cost; edge[tol].flow = 0; edge[tol].next = head[u]; head[u] = tol++; edge[tol].to = u; edge[tol].cap = 0; edge[tol].cost = -cost; edge[tol].flow = 0; edge[tol].next = head[v]; head[v] = tol++; } bool spfa(int s,int t) { queue<int>q; for(int i = 0; i < N; i++) { dis[i] = INF; vis[i] = false; pre[i] = -1; } dis[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) { int v = edge[i].to; if(edge[i].cap > edge[i].flow && dis[v] > dis[u] + edge[i].cost ) { dis[v] = dis[u] + edge[i].cost; pre[v] = i; if(!vis[v]) { vis[v] = true; q.push(v); } } } } if(pre[t] == -1)return false; else return true; } //返回的是最大流,cost存的是最小费用 int minCostMaxflow(int s,int t,int &cost) { int flow = 0; cost = 0; while(spfa(s,t)) { int Min = INF; for(int i = pre[t]; i != -1; i = pre[edge[i^1].to]) { if(Min > edge[i].cap - edge[i].flow) Min = edge[i].cap - edge[i].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; } return flow; } inline int dist(int a,int b) { return abs(x[0][a]-x[1][b])+abs(y[0][a]-y[1][b]); } int main() { while(cin >> n >> m && n!=0 && m!=0) { int out=0; num1=num2=0; for(int i=0; i<n; i++) for(int j=0; j<m; j++) { cin >> maze[i][j]; if(maze[i][j]=='m') { num1++; x[0][num1]=i; y[0][num1]=j; } if(maze[i][j]=='H') { num2++; x[1][num2]=i; y[1][num2]=j; } } init(num1+num2+2); for(int i=1;i<=num1;i++) addedge(0,i,1,0); for(int i=num1+1;i<=num1+num2;i++) addedge(i,num1+num2+1,1,0); for(int i=1;i<=num1;i++) for(int j=num1+1;j<=num1+num2;j++) addedge(i,j,1,dist(i,j-num1)); minCostMaxflow(0,num1+num2+1,out); cout << out << endl; } return 0; }
相关文章推荐
- Django开发系列之二
- 初建django博客之二
- Django笔记 数据备份和恢复
- 初建Django博客之一
- Google:坚决杀死Flash
- ACM之搜索-DFS-cugoj-1149
- 啦啦啦啦~Django1.96---编写第一个Django应用(1)
- go template 循环中使用 外部变量
- 《算法》第四版《Algorithms》4th Edition 学习环境搭建
- 啦啦啦啦~Django1.96---字段查询
- ROS(indigo)_pr2_simulator仿真(gazebo)示例
- ROS(indigo)_pr2_simulator仿真(gazebo)示例
- ROS(indigo)_pr2_simulator仿真(gazebo)示例
- goahead 的跳转问题
- genymotion登陆出现unknown genymotion error;启动出现got no ip address, unable to load virtual engin错误的解决办法
- Django的Model上都有些什么
- Google学术上不去,几个替代网址
- Mongoose
- Google Chrome以root身份启动
- Google Guava官方教程(中文版)