POJ2195--Going Home
2013-05-23 12:15
225 查看
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
Sample Output
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
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> using namespace std; #define maxn 10080 #define edge 1000008 #define inf 0x3f3f3f3f int first[maxn]; int vv[edge],ww[edge],nxt[edge],cst[edge]; int e; int pre[maxn],pos[maxn],dis[maxn],que[maxn*10]; bool vis[maxn]; inline int min(int a,int b) { return a>b?b:a; } void addEdge(int u,int v,int w,int cost) { vv[e] = v;ww[e] = w;cst[e] = cost;nxt[e] = first[u];first[u] = e++; vv[e] = u;ww[e] = 0;cst[e] = -cost;nxt[e] = first[v];first[v] = e++; } bool spfa(int s,int t) { int i; memset(pre,-1,sizeof(pre)); memset(vis,0,sizeof(vis)); int head,tail; head = tail = 0; for(i=0;i<maxn;i++) dis[i] = inf; que[tail++] = s; pre[s] = s; dis[s] = 0; vis[s] = 1; while(head != tail) { int u = que[head++]; vis[u] = 0; for(i=first[u];i!=-1;i=nxt[i]) { int v = vv[i]; if(ww[i] > 0 && dis[u] + cst[i] < dis[v]) { dis[v] = dis[u] + cst[i]; pre[v] = u; pos[v] = i; if(!vis[v]) { vis[v] = 1; que[tail++] = v; } } } } return pre[t]!=-1; } int MinCostFlow(int s,int t) { int i; int cost = 0; while(spfa(s,t)) { int f = inf; for(i=t;i!=s;i=pre[i]) { if(ww[pos[i]] < f) f = ww[pos[i]]; } cost += dis[t] * f; for(i=t;i!=s;i=pre[i]) { ww[pos[i]] -= f; ww[pos[i]^1] += f; } } return cost; } struct PP { int r,c; }pp[10008]; struct HS { int r,c; }hs[108]; int main() { //freopen("in.txt","r",stdin); int n,m; while(scanf("%d%d",&n,&m)==2 && (n||m)) { memset(first,-1,sizeof(first)); e = 0; getchar(); int h,p; h = p = 0; for(int i=1;i<=n;i++) { for(int j=1;j<=m;j++) { char c = getchar(); if(c == 'H') { hs[h].r = i; hs[h++].c = j; } else if(c == 'm') { pp[p].r = i; pp[p++].c = j; } } getchar(); } for(int i=0;i<p;i++) { for(int j=0;j<h;j++) { int cost = abs(pp[i].r-hs[j].r)+abs(pp[i].c-hs[j].c); addEdge(i+1,j+p+1,1,cost); } } for(int i=0;i<p;i++) { addEdge(0,i+1,1,0); } for(int i=0;i<h;i++) { addEdge(i+p+1,h+p+1,1,0); } int ans = MinCostFlow(0,h+p+1); printf("%d\n",ans); } return 0; }
相关文章推荐
- POJ 2195 Going Home
- POJ2195 Going Home(spfa费用流+链式前向星)
- POJ 2195 Going Home (最小费用最大流)
- POJ 2195 Going Home(最小权匹配、KM算法)
- poj 2195 Going Home (费用流)
- POJ 2195 Going Home(二分图最大权值匹配) KM
- poj 2195//hdu 1533 Going Home 最小费用流(spfa)
- POJ2195 Going Home(最小费用最大流mcmf)
- POJ-2195-Going Home
- POJ 2195 Going Home(最小费用最大流)
- poj 2195 zoj 2404 hdu 1533 Going Home(最小费用流)
- poj 2195 Going Home 最小费最大流
- poj 2195 Going Home 网络流
- 【POJ 2195】 Going Home(最小费)
- poj-2195-Going Home最小费用最大流
- poj 2195 Going Home(最小费最大流)
- POJ 2195 Going Home【KM算法-二分图的最优匹配】
- POJ 2195 Going Home 最小费用最大流 尼玛,心累
- 【POJ 2195】 Going Home(KM算法求最小权匹配)
- POJ-2195 Going Home (最小费用最大流初学 && 最大权二分匹配—KM算法)