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 ...
随机推荐
- PIP安装模块下载慢或者无法下载
pip下载不动,模块安装失败 由于pip默认的下载源在国外,下载的人也多,难免有时会抽风,下载慢还能熬一熬,有时候就直接安装失败了. 安装gevent模块的的时候,进度条慢悠悠的到达100%,以为马上 ...
- 【网摘】JS 或 jQuery 获取当前页面的 URL 信息
1.设置或获取对象指定的文件名或路径. window.location.pathname 2.设置或获取整个 URL 为字符串. window.location.href 3.设置或获取与 URL 关 ...
- Chrome 打不开任意网页以及设置、扩展程序等页面解决方法
解决办法:在快捷键加启动参数 -no-sandbox,如果可以启动,就说明是电脑上有某个软件与Chrome沙盒有冲突
- 2.1 MySQL基础使用
本文是课上资料的总结非原创没有转载地址 目录 引言 为什么需要数据库? 数据库和应用程序的关系 MySQL基础使用 一.数据库简介 1.1 简介 1.2 常见数据库管理系统 1.3 MySQL卸载 1 ...
- 虚拟机与ubuntu系统的安装与基础操作
1.虚拟机的下载: 常见的虚拟机软件有:VMware VirtuaIBOX Virtual PC 等. 这里主要介绍VMware ,VMware目前已经有很多个版本,可以根据个人情况进行选择.安 ...
- 数据包报文格式(IP包、TCP报头、UDP报头)
转自: https://blog.51cto.com/lyhbwwk/2162568 一.IP包格式 IP数据包是一种可变长分组,它由首部和数据负载两部分组成.首部长度一般为20-60字节(Byte) ...
- [转]利用 Commons-Fileupload 实现文件上传
转载 Java Web开发人员可以使用Apache文件上传组件来接收浏览器上传的文件,该组件由多个类共同组成,但是,对于使用该组件来编写文件上传功能的Java Web开发人员来说,只需要了解和使用其中 ...
- 十五 OGNL的入门
一.访问对象的方法
- 吴裕雄--天生自然python数据清洗与数据可视化:MYSQL、MongoDB数据库连接与查询、爬取天猫连衣裙数据保存到MongoDB
本博文使用的数据库是MySQL和MongoDB数据库.安装MySQL可以参照我的这篇博文:https://www.cnblogs.com/tszr/p/12112777.html 其中操作Mysql使 ...
- Pytorch本人疑问(1) torch.nn和torch.nn.functional之间的区别
在写代码时发现我们在定义Model时,有两种定义方法: torch.nn.Conv2d()和torch.nn.functional.conv2d() 那么这两种方法到底有什么区别呢,我们通过下述代码看 ...