博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj 2195 (最小费用最大流)
阅读量:4959 次
发布时间:2019-06-12

本文共 4521 字,大约阅读时间需要 15 分钟。

赤裸裸的最小费用最大流,不过话说KM 算法当然也可以搞定。

Going Home
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 14859   Accepted: 7604

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.mH.5 5HH..m...............mm..H7 8...H.......H.......H....mmmHmmmm...H.......H.......H....0 0

Sample Output

21028

Source

 
#include 
#include
#include
using namespace std;#define N 10010#define INF 0x3fffffffstruct node{ int to,next,w,c;}edge[100*N];int cnt,pre[N];int n,m;char g[110][110];int s,t;int que[N*1000];int point[N],pedge[N];void add_edge(int u,int v,int w,int c){ edge[cnt].to=v; edge[cnt].w=w; edge[cnt].c=c; edge[cnt].next=pre[u]; pre[u]=cnt++;}int spfa(){ int qf=1,qd=0; int dis[N]; int mark[N]; memset(point,-1,sizeof(point)); memset(pedge,-1,sizeof(pedge)); for(int i=0;i<=t;i++) { dis[i]=INF; mark[i]=0; } dis[s]=0; mark[s]=1; que[0]=s; while(qf>qd) { int cur=que[qd++]; mark[cur]=0; for(int p=pre[cur];p!=-1;p=edge[p].next) { int v=edge[p].to; int w=edge[p].w; int c=edge[p].c; if(edge[p].w==0) continue; if( dis[v] > dis[cur]+c ) { dis[v]=dis[cur]+c; point[v]=cur; pedge[v]=p; if(mark[v]==0) { mark[v]=1; que[qf++]=v; } } } } if( dis[t] == INF ) return 0; else return 1;}int fuc(){ int sum=0; int tmp=t; while(tmp!=s) { int p=pedge[tmp]; edge[p].w -= 1; edge[p^1].w += 1; sum+=edge[p].c; tmp=point[tmp]; } return sum;}int main(){ while(scanf("%d%d",&n,&m)&&(n+m)) { cnt=0; s=0; t=n*m+1; memset(pre,-1,sizeof(pre)); for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) { int id=(i-1)*m+j; cin>>g[i][j]; if( g[i][j]=='m' ) { add_edge(s,id,1,0); add_edge(id,s,0,0); } if(g[i][j]=='H') { add_edge(id,t,1,0); add_edge(t,id,0,0); } if(i!=1) { add_edge(id,id-m,INF,1); add_edge(id-m,id,0,-1); } if(i!=n) { add_edge(id,id+m,INF,1); add_edge(id+m,id,0,-1); } if(j!=1) { add_edge(id,id-1,INF,1); add_edge(id-1,id,0,-1); } if(j!=m) { add_edge(id,id+1,INF,1); add_edge(id+1,id,0,-1); } } int sum=0; while(spfa()) { sum+=fuc(); } printf("%d\n",sum); } return 0;}

 

转载于:https://www.cnblogs.com/chenhuan001/archive/2013/03/09/2951601.html

你可能感兴趣的文章
DirectX11--HR宏关于dxerr库的替代方案
查看>>
添加cordova-plugin-file-opener2后,打包出错
查看>>
wpf SnapsToDevicePixels
查看>>
JavaScript 数值Number类型详解
查看>>
WPF的5种绑定模式(mode)
查看>>
电子商务(电销)平台中用户模块(User)数据库设计明细
查看>>
用SignalR 2.0开发客服系统[系列4:负载均衡的情况下使用SignalR]
查看>>
IIS 7.5 使用URL Rewrite模块简单设置网页跳转
查看>>
C#开发微信门户及应用(15)-微信菜单增加扫一扫、发图片、发地理位置功能
查看>>
ASP.NET MVC 音乐商店 - 7.成员管理和授权
查看>>
与众不同 windows phone (26) - Contacts and Calendar(联系人和日历)
查看>>
php+sqlite cms
查看>>
php实现邮件自动发送之PHPMailer
查看>>
Jersey
查看>>
CentOS 6.3安装phpmyadmin出错解决办法!
查看>>
Oracle的三种高可用集群方案
查看>>
OracleUNDO
查看>>
rhel 6.5 yum源的配置
查看>>
zorka
查看>>
JMX实现远程服务器Tomcat系统监控之三
查看>>