POJ 2195 Going Home (最小費用最大流)
Going Home
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 20617 | Accepted: 10449 |
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
.m
H.
5 5
HH..m
.....
.....
.....
mm..H
7 8
...H....
...H....
...H....
mmmHmmmm
...H....
...H....
...H....
0 0
Sample Output
2
10
28
Source
Pacific Northwest 2004
題目連結:http://poj.org/problem?id=2195
題目大意:n個人n個房子,每間房只能有一個人,每個人走一步花費1元,求每個人都到房子裡所花費的最小總費用
題目分析:基礎的最小費用流,建圖(除了超源和超匯其實是個完全二分圖):超級源點到每個man容量為1,費用為0,每個man到每個house容量為1,費用為兩點間的曼哈頓距離,每個house到超級匯點容量為1,費用為0,跑一下最小費用流即可
題目連結:http://poj.org/problem?id=2195
題目大意:n個人n個房子,每間房只能有一個人,每個人走一步花費1元,求每個人都到房子裡所花費的最小總費用
題目分析:基礎的最小費用流,建圖(除了超源和超匯其實是個完全二分圖):超級源點到每個man容量為1,費用為0,每個man到每個house容量為1,費用為兩點間的曼哈頓距離,每個house到超級匯點容量為1,費用為0,跑一下最小費用流即可
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <cstdlib>
#define x first
#define y second
#define mk make_pair
using namespace std;
int const INF = 0x3fffffff;
int const MAX = 205;
int n, m;
char a[105][105];
int head[MAX], cnt;
int pre[MAX], vis[MAX], dis[MAX];
int src, sk, ans;
struct EGDE
{
int to, nxt, cap, cost;
}e[MAX * MAX / 2];
pair < int, int > house[105];
pair < int, int > man[105];
int hnum, mnum;
void Init()
{
hnum = 1;
mnum = 1;
cnt = 0;
ans = 0;
memset(head, -1, sizeof(head));
}
void Add(int u, int v, int cap, int cost)
{
e[cnt].to = v;
e[cnt].cap = cap;
e[cnt].cost = cost;
e[cnt].nxt = head[u];
head[u] = cnt ++;
e[cnt].to = u;
e[cnt].cap = 0;
e[cnt].cost = -cost;
e[cnt].nxt = head[v];
head[v] = cnt ++;
}
int Get_dis(int i, int j)
{
return abs(house[i].x - man[j].x) + abs(house[i].y - man[j].y);
}
void Build_graph()
{
for(int i = 1; i <= n; i++)
{
scanf("%s", a[i] + 1);
for(int j = 1; j <= m; j++)
{
if(a[i][j] == 'H')
house[hnum ++] = mk(i, j);
if(a[i][j] == 'm')
man[mnum ++] = mk(i, j);
}
}
src = 0;
sk = hnum + mnum - 1;
for(int i = 1; i < mnum; i++)
Add(src, i, 1, 0);
for(int i = 1; i < mnum; i++)
for(int j = 1; j < hnum; j++)
Add(i, mnum + j - 1, 1, Get_dis(i, j));
for(int j = 1; j < hnum; j++)
Add(mnum + j - 1, sk, 1, 0);
}
bool SPFA()
{
memset(vis, false, sizeof(vis));
for(int i = 0; i < MAX; i++)
dis[i] = INF;
memset(pre, -1, sizeof(pre));
queue <int> q;
q.push(src);
vis[src] = true;
dis[src] = 0;
while(!q.empty())
{
int u = q.front();
q.pop();
vis[u] = false;
for(int i = head[u]; i != -1; i = e[i].nxt)
{
int v = e[i].to;
int cap = e[i].cap;
int cost = e[i].cost;
if(cap > 0 && dis[v] > dis[u] + cost)
{
dis[v] = dis[u] + cost;
pre[v] = i;
if(!vis[v])
{
vis[v] = true;
q.push(v);
}
}
}
}
return dis[sk] != INF;
}
void Augment()
{
int mi = INF;
while(SPFA())
{
for(int i = sk; i != src; i = e[pre[i] ^ 1].to)
mi = min(mi, e[pre[i]].cap);
for(int i = sk; i != src; i = e[pre[i] ^ 1].to)
{
ans += mi * e[pre[i]].cost;
e[pre[i]].cap -= mi;
e[pre[i] ^ 1].cap += mi;
}
}
}
int main()
{
while(scanf("%d %d", &n, &m) && (n + m))
{
Init();
Build_graph();
Augment();
printf("%d\n", ans);
}
}
相關文章
- POJ 2195 Going Home 最小費用最大流Go
- POJ 2195-Going Home(KM演算法/最小費用最大流演算法)Go演算法
- 網路中最小費用最大流
- POJ 3308 Paratroopers 最小割、最大流OOP
- POJ 3469-Dual Core CPU(Dinic 最大流/最小割演算法)演算法
- 網路流(最大流,最小割)
- bzoj1834: [ZJOI2010]network 網路擴容(最小費用最大流)
- poj1087 網路最大流
- matlab練習程式(最大流/最小割)Matlab
- 偷寶石(最大流轉化最小割)
- POJ2387-Til the Cows Come Home
- poj 3436 最大流的增廣路演算法演算法
- 網路流最大流、最小割學習筆記筆記
- POJ 2051(最小堆/優先佇列)佇列
- POJ1273 Drainage Ditches【網路流 最大流】AI
- where are you going ? 反序為:going you are whereGo
- POJ 1465-Multiple(BFS-最小整倍數)
- POJ 1459-Power Network(最大流-Edmond-Karp演算法)演算法
- POJ 3565 Ants (最小權完美匹配 KM演算法)演算法
- POJ 2387-Til the Cows Come Home(Dijkstra+堆優化)優化
- POJ 1734 Sightseeing trip Floyd求無向圖最小環
- POJ2387 Til the Cows Come Home【最短路 Dijkstra演算法】演算法
- POJ 1273-Drainage Ditches(最大流-Edmond-Karp演算法/模板)AI演算法
- 轉:How is Internat Bank going?Go
- POJ--2406Power Strings+KMP求字串最小週期KMP字串
- POJ2031 Building a Space Station(最小生成樹,prim)UI
- POJ 3436-ACM Computer Factory(最大流輸出路徑-Edmond-Karp演算法)ACM演算法
- POJ 1149-PIGS(Ford-Fulkerson 標號法求網路最大流)
- POJ 2914-Minimum Cut(Stoer_Wagner最小割演算法)演算法
- poj2594Treasure Exploration【最小路徑覆蓋+floyd傳遞閉包】
- 746. 使用最小花費爬樓梯
- POJ 3253-Fence Repair(哈夫曼樹-最小值優先佇列)AI佇列
- 蘋果應用快速啟動器: Alfred 5 for Mac v5.1.4 (2195)中/英免啟用版蘋果AlfredMac
- GI PSU滾動方式應用GI HOME和DB HOME(opatch auto together)
- 京東技術:用最小的圖片格式,打造最優的使用者體驗
- cf1945D 插隊的最小花費
- spoj 839 最小割應用
- LCR 088. 使用最小花費爬樓梯