hdu 1533 Going Home(最小费用最大流)
2016-07-29 17:36
459 查看
Going Home
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4204 Accepted Submission(s): 2163
Problem 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
题意:给一个n*m的矩阵,.代表空地,H代表房子,m代表人,m的数量和H相同,H最多不超过100
现在问所有的人去房子里最少要多久,走一步算一个单位时间
思路:很容易看出是个二分带权匹配问题,这里练习最小费用,所以用网络流来解。
构图很简单,构建超级源点和超级汇点,所有的容量都为1,m与H之间的费用为两者间的距离,跑一遍MCMF即可
代码:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <queue> using namespace std; #define N 410 #define INF 99999999 struct Node { int x,y; } p ,q ; struct Edge { int u,v,cap,cost; int next; } edge[N*N]; int cnt,head ,vis ,pp ,d ; int n,m,cn,cm; char ma ; void init() { cnt=cn=cm=0; memset(head,-1,sizeof(head)); } void addedge(int u,int v,int cost,int cap) { edge[cnt].u=u,edge[cnt].v=v; edge[cnt].cost=cost,edge[cnt].cap=cap; edge[cnt].next=head[u]; head[u]=cnt++; edge[cnt].u=v,edge[cnt].v=u; edge[cnt].cost=-cost,edge[cnt].cap=0; edge[cnt].next=head[v]; head[v]=cnt++; } int dist(Node a,Node b) { return abs(b.x-a.x)+abs(b.y-a.y); } int spfa(int s,int t,int n) { memset(pp,-1,sizeof(pp)); memset(vis,0,sizeof(vis)); for(int i=0; i<=n; i++) d[i]=INF; d[s]=0; queue<int>que; vis[s]=1; que.push(s); while(!que.empty()) { int u=que.front(); que.pop(); vis[u]=0; for(int i=head[u]; i!=-1; i=edge[i].next) { int v=edge[i].v; if(edge[i].cap>0&&d[v]>d[u]+edge[i].cost) { d[v]=d[u]+edge[i].cost; pp[v]=i; if(!vis[v]) { vis[v]=1; que.push(v); } } } } if(d[t]==INF) return 0; return 1; } int MCMF(int s,int t,int n) { int mincost=0,minflow; while(spfa(s,t,n)) { minflow=INF+1; for(int i=pp[t]; i!=-1; i=pp[edge[i].u]) minflow=min(minflow,edge[i].cap); for(int i=pp[t]; i!=-1; i=pp[edge[i].u]) { edge[i].cap-=minflow; edge[i^1].cap+=minflow; } mincost+=minflow*d[t]; } return mincost; } int main() { while(~scanf("%d %d",&n,&m)&&(n+m)) { init(); for(int i=0; i<n; i++) scanf("%s",ma[i]); for(int i=0; i<n; i++) for(int j=0; j<m; j++) { if(ma[i][j]=='H') p[++cn].x=i+1,p[cn].y=j+1; else if(ma[i][j]=='m') q[++cm].x=i+1,q[cm].y=j+1; } for(int i=1; i<=cm; i++) for(int j=1; j<=cn; j++) addedge(i,j+cm,dist(q[i],p[j]),1); int s=0,t=2*cn+1; for(int i=1; i<=cm; i++) addedge(0,i,0,1); for(int i=1; i<=cn; i++) addedge(i+cm,t,0,1); printf("%d\n",MCMF(s,t,t)); } return 0; }
相关文章推荐
- Algorithm中Heap
- 每天进步一点点——ubuntu 13.10中安装google的gtest库
- 马哥预习视频
- hdu 1847(Good Luck in CET-4 Everybody!——巴什博弈)
- Google Material Design的图标字体使用教程
- mac 下配置 VS Code 开发 Golang
- Glide,Google推荐的图片加载库
- django基本命令
- 《GO学习笔记》------------------------(2)
- go语言
- google play商店应用过滤机制,设备应用不兼容的说明及解决办法
- Django博客功能实现—文章评论功能
- Mongoose 对象的特殊性
- 有关《口袋妖怪GO》的六个基本事实
- 设计模式GOF23——抽象工厂模式
- gogs 安装使用
- Django csrf cooie is not set
- 基于google蓝牙示例讲解蓝牙开发步骤
- Go语言之基本数据类型以及一些规范
- Go语言之介绍