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. [HDU 2068] RPG的错排 (错排问题)

    RPG的错排 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2068 题目大意: 有N个人对应N个名字,然后你去把每一个名字对应到每个人,只要求答对一半 ...

  2. motan源码分析十:流量切换

    motan提供了流量切换的功能,可以实现把一个group的流量切换到另一个group(一个或多个服务都可以).大家可以使用tomcat部署motan的管理工具,并设置几个组,例如可以参考demo代码: ...

  3. 微信小程序开发手册

    目录: 数据绑定 条件渲染 列表渲染 API FAQ: <block wx:for> 和 <view wx:for>的区别 wx:if 什么情况下判断为假 坑列表: 微信版本6 ...

  4. iOS 启动连续闪退保护方案

    引言 “如果某个实体表现出以下任何一种特性,它就具备自主性:自我修复.自我保护.自我维护.对目标的自我控制.自我改进.” —— 凯文·凯利 iOS App 有时可能遇到启动必 crash 的绝境:每次 ...

  5. 初识html、css时随笔记录

    css部分样式 圆框效果:border-radius:5px; 固定样式:position:fixed;悬浮窗在IE6中position:absolute其余浏览器可以用fixed: 使table中的 ...

  6. Web中常用字体介绍(转)

    1.在Web编码中,CSS默认应用的Web字体是有限的,虽然在新版本的CSS3,我们可以通过新增的@font-face属性来引入特殊的浏览器加载字体. 浏览器中展示网页文字内容时,文字字体都会按照设计 ...

  7. 各版本 linux(转)

    Linux各种版本下载 ftp://ftp.linuxforum.net/ISO/Redhat7.3/valhalla-i386-disc1.iso ftp://ftp.linuxforum.net/ ...

  8. IMP数据文件时ORA-00959错误分析

    有时间模拟一下ORA-00959的测试分析.

  9. Oracle 创建分页存储过程(转帖)

    原贴地址:http://19880614.blog.51cto.com/4202939/1316560 ps:源代码还有很多错误,我修改了 ------------------------------ ...

  10. 【转】iOS申请发布证书-图文详解

    摘要 发布产品到App Store所需证书,2013年5月26日测试 IOS 发布证书 distribution 打包程序 真机调试证书 本文讲述发布证书的申请,申请真机调试证书请参考:http:// ...