Poj 2195 Going Home(费用流)
来源:互联网 发布:大数据 云计算 关系 编辑:程序博客网 时间:2024/06/05 19:05
Going Home
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 22327 Accepted: 11280
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
Source
Pacific Northwest 2004
/*费用流裸题.二分图搞.从源点向man连边流量为1费用为0.从man向house连边流量为1费用为曼哈顿距离.从house向汇点连边流量为1费用为0. */#include<iostream>#include<cstdio>#include<cmath>#include<cstring>#include<queue>#define MAXN 101#define INF 1e9using namespace std;int n,m,tot1,tot2,cut=1,S,T,ans;int g[MAXN][MAXN],vis[MAXN*MAXN],fa[MAXN*MAXN],head[MAXN*MAXN],dis[MAXN*MAXN];struct data{int x,y;}a[MAXN*MAXN],b[MAXN*MAXN];struct edge{int u,v,c,f,next;}e[MAXN*MAXN*2];queue<int>q;void add(int u,int v,int c,int f){ e[++cut].u=u;e[cut].v=v;e[cut].next=head[u];e[cut].c=c;e[cut].f=f;head[u]=cut; e[++cut].u=v;e[cut].v=u;e[cut].next=head[v];e[cut].c=0;e[cut].f=-f;head[v]=cut;}bool bfs(int t){ for(int i=1;i<=T;i++) dis[i]=INF;dis[0]=0; q.push(S); while(!q.empty()) { int u=q.front();q.pop();vis[u]=0; for(int i=head[u];i;i=e[i].next) { int v=e[i].v; if(dis[v]>dis[u]+e[i].f&&e[i].c) { dis[v]=dis[u]+e[i].f;fa[v]=i; if(vis[v]!=t) vis[v]=t,q.push(v); } } } return dis[T]!=INF;}void mincost(){ int t=1;ans=0; while(bfs(t)) { int tmp=fa[T],x=INF; while(tmp) x=min(x,e[tmp].c),tmp=fa[e[tmp].u]; tmp=fa[T]; while(tmp) { e[tmp].c-=x; e[tmp^1].c+=x; e[tmp].f*=x; ans+=e[tmp].f; tmp=fa[e[tmp].u]; } t++; } printf("%d\n",ans);}void slove(){ for(int i=1;i<=tot1;i++) add(0,i,1,0); for(int i=1;i<=tot2;i++) add(i+tot1,tot1+tot2+1,1,0); for(int i=1;i<=tot1;i++) for(int j=1;j<=tot2;j++) { int x=fabs(a[i].x-b[j].x)+fabs(a[i].y-b[j].y); add(i,tot1+j,1,x); } mincost(); return ;}void Clear(){ tot1=tot2=0;cut=1; memset(head,0,sizeof head); memset(b,0,sizeof b); return ;}int main(){ char ch; while(scanf("%d%d",&n,&m)) { if(!n&&!m) break; Clear(); for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) { cin>>ch; if(ch=='m') a[++tot1].x=i,a[tot1].y=j; else if(ch=='H') b[++tot2].x=i,b[tot2].y=j; } T=tot1+tot2+1;slove(); } return 0;}
- POJ 2195 Going Home(费用流)
- poj 2195 Going Home (费用流)
- [费用流] POJ 2195 Going Home
- poj 2195 Going Home (费用流/KM)
- poj 2195 Going Home【zkw费用流】
- POJ 2195 Going Home(费用流)
- [POJ 2195]Going Home[费用流]
- Poj 2195 Going Home【费用流Min_Cost_Max_flow】
- POJ 2195 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最小费用最大流
- poj - 2195 - Going Home(最小费用最大流)
- poj 2195 Going Home(KM||费用流)
- hdu 1533 || poj 2195 Going Home (最小费用最大流)
- Java Web 常用工具类(持续更新)
- 在线 Python运行工具
- 浅析I/O模型及其设计模式
- task_struct 结构描述
- Java初学之命令行参数
- Poj 2195 Going Home(费用流)
- go各种初步技能
- 剑指offer 二叉树中和为某一值的路径
- SpringMVC之RequestMethod
- 关于在myeclipse中使用Hibernate遇到的问题
- 图的存储二
- 解决Handler导致的内存泄漏真实样例
- 蓝桥杯-P1103-复数运算
- 去除CSDN 博客页广告的历程