POJ2195(最小费用最大流)
来源:互联网 发布:qq飞车mac版下载 编辑:程序博客网 时间:2024/06/04 18:47
Going Home
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 23761 Accepted: 11948
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.
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
Pacific Northwest 2004
解题思路:费用流模板题,建图方式为超级源点到每一个房子连边,流量为1,费用为0,每个房子到每个人连边,流量为1,费用为这个的曼哈顿距离, 然后每个人到超级汇点连边,流量为1,费用为0,然后求一个源点到汇点的最小费用最大流就行。
#include <stdio.h>#include <algorithm>#include <vector>#include <queue>#include <iostream>#include <cstring>using namespace std;int inf = 0x3f3f3f3f;const int maxm = (10000<<1);const int maxn = 1000;int N, M;char Map[105][105];int head[maxn];int Maxflow;int tot;int num1, num2;//人和房子的数量int src, sink;int dist[maxn];bool inQue[maxn];int prev[maxn], pree[maxn];vector<pair<int, int > > human;vector<pair<int, int> > house;struct edge{ int v, w, f, nxt;}Edge[maxm];void init(){ num1 = num2 = tot = 0; Maxflow = 0; memset(head, -1, sizeof(head)); human.clear(); house.clear();}void add(int u, int v, int w, int f){ Edge[tot].v = v; Edge[tot].w = w; Edge[tot].f = f; Edge[tot].nxt = head[u]; head[u] = tot++; Edge[tot].v = u; Edge[tot].w = -w; Edge[tot].f = 0; Edge[tot].nxt = head[v]; head[v] = tot++;}bool SPFA()//从源点到汇点进行最短路的增广{ memset(inQue, false, sizeof(inQue)); memset(dist, inf, sizeof(dist)); queue<int> que; while(!que.empty()) que.pop(); que.push(src); inQue[src] = true; dist[src] = 0; while(!que.empty()) { int u = que.front(); que.pop(); for(int i = head[u]; i != -1; i = Edge[i].nxt) { int v = Edge[i].v; int w = Edge[i].w; int f = Edge[i].f; if(f > 0 && dist[u] + w < dist[v]) { dist[v] = dist[u] + w; prev[v] = u; pree[v] = i; if(!inQue[v]) { inQue[v] = true; que.push(v); } } } inQue[u] = false; } if(dist[sink] < inf) return true; else return false;}int mincostflow(){ int ans = 0; while(SPFA()) { int u = sink; int delta = inf; while(u != src) { int f = Edge[pree[u]].f; if(f < delta) delta = f; u = prev[u]; } u = sink; while(u != src) { Edge[pree[u]].f -= delta; Edge[pree[u]^1].f += delta; u = prev[u]; } Maxflow += delta; ans += delta * dist[sink]; } return ans;}int main(){ while(~scanf("%d%d", &N, &M) && N && M) { for(int i = 1; i <= N; i++) { scanf("%s", Map[i] + 1); } init(); for(int i = 1; i <= N; i++) { for(int j = 1; j <= M; j++) { if(Map[i][j] == 'm') { human.push_back(make_pair(i, j)); } else if(Map[i][j] == 'H') { house.push_back(make_pair(i, j)); } } } num1 = human.size(); num2 = house.size(); src = 0; sink = num1 + num2 + 1; for(int i = 0; i < num1; i++) { add(src, i + 1, 0, 1); } for(int i = 0; i < num2; i++) { add(num1 + 1 + i, sink, 0, 1); } for(int i = 0; i < num1; i++) { for(int j = 0; j < num2; j++) { pair<int, int> p1 = human[i]; pair<int, int> p2 = house[j]; int dis = abs(p1.first - p2.first) + abs(p1.second - p2.second); add(i + 1, num1 + j + 1, dis, 1); } } printf("%d\n", mincostflow()); } return 0;}
阅读全文
0 0
- 最小费用最大流--poj2195
- POJ2195(最小费用最大流)
- POJ2195 Going Home 最小费用最大流
- poj2195 Going Home,最小费用最大流
- poj2195--Going Home(最小费用最大流)
- poj2195 Going Home 最小费用最大流
- poj2195 spfa 最小费用最大流
- poj2195 -最小费用流
- POJ2195 Going Home(SPFA最小费用最大流)
- poj2195(二分图最大匹配,最小费用流)
- poj2195 Going Home(最大费用最小流)
- POJ2195 Going Home(最小费用最大流mcmf)
- POJ2195-Going Home(最小费用最大流)
- poj2195——Going Home(最小费用最大流)
- 【POJ2195】Going Home-最小费用最大流模板题
- poj2195 going home最小费用流
- POJ2195-Going Home(最小费用流)
- vector 的注意事项 (poj2195 小人进房子 最小费用最大流)
- 关于genymotion的问题
- 手把手教你Adobe Photoshop Lightroom CC 2015中文版的官方下载、安装与注册激活教程
- kubelet之DockerClient源码分析
- PCM和I2S区别
- java堆内存与栈内存
- POJ2195(最小费用最大流)
- 【转载】消息模式Toast.makeText的几种常见用法
- I2C接口与SPI和UART接口的区别
- 15、C#里面的for循环结构的使用
- 【BZOJ1798】【AHOI2009】维护序列
- 求正整数2和n之间的完全数
- 10月3日 c语言简单循环结构 求1+2+3+……+100=?
- noip模板(不定时更新)
- QT中使用数据库(SQLite)