hdu 1533 Going Home 最小费用最大流
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1533
On 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.
题意描述:在n*m的矩阵格子里有x个人要走到x个房间里,每个房间里只能放一人,人在目前所在的格子处向上下左右相邻的格子走一步就花费1美元,问最后全部的人走到房间后最小的花费。
算法分析:这道题可以用KM算法或者最小费用最大流算法解之,这里讲解一下最小费用最大流的方法。
新增源点from和汇点to,from->人(w为1,cost为0)
房子->to(w为1,cost为0)
每个人->每间房(w为1,cost为最短路径的美元花费)
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<vector>
#include<queue>
#define inf 0x7fffffff
using namespace std;
const int maxn=+;
const int M = +; int n,m,from,to;
struct node
{
int v,flow,cost;
int next;
}edge[M*];
int head[maxn],edgenum;
int dis[maxn],pre[maxn],pid[maxn],vis[maxn]; void add(int u,int v,int flow,int cost)
{
edge[edgenum].v=v ;edge[edgenum].flow=flow ;
edge[edgenum].cost=cost ;edge[edgenum].next=head[u];
head[u]=edgenum++; edge[edgenum].v=u ;edge[edgenum].flow=;
edge[edgenum].cost=-cost ;edge[edgenum].next=head[v];
head[v]=edgenum++;
} int spfa()
{
for (int i= ;i<=to ;i++)
{
dis[i]=inf;
vis[i]=;
}
queue<int> Q;
Q.push(from);
dis[from]=;
vis[from]=;
while (!Q.empty())
{
int u=Q.front() ;Q.pop() ;
vis[u]=;
for (int i=head[u] ;i!=- ;i=edge[i].next)
{
int v=edge[i].v;
if (edge[i].flow> && dis[v]>dis[u]+edge[i].cost)
{
dis[v]=dis[u]+edge[i].cost;
pre[v]=u;
pid[v]=i;
if (!vis[v])
{
vis[v]=;
Q.push(v);
}
}
}
}
return dis[to];
} int mincost()
{
int aug=,maxflow=;
int ans=,tmp=;
while ()
{
tmp=spfa();
if (tmp==inf) break;
aug=inf;
for (int i=to ;i!=from ;i=pre[i])
{
if (edge[pid[i] ].flow<aug)
aug=edge[pid[i] ].flow;
}
for (int i=to ;i!=from ;i=pre[i])
{
edge[pid[i] ].flow -= aug;
edge[pid[i]^ ].flow += aug;
}
maxflow += aug;
ans += tmp;
}
return ans;
} int main()
{
while (scanf("%d%d",&n,&m)!=EOF)
{
if (!n && !m) break;
memset(head,-,sizeof(head));
edgenum=;
char str[][];
memset(str,,sizeof(str));
for (int i= ;i<=n ;i++)
{
scanf("%s",str[i]+);
}
from=n*m+;
to=from+;
int h[],c[],cnt=;
int h2[],c2[],cnt2=;
for (int i= ;i<=n ;i++)
{
for (int j= ;j<=m ;j++)
{
if (str[i][j]=='m')
{
add(from,(i-)*m+j,,);
h[cnt]=i ;c[cnt]=j ;cnt++ ;
}
else if (str[i][j]=='H')
{
add((i-)*m+j,to,,);
h2[cnt2]=i ;c2[cnt2]=j ;cnt2++ ;
}
}
}
for (int i= ;i<cnt ;i++)
{
for (int j= ;j<cnt2 ;j++)
add((h[i]-)*m+c[i],(h2[j]-)*m+c2[j],,abs(h[i]-h2[j])+abs(c[i]-c2[j]));
}
printf("%d\n",mincost());
}
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 最小费用最大流 (模板题)
Going Home Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- HDU 5988.Coding Contest 最小费用最大流
Coding Contest Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)To ...
- hdu 3667(拆边+最小费用最大流)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3667 思路:由于花费的计算方法是a*x*x,因此必须拆边,使得最小费用流模板可用,即变成a*x的形式. ...
- hdu 3488(KM算法||最小费用最大流)
Tour Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total Submis ...
- hdu 3395(KM算法||最小费用最大流(第二种超级巧妙))
Special Fish Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- HDU–5988-Coding Contest(最小费用最大流变形)
Coding Contest Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)To ...
- hdu 1853 Cyclic Tour 最小费用最大流
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1853 There are N cities in our country, and M one-way ...
随机推荐
- Ubuntu CTRL+ALT+F1~F6 进入命令模式后不支持中文显示的解决办法
前言 我在实验进入linux系统启动xwindow server而不启动KDE GNOME等桌面系统时遇到的问题.只启动x server而不启动桌面系统,在xserver之上运行一个全屏的图形界面程序 ...
- php去除换行(回车换行)的方法
php去除换行(回车换行)的三种方法. 代码: <?php //php 不同系统的换行 //不同系统之间换行的实现是不一样的 //linux 与unix中用 \n //MAC ...
- C++ string 转 char*
string 转到 char* char name[20]; string sname=GatherName[n]; strcpy(name,sname.c_str());
- CodeBlocks集成cppcheck
From:http://www.cnblogs.com/killerlegend/p/3624117.html Writer:KillerLegend CodeBlocks本身配置了cppcheck的 ...
- 【easyui】--普通js中获取easyui中分页信息(page,pageSize等)
对于datagrid,获取其分页信息: 方法: var pageopt = $('#list_data').datagrid('getPager').data("pagination&quo ...
- sendBroadcast 无法接收
项目中遇到已经sendbroadcast,但是在对应的BroadcastReceiver中却无法调用onReceiver 真是个纠结的问题.找了许久. 终于发现Intent中传递了一个参数(自定义类继 ...
- devexpress 控制面板汉化方式 参考信息
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- 小心C语言的定义与声明
小心C语言的定义与声明 转自360博客 注:为便于说明问题,文中提及的变量和函数都被简化. 一.起源 DBProxy在测试过程中,发现对其执行某步管理操作后,程序有时会崩溃,但不是每次都出现. 二.G ...
- .NET开源工作流RoadFlow-流程设计-流程步骤设置-数据设置
数据设置是控制在流程处理过程中,当前步骤的数据显示与编辑状态,控制当前步骤哪些字段为只读,隐藏或可编辑.需要配合表单设计器使用.
- android 连续点击退出程序
package com.test.twiceexit; import java.util.Timer; import android.app.Activity;import android.os.Bu ...