POJ 2195 D - Going Home 费用流
2015-08-16 19:07
645 查看
[b]D - Going Home[/b]
Time Limit: 20 Sec
Memory Limit: 256 MB
题目连接
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=88038#problem/D
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.
![](http://7xjob4.com1.z0.glb.clouddn.com/29da4c22cd9a35eb96b2a08b75ce8420)
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
X D
Sample Output
2
10
28
HINT
题意
有一个图,图里面h表示人,m表示房间,每一个人需要滚回一个房间,每个人走一步需要1的花费
问你最小花费,使得每个人都走到不同的房间
题解:
偶的代码是抄的,所以直接看这儿吧:http://blog.csdn.net/u012860063/article/details/41792973
代码:
Time Limit: 20 Sec
Memory Limit: 256 MB
题目连接
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=88038#problem/D
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
X D
Sample Output
2
10
28
HINT
题意
有一个图,图里面h表示人,m表示房间,每一个人需要滚回一个房间,每个人走一步需要1的花费
问你最小花费,使得每个人都走到不同的房间
题解:
偶的代码是抄的,所以直接看这儿吧:http://blog.csdn.net/u012860063/article/details/41792973
代码:
#include <cstdio> #include <cstring> #include <algorithm> #include <queue> #include <cmath> using namespace std; const int MAXN = 10000; const int MAXM = 100000; const int INF = 0x3f3f3f3f; struct Edge { int to, next, cap, flow, cost; int x, y; } edge[MAXM],HH[MAXN],MM[MAXN]; int head[MAXN],tol; int pre[MAXN],dis[MAXN]; bool vis[MAXN]; int N, M; char map[MAXN][MAXN]; void init() { N = MAXN; 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; } int main() { int n, m; while(~scanf("%d%d",&n,&m)) { if(n==0 && m==0) break; int ch = 0, cm = 0; init();//注意 for(int i = 0; i < n; i++) { scanf("%s",map[i]); for(int j = 0; j < m; j++) { if(map[i][j]=='H') { HH[ch].x = i; HH[ch++].y = j; } else if(map[i][j]=='m') { MM[cm].x = i; MM[cm++].y = j; } } } //printf("ch:%d cm:%d\n",ch,cm); int beg = 0;//超级起点 int end = 2*ch+1;//超级汇点 for(int i = 0; i < cm; i++) { addedge(beg,i+1,1,0);//超级起点,容量为1,花费为0 for(int j = 0; j < ch; j++) { int tt = abs(HH[i].x-MM[j].x)+abs(HH[i].y-MM[j].y); //printf("tt:%d\n",tt); addedge(i+1,j+1+ch,1,tt); } addedge(i+1+ch,end,1,0);//超级汇点容量为1,花费为0 } int ans = 0; minCostMaxflow(beg,end,ans); printf("%d\n",ans); } return 0; }
相关文章推荐
- GoAgent下local文件下proxy.ini文件各项参数介绍
- 如何直接从 Google Play 下载 APK 文件?
- 如何用Go语言打造一个高性能MySQL Proxy
- [Machine Learning & Algorithm] 决策树与迭代决策树(GBDT)
- 如何用Go语言打造一个高性能MySQL Proxy
- Google APAC 2016 University Graduates Test Practice Round APAC test
- django-rest-framework指南(3):Class Based Views(类视图)
- Google APAC 2016 University Graduates Test(google校招笔试)
- Google笔试(2015年8月)
- Codeforces Round #313 (Div. 2)-Gerald's Hexagon 找规律-几何规律-
- HDU_1847 Good Luck in CET-4 Everybody!(SG函数)
- hdu 3699 Aragorn's Story(树链剖分)
- GoldenGate 使用reperror进行错误处理
- Leetcode问题-1(Two Sum)
- win10 google浏览器设置
- poj 3107 Godfather 求树的所有重心 树形DP
- golang byte转string 字节数组转字符串的问题
- 欧几里得 & 拓展欧几里得算法 讲解 (Euclid & Extend- Euclid Algorithm)
- gem install mongo
- Gym - 100203G Good elements 水+模拟