POJ 2195 Going Home
来源:互联网 发布:红色 知乎 编辑:程序博客网 时间:2024/06/06 14:01
Going Home
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 18013 Accepted: 9181
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
刚学费用流,理解的还不是很深刻,对于这个题,找到m的点,与源点S相连,找到H的点,与汇点T相连,在依次找到各点之间的距离,进行建图
#include <stdio.h>#include <stdlib.h>#include <string.h>#include <iostream>#include <math.h>#include <queue>#define Maxm 100001#define inf 9999999using namespace std;struct node{int v,u;int f;int w; int next;}edge[Maxm]; int n,m,S,T,cnt;int head[Maxm];int pre[Maxm],dis[Maxm],vis[Maxm];void add(int x,int y,int f,int w){ edge[cnt].u=x; edge[cnt].v=y; edge[cnt].f=f; edge[cnt].w=w; edge[cnt].next=head[x]; head[x]=cnt++; edge[cnt].u=y; edge[cnt].v=x; edge[cnt].f=0; edge[cnt].w=-w; edge[cnt].next=head[y]; head[y]=cnt++;}int spfa(){ memset(dis,inf,sizeof(dis)); memset(vis,0,sizeof(vis)); memset(pre,-1,sizeof(pre)); vis[S]=1; dis[S]=0; queue<int>q; q.push(S); while(!q.empty()) { int t=q.front(); q.pop(); vis[t]=0; int r=head[t]; while(r!=-1) { if(edge[r].f>0 && dis[edge[r].v] > dis[t] + edge[r].w) { dis[edge[r].v] = dis[t] + edge[r].w; pre[edge[r].v]=r; if(!vis[edge[r].v]) { vis[edge[r].v]=1; q.push(edge[r].v); } } r=edge[r].next; } } if(pre[T]==-1) return 0; else return 1;}int minfei(){ int ans=0; while(spfa()) { int max1=inf; for(int t=pre[T];t!=-1;t=pre[edge[t].u]) { max1=min(max1,edge[t].f); } for(int t=pre[T];t!=-1;t=pre[edge[t].u]) { edge[t].f-=max1; edge[t+1].f+=max1; ans+= max1*edge[t].w; } } return ans;}int main(){ while(~scanf("%d%d",&n,&m)&&(m||n)) { char map[110][110]; int i,j; int x1[Maxm],y1[Maxm]; int x2[Maxm],y2[Maxm]; int s1=0,s2=0; S=0; T=m*n+1; cnt=0; memset(head,-1,sizeof(head)); for(i=0;i<n;i++) scanf("%s",map[i]); for(i=0;i<n;i++) { for(j=0;j<m;j++) { int t=i*m+j+1; if(map[i][j]=='m') { add(S,t,1,0); x1[s1]=i+1; y1[s1++]=j+1; } if(map[i][j]=='H') { add(t,T,1,0); x2[s2]=i+1; y2[s2++]=j+1; } } } for(i=0;i<s1;i++) { for(j=0;j<s2;j++) { int s=abs(x1[i]-x2[j]) + abs(y1[i]-y2[j]) ; int a=x1[i]*m+y1[i]-m; int b=x2[j]*m+y2[j]-m; add(a,b,1,s); } } printf("%d\n",minfei()); } return 0;}
0 0
- 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
- 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
- hdu 1532 Drainage Ditches(网络流dinic模板)
- Nivoslider:丰富的图片切换效果
- Tomcat启动时,spring加载配置文件applicationContext.xml出错,抛出nested exception is og.xml.sax.SAXParseException
- poj 2498 StuPId
- leetcode之Container With Most Water 和Trapping Rain Water
- POJ 2195 Going Home
- logback 常用配置详解(方便以后查阅)
- 在jenkins中解决svn目录中有中文的问题
- 帮助写贝塞尔曲线控制点代码的神器
- tomcat SSL配置异常-“No Certificate file specified or invalid file format”
- 银行调度系统
- java设计模式学习(五):命令模式
- Lua学习笔记 第七章 迭代器与泛型for
- Ajax form 提交