hdu 1533 Going Home 最小费用最大流 (模板题)
Going Home
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7405 Accepted Submission(s): 3907
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.
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.

#include<iostream>
#include<string.h>
#include<string>
#include<algorithm>
#include<queue>
#define ll long long
#define mx 0x3f3f3f3f
using namespace std;
int cap[][],cost[][],flow[][];//cap是容量,cost是花费,flow是流量
int pre[],dis[],vis[],cnt[];//pre是记录增广路的前驱节点,dis[i]是记录源点到节点i的最小距离
//vis[i]标记点是否在队列中,cnt[i]记录点i进入队列的次数
char str[][];
struct node
{
int x;
int y;
}s[],e[]; int n,m;
int st,endd,nn,mm;//st是源点,endd是汇点
int mn_cost,mx_flow;
int spfa()
{
for(int i=;i<n;i++)
dis[i]=mx;
memset(vis,,sizeof(vis));
memset(cnt,,sizeof(cnt)); queue<int>p;
p.push(st);//st是起点
vis[st]=;
cnt[st]=;
dis[st]=;
while(!p.empty())
{
int now=p.front();
p.pop();
vis[now]=;
for(int i=;i<n;i++)
{
if(cap[now][i]>flow[now][i]&&dis[i]>dis[now]+cost[now][i])//这里将费用看成是长度,求源点到汇点的最小距离
{
dis[i]=dis[now]+cost[now][i];
pre[i]=now;
if(vis[i]==)
{
p.push(i);
vis[i]=;
cnt[i]++;
if(cnt[i]>n)
return ;
}
}
}
}
if(dis[endd]>=mx)
return ;
return ;
}
void bfs(int n)//顶点数
{
memset(flow,,sizeof(flow));
mx_flow=;
mn_cost=;
while(spfa())
{
int f=mx;
for(int i=endd;i!=st;i=pre[i])
f=min(f,cap[pre[i]][i]-flow[pre[i]][i]); for(int i=endd;i!=st;i=pre[i])//更新流量
{
flow[pre[i]][i]+=f;
flow[i][pre[i]]-=f;
}
mn_cost+=dis[endd]*f;
mx_flow+=f; }
}
int main()
{
while(~scanf("%d%d",&nn,&mm)&&nn&&mm)
{
//这道题需要自己将输入转化,建立一个有向图
int cnt1=,cnt2=;
for(int i=;i<nn;i++)
{
scanf("%s",str[i]);
for(int j=;j<mm;j++)
{
if(str[i][j]=='m')//起点
{
cnt1++;
s[cnt1].x=i;
s[cnt1].y=j;
}
if(str[i][j]=='H')//终点
{
cnt2++;
e[cnt2].x=i;
e[cnt2].y=j;
}
}
}
n=cnt1+cnt2+;//加上源点和汇点,共有n个点,[0,n-1]
st=;//源点
endd=cnt1+cnt2+;//汇点
memset(cap,,sizeof(cap));
memset(cost,,sizeof(cost));
for(int i=;i<=cnt1;i++)//初始化源点到任意起点的花费为0,容量为1;
{
cap[][i]=;
cost[][i]=;
cost[i][]=;
}
for(int i=;i<=cnt2;i++)//初始化汇点到所有终点的花费为0,容量为1;
{
cap[i+cnt1][endd]=;
cost[i+cnt1][endd]=;
cost[endd][i+cnt1]=;
}
for(int i=;i<=cnt1;i++)//初始化起点到终点的花费和容量
{
for(int j=;j<=cnt2;j++)
{
cap[i][cnt1+j]=;
cost[i][cnt1+j]=abs(s[i].x-e[j].x)+abs(s[i].y-e[j].y);
cost[cnt1+j][i]=-cost[i][cnt1+j];
}
}
bfs(n);
printf("%d\n",mn_cost);
}
return ;
}
hdu 1533 Going Home 最小费用最大流 (模板题)的更多相关文章
- hdu 1533 Going Home 最小费用最大流 入门题
Going Home Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tota ...
- POJ 2195 & HDU 1533 Going Home(最小费用最大流)
这就是一道最小费用最大流问题 最大流就体现到每一个'm'都能找到一个'H',但是要在这个基础上面加一个费用,按照题意费用就是(横坐标之差的绝对值加上纵坐标之差的绝对值) 然后最小费用最大流模板就是再用 ...
- hdu 1533 Going Home 最小费用最大流
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1533 On a grid map there are n little men and n house ...
- 【网络流#2】hdu 1533 - 最小费用最大流模板题
最小费用最大流,即MCMF(Minimum Cost Maximum Flow)问题 嗯~第一次写费用流题... 这道就是费用流的模板题,找不到更裸的题了 建图:每个m(Man)作为源点,每个H(Ho ...
- POJ2135 最小费用最大流模板题
练练最小费用最大流 此外此题也是一经典图论题 题意:找出两条从s到t的不同的路径,距离最短. 要注意:这里是无向边,要变成两条有向边 #include <cstdio> #include ...
- 2018牛客网暑期ACM多校训练营(第五场) E - room - [最小费用最大流模板题]
题目链接:https://www.nowcoder.com/acm/contest/143/E 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言524288K ...
- hdu 3395(KM算法||最小费用最大流(第二种超级巧妙))
Special Fish Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- HDU3376 最小费用最大流 模板2
Matrix Again Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 102400/102400 K (Java/Others)To ...
- 图论算法-最小费用最大流模板【EK;Dinic】
图论算法-最小费用最大流模板[EK;Dinic] EK模板 const int inf=1000000000; int n,m,s,t; struct node{int v,w,c;}; vector ...
随机推荐
- FTP 上传下载 进度条
11 /// <summary> /// 文件上传 /// </summary> /// <param name="filePath">原路径( ...
- wpf表单验证
在做表单的,需要对User提交数据做验证,wpf与silverlight 都提供自带的验证机制,但是只是验证,并不能在提交时提供详细的信息,此时可使用 依赖属性将错误信息整合进自定义错误集合中,即可在 ...
- Mockito 中文文档 ( 2.0.26 beta )
Mockito 中文文档 ( 2.0.26 beta ) 由于缺乏校对,难免有谬误之处,如果发现任何语句不通顺.翻译错误,都可以在github中的项目提出issue.谢谢~ Mockito框架官方地址 ...
- Matlab的简单数据保存读取
1.使用load进行文件读取 例如读入文件名为'filename.txt'中的数据,那么可以使用以下代码: load('filename.txt') 注意:filename.txt中的数据应符合矩阵形 ...
- JavaWeb之过滤器
过滤器 什么是过滤器 1示意图: 过滤器的作用: 1.过滤器的作用好比一个保安.是servlet规范中的技术 2.用户在访问应用的资源之前或者之后,可以对请求做出一定的处理 编写过滤器步骤: 1.编写 ...
- 吴裕雄--天生自然ORACLE数据库学习笔记:SQL语言基础
select empno,ename,sal from scott.emp; SELECT empno,ename,sal FROM scott.emp; selECT empno,ename,sal ...
- Spring boot 中发送邮件
参考:https://blog.csdn.net/qq_39241443/article/details/81293939 添加依赖: <dependency> <groupId&g ...
- Java基础 -1.4
标识符与关键字 对于标识符的组成在Java之中的定义如下:由字母.数字._.$ 组成 其中不能使用Java的保留字(关键字) 其中 $ 一般都有特殊的含义 不建议出现在自己所编写的代码上 关键字 是系 ...
- task20160125
http://task.zbj.com/2034844/n15o1.html 百度开放平台--首页>帮助文档首页>Frontia>Android开发指南>个人数据存储 http ...
- Python学习 —— 实现简单的爬虫
为了加快学习python3.x,查了许多资料后写了这个脚本,这个脚本主要是爬取百度图片'东方幻想乡'的图片,但还是有很多问题存在. 下面给出代码: # 更新了一下代码 from urllib impo ...