POJ 2195 - Going Home - [最小费用最大流][MCMF模板]
题目链接:http://poj.org/problem?id=2195
Time Limit: 1000MS Memory Limit: 65536K
Description
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.
Input
Output
Sample Input
2 2
.m
H.
5 5
HH..m
.....
.....
.....
mm..H
7 8
...H....
...H....
...H....
mmmHmmmm
...H....
...H....
...H....
0 0
Sample Output
2
10
28
转载请注明出处:優YoU http://blog.csdn.net/lyy289065406/article/details/6732762 大致题意:
给定一个N*M的地图,地图上有若干个man和house,且man与house的数量一致。man每移动一格需花费$1(即单位费用=单位距离),一间house只能入住一个man。现在要求所有的man都入住house,求最小费用。
构图:
把man作为一个顶点集合U,house作为另一个顶点集合V,把U中所有点到V中所有点addedge{from∈U,to∈V,cap=1,cost=两点间哈密顿距离},构成一个多源多汇的二分图。
构造一个超级源点s和超级汇点t:s与U中所有点相连,费用为0,容量为1;V中所有点与t相连,费用为0,容量为1。
这种构图思路很简单,我们建立起的二分图:
不难想象,maxflow必然等于the number of men(or the number of houses),在上图的例子中,即为3;
由于我们在所有man点与house点间建立的边容量为1,显然,最大流保证了:
①所有man都能到house里;
②一个man只进到一间house,一间house只有一个man;
就像在上图中,最大流的情况下,每个m[i]只能选一个H[j],而且i,j也不能重复;
这恰好就满足了题目所给的要求,然后,只要我们在m[i]到H[j]的边上加上cost=Hamiltonian_distance(m[i],H[j]),求出最小费用就是答案啦。
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<vector>
#include<queue>
#define MAXN 203
#define INF 0x3f3f3f3f
using namespace std;
struct Edge{
int u,v,c,f,a;
};
struct MCMF
{
int s,t;
vector<Edge> E;
vector<int> G[MAXN];
int vis[MAXN];
int d[MAXN];
int pre[MAXN];
int aug[MAXN];
void init(int l,int r)
{
for(int i=l;i<=r;i++) G[i].clear();
E.clear();
}
void addedge(int from,int to,int cap,int cost)
{
E.push_back((Edge){from,to,cap,,cost});
E.push_back((Edge){to,from,,,-cost});
int m=E.size();
G[from].push_back(m-);
G[to].push_back(m-);
}
bool SPFA(int s,int t,int &flow,int &cost)
{
memset(d,INF,sizeof(d));
memset(vis,,sizeof(vis));
d[s]=, vis[s]=, pre[s]=, aug[s]=INF;
queue<int> q;
q.push(s);
while(!q.empty())
{
int now=q.front(); q.pop();
vis[now]=;
for(int i=;i<G[now].size();i++)
{
Edge& e=E[G[now][i]];
int nex=e.v;
if(e.c>e.f && d[nex]>d[now]+e.a)
{
d[nex]=d[now]+e.a;
pre[nex]=G[now][i];
aug[nex]=min(aug[now],e.c-e.f);
if(!vis[nex])
{
q.push(nex);
vis[nex]=;
}
}
}
}
if(d[t]==INF) return ;
flow+=aug[t];
cost+=d[t]*aug[t];
for(int i=t;i!=s;i=E[pre[i]].u)
{
E[pre[i]].f+=aug[t];
E[pre[i]^].f-=aug[t];
}
return ;
} int mincost()
{
int flow=,cost=;
while(SPFA(s,t,flow,cost));
return cost;
}
}mcmf; char map[][];
struct Node{int row,col;};
int Hami_dist(Node a,Node b){return abs(a.row-b.row) + abs(a.col-b.col);} int main()
{
int N,M;//N行,M列
while(scanf("%d%d",&N,&M) && N!=)
{
vector<Node> men,houses;
for(int i=;i<=N;i++) scanf("%s",map[i]+);
for(int i=;i<=N;i++)
{
for(int j=;j<=M;j++)
{
if(map[i][j]=='m') men.push_back((Node){i,j});
if(map[i][j]=='H') houses.push_back((Node){i,j});
}
}
int n=men.size();
mcmf.init(,*n+);
mcmf.s=, mcmf.t=*n+;
for(int i=;i<n;i++)
{
mcmf.addedge(mcmf.s,i+,,);
for(int j=;j<n;j++)
{
if(i==) mcmf.addedge(j++n,mcmf.t,,);
mcmf.addedge(i+,j++n,,Hami_dist(men[i],houses[j]));
}
}
printf("%d\n",mcmf.mincost());
}
}
POJ 2195 - Going Home - [最小费用最大流][MCMF模板]的更多相关文章
- POJ 2195 Going Home 最小费用最大流 尼玛,心累
D - Going Home Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Subm ...
- poj 2195 Going Home(最小费用最大流)
题目:http://poj.org/problem?id=2195 有若干个人和若干个房子在一个给定网格中,每人走一个都要一定花费,每个房子只能容纳一人,现要求让所有人进入房子,且总花费最小. 构造一 ...
- 网络流--最小费用最大流MCMF模板
标准大白书式模板 #include<stdio.h> //大概这么多头文件昂 #include<string.h> #include<vector> #includ ...
- poj 2351 Farm Tour (最小费用最大流)
Farm Tour Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 17230 Accepted: 6647 Descri ...
- poj2135最小费用最大流经典模板题
Farm Tour Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13509 Accepted: 5125 Descri ...
- 把人都送到房子里的最小花费--最小费用最大流MCMF
题意:http://acm.hdu.edu.cn/showproblem.php?pid=1533 相邻的容量为inf,费用为1,S到m容量为1,费用为0 ,H到T容量为1,费用为0. 建图跑-最小费 ...
- POJ 2157 Evacuation Plan [最小费用最大流][消圈算法]
---恢复内容开始--- 题意略. 这题在poj直接求最小费用会超时,但是题意也没说要求最优解. 根据线圈定理,如果一个跑完最费用流的残余网络中存在负权环,那么顺着这个负权环跑流量为1那么会得到更小的 ...
- poj 2135 Farm Tour 最小费用最大流建图跑最短路
题目链接 题意:无向图有N(N <= 1000)个节点,M(M <= 10000)条边:从节点1走到节点N再从N走回来,图中不能走同一条边,且图中可能出现重边,问最短距离之和为多少? 思路 ...
- POJ 3680: Intervals【最小费用最大流】
题目大意:你有N个开区间,每个区间有个重量wi,你要选择一些区间,使得满足:每个点被不超过K个区间覆盖的前提下,重量最大 思路:感觉是很好想的费用流,把每个区间首尾相连,费用为该区间的重量的相反数(由 ...
随机推荐
- C# winform开发嵌套Chrome内核浏览器(WebKit.net)开发(一)
https://www.cnblogs.com/Maxq/p/6566558.html WebKit.net是对WebKit的.Net封装, 使用它.net程序可以非常方便的集成和使用webkit作为 ...
- Okhttp封装、网络层扩展
一.概述 首先在这里本片文章是以网络通信封装为主,而app开发首先重要就是网络通信,而如今主流的async.volley.okhttp等,阿么这么网络库怎样能做到更好封装.更好的切换,从而不影响业务层 ...
- EasyHook实现
using System; using System.Runtime.InteropServices; using System.Windows.Forms; using System.Collect ...
- WebService之JDK中wsimport命令
1.编写WebService类,使用@WebService注解 package test; import javax.jws.WebService; @WebService public class ...
- 【RF库测试】算法运算
- C++标准程序库笔记之一
本篇博客笔记顺序大体按照<C++标准程序库(第1版)>各章节顺序编排. ---------------------------------------------------------- ...
- java web当中表单提交到后台出现乱码的解决方法
1.如果提交方式为post,想不乱码,只需要在服务器端设置request对象的编码即可,客户端以哪种编码提交的,服务器端的request对象就以对应的编码接收,比如客户端是以UTF-8编码提交的,那么 ...
- Jquery-无法有效获取当前窗口高度
今天碰到个很奇怪的事情,那就是滚动条往下滚动时候没有触发提示,反而是往上滚动的时候,触发了提示.百思不得其解,尤其是拿了美工大大的切图过来,一点问题都没有. 那么就进行console.log输出查看了 ...
- 【EF框架】使用params参数传值防止SQL注入报错处理
通过SqlParameter传时间参数,代码如下: var param = new List<SqlParameter>(); param.Add(new SqlParameter(&qu ...
- Esper学习之十:EPL语法(六)
在esper的文档中,epl访问数据库的配置放在了比较靠后的位置,不过为了方便各位学习,这里会先说明和数据库交互的相关配置,然后再说epl怎么访问数据库. 配置文件在官方esper包的etc文件夹下, ...