poj 2195Going Home
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的更多相关文章
- POJ 2195Going Home(网络流之最小费用流)
题目地址:id=2195">POJ2195 本人职业生涯费用流第一发!!快邀请赛了.决定还是多学点东西.起码碰到简单的网络流要A掉.以后最大流费用流最小割就一块刷. 曾经费用流在我心目 ...
- POJ 3370. Halloween treats 抽屉原理 / 鸽巢原理
Halloween treats Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7644 Accepted: 2798 ...
- POJ 2356. Find a multiple 抽屉原理 / 鸽巢原理
Find a multiple Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7192 Accepted: 3138 ...
- POJ 2965. The Pilots Brothers' refrigerator 枚举or爆搜or分治
The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 22286 ...
- POJ 1753. Flip Game 枚举or爆搜+位压缩,或者高斯消元法
Flip Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 37427 Accepted: 16288 Descr ...
- POJ 3254. Corn Fields 状态压缩DP (入门级)
Corn Fields Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 9806 Accepted: 5185 Descr ...
- POJ 2739. Sum of Consecutive Prime Numbers
Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20050 ...
- POJ 2255. Tree Recovery
Tree Recovery Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11939 Accepted: 7493 De ...
- 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 ...
随机推荐
- Miller-Rabin质数测试
Miller-Rabin质数测试 本文主要讨论使用Miller-Rabin算法编写素数的判定算法,题目来源于hihocoder. 题目 题目要求 时间限制:10000ms 单点时限:1000ms 内存 ...
- 微信小程序开发手册
目录: 数据绑定 条件渲染 列表渲染 API FAQ: <block wx:for> 和 <view wx:for>的区别 wx:if 什么情况下判断为假 坑列表: 微信版本6 ...
- Demon_动画控制(实现前后左右移动,喊叫)
using UnityEngine; using System.Collections; public class PlayerAnimation : MonoBehaviour { float ve ...
- Java基础知识强化之IO流笔记18:FileOutputStream写入数据
1. 创建字节输出流对象,做了几件事情: (1)调用系统功能去创建文件(2)创建fos对象(3)把fos对象指向这个文件 2. 代码示例: package com.himi.fileoutputstr ...
- U口破解指令介绍.
- ADO.NET和ORACLE操作数据库传参数赋值的方式
在使用.Net使用OracleParameter进行Oracle数据库操作的时候,因为Oracle和SQLServer针对查询参数化的语法不同, 在操作SQLServer的时候使用的是@Paramet ...
- Android中使用PullToRefreshListView遇到的问题
1.布局文件中要是设置visible属性为Gone的时候,注意了,这样会有一个bug,在代码中setVisible的时候设置为VISIBLE是不起作用的..这个应该是自身的一个小bug(本人目前没有找 ...
- 一个PHP开发者总结的九条建议
本文只是个人从实际开发经验中总结的一些东西,并不是什么名言警句,写出来有两个目的:一是时刻提醒自己要按照这些知识点来写自己代码,二是为了分享,说不定对你有用呢?万一,是吧... 1.首要意识:安全 大 ...
- How do I size a UITextView to its content?
UITextView 自适应高度,搬来一篇stack上的: Is there a good way to adjust the size of a UITextView to conform to ...
- OC语法简写
NSNumber [NSNumber numberWithInt:666] 等价于 @666 [NSNumber numberWithLongLong:666ll] 等价于 @666ll [NSNum ...