http://poj.org/problem?id=2195

 #include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
#include<cstdlib>
#define maxn 500
using namespace std;
int n,mm;
char g[maxn][maxn];
const int inf=<<; struct node
{
int x,y;
}m[maxn]; struct node1
{
int x,y;
}h[maxn]; int cap[maxn][maxn];
int cost[maxn][maxn];
int flow[maxn][maxn];
int p[maxn];
int s,t;
int main()
{
while(scanf("%d%d",&n,&mm)&&n&&mm)
{
memset(cap,,sizeof(cap));
memset(cost,,sizeof(cost));
int t1=,t2=;
for(int i=; i<n; i++)
{
scanf("%s",g[i]);
for(int j=; j<mm; j++)
{
if(g[i][j]=='m')
{
m[++t1].x=i;
m[t1].y=j;
}
else if(g[i][j]=='H')
{
h[++t2].x=i;
h[t2].y=j;
}
}
}
for(int i=; i<=t1; i++)
{
cap[][i]=;
cost[][i]=;
}
for(int i=; i<=t1; i++)
{
for(int j=; j<=t2; j++)
{
cap[i][t1+j]=;
cost[i][t1+j]=abs(m[i].x-h[j].x)+abs(m[i].y-h[j].y);
cost[t1+j][i]=-cost[i][t1+j];
}
}
for(int j=; j<=t2; j++)
{
cap[t1+j][t1+t2+]=;
cost[t1+j][t1+t2+]=;
}
s=,t=t1+t2+;
queue<int>q;
int d[maxn];
memset(flow,,sizeof(flow));
int c=,f=;
for(;;)
{
bool inq[maxn];
for(int i=; i<=t1+t2+; i++) d[i]=(i==?:inf);
memset(inq,,sizeof(inq));
q.push(s);
while(!q.empty())
{
int u=q.front();q.pop();
inq[u]=false;
for(int v=; v<=t1+t2+; v++) if(cap[u][v]>flow[u][v] && d[v]>d[u]+cost[u][v])
{
d[v]=d[u]+cost[u][v];
p[v]=u;
if(!inq[v])
{
inq[v]=true;
q.push(v);
}
}
}
if(d[t]==inf) break;
int a=inf;
for(int u=t; u!=s; u=p[u])
{
if(cap[p[u]][u]-flow[p[u]][u]<a)
{
a=cap[p[u]][u]-flow[p[u]][u];
}
}
for(int u=t; u!=s; u=p[u])
{
flow[p[u]][u]+=a;
flow[u][p[u]]-=a;
}
c+=d[t]*a;
f+=a;
}
printf("%d\n",c);
}
return ;
}

poj 2195Going Home的更多相关文章

  1. POJ 2195Going Home(网络流之最小费用流)

    题目地址:id=2195">POJ2195 本人职业生涯费用流第一发!!快邀请赛了.决定还是多学点东西.起码碰到简单的网络流要A掉.以后最大流费用流最小割就一块刷. 曾经费用流在我心目 ...

  2. POJ 3370. Halloween treats 抽屉原理 / 鸽巢原理

    Halloween treats Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7644   Accepted: 2798 ...

  3. POJ 2356. Find a multiple 抽屉原理 / 鸽巢原理

    Find a multiple Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7192   Accepted: 3138   ...

  4. POJ 2965. The Pilots Brothers' refrigerator 枚举or爆搜or分治

    The Pilots Brothers' refrigerator Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 22286 ...

  5. POJ 1753. Flip Game 枚举or爆搜+位压缩,或者高斯消元法

    Flip Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 37427   Accepted: 16288 Descr ...

  6. POJ 3254. Corn Fields 状态压缩DP (入门级)

    Corn Fields Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9806   Accepted: 5185 Descr ...

  7. POJ 2739. Sum of Consecutive Prime Numbers

    Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20050 ...

  8. POJ 2255. Tree Recovery

    Tree Recovery Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11939   Accepted: 7493 De ...

  9. POJ 2752 Seek the Name, Seek the Fame [kmp]

    Seek the Name, Seek the Fame Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 17898   Ac ...

随机推荐

  1. Java日期相关操作

    1.获得日期 在旧版本 JDK 的时代,有不少代码中日期取值利用了 java.util.Date 类,但是由于 Date 类不便于实现国际化,其实从 JDK1.1 开始,就更推荐使用 java.uti ...

  2. mybatis 关联对象mapper.xml的写法

    https://github.com/zfrHJ/mybaties/blob/master/mybaties/src/com/itheima/mybatits/mapper/OrdersMapperC ...

  3. 【BZOJ4327】JSOI2012 玄武密码 AC自动机

    [BZOJ4327]JSOI2012 玄武密码 Description 在美丽的玄武湖畔,鸡鸣寺边,鸡笼山前,有一块富饶而秀美的土地,人们唤作进香河.相传一日,一缕紫气从天而至,只一瞬间便消失在了进香 ...

  4. Android(java)学习笔记246:ContentProvider使用之学习ContentProvider(内容提供者)的目的

    1.使用ContentProvider,把应用程序私有的数据暴露给别的应用程序,让别的应用程序完成对自己私有的数据库数据的增删改查的操作. 2.ContentProvider的应用场景: 获取手机系统 ...

  5. gvim & vim

    安装了 GVim for Windows. 一 普通功能配置 配置文件 _vimrc 在安装目录下面. 关闭闪屏和声音提示功能: set visualbell t_vb= "关闭visual ...

  6. HDU5301

    题意:给n*m的矩形区域,剔除其中1*1的方块,然后用不同矩形块填充整个矩形区域,求需要的矩形块最大面积的最小值. 思路:先判把矩形矫正,然后特殊处理边值为奇数,且在中心点的情况,最后处理障碍在其他位 ...

  7. 2015 Multi-University Training Contest 2

    附上第二场比赛的链接 从5300-5309 我是链接 顺便贴出官方题解:

  8. Spring框架

    Spring框架的根本使命是:简化JAVA开发,为了简化开发,有以下四个策略 基于POJO的轻量级和最小侵入性编程: 通过依赖注入和面向接口实现松耦合: 基于切面和惯性进行声明式编程: 通过切面和模板 ...

  9. 跳ajax方式进行前后台交互之后台代码要怎么写

    package com.zq.www.mis.action; import java.util.List; import org.apache.struts2.convention.annotatio ...

  10. (转)Mac OS X中配置Apache

    我使用的Mac OS X版本是10.8.2,Mac自带了Apache环境. 启动Apache 设置虚拟主机 启动Apache 打开“终端(terminal)”,输入 sudo apachectl -v ...