http://poj.org/problem?id=2195

题意:

在一个网格地图上,有n个小人和n栋房子。在每个时间单位内,每个小人可以往水平方向或垂直方向上移动一步,走到相邻的方格中。对每个小人,每走一步需要支付1美元,直到他走入到一栋房子里。每栋房子只能容纳一个小人。

计算出让n个小人移动到n个不同的房子需要支付的最小费用。

思路:

源点和每个人相连,容量为1,费用为0。

汇点和每栋房子相连,容量为1,费用为0。

每个人和每栋房子相连,容量为1,费用为人和房子之间的距离。

这样一来,跑一遍费用流即可。

 #include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<sstream>
#include<vector>
#include<stack>
#include<queue>
#include<cmath>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
typedef long long ull;
typedef pair<int,int> pll;
const int INF = 0x3f3f3f3f;
const int maxn = + ; int n, m, k; struct Edge
{
int from, to, cap, flow, cost;
Edge(int u, int v, int c, int f, int w) :from(u), to(v), cap(c), flow(f), cost(w) {}
}; struct MCMF
{
int n, m;
vector<Edge> edges;
vector<int> G[maxn];
int inq[maxn];
int d[maxn];
int p[maxn];
int a[maxn]; void init(int n)
{
this->n = n;
for (int i = ; i<n; i++) G[i].clear();
edges.clear();
} void AddEdge(int from, int to, int cap, int cost)
{
edges.push_back(Edge(from, to, cap, , cost));
edges.push_back(Edge(to, from, , , -cost));
m = edges.size();
G[from].push_back(m - );
G[to].push_back(m - );
} bool BellmanFord(int s, int t, int &flow, int & cost)
{
for (int i = ; i<n; i++) d[i] = INF;
memset(inq, , sizeof(inq));
d[s] = ; inq[s] = ; p[s] = ; a[s] = INF; queue<int> Q;
Q.push(s);
while (!Q.empty()){
int u = Q.front(); Q.pop();
inq[u] = ;
for (int i = ; i<G[u].size(); i++){
Edge& e = edges[G[u][i]];
if (e.cap>e.flow && d[e.to]>d[u] + e.cost){
d[e.to] = d[u] + e.cost;
p[e.to] = G[u][i];
a[e.to] = min(a[u], e.cap - e.flow);
if (!inq[e.to]) { Q.push(e.to); inq[e.to] = ; }
}
}
} if (d[t] == INF) return false;
flow += a[t];
cost += d[t] * a[t];
for (int u = t; u != s; u = edges[p[u]].from)
{
edges[p[u]].flow += a[t];
edges[p[u] ^ ].flow -= a[t];
}
return true;
} int MincostMaxdflow(int s, int t){
int flow = , cost = ;
while (BellmanFord(s, t, flow, cost));
return cost;
}
}t; struct node
{
int x, y;
}people[maxn],house[maxn]; int main()
{
//freopen("in.txt","r",stdin);
while(~scanf("%d%d",&n,&m) && n && m)
{
char c;
int cnt_p=, cnt_h=;
for(int i=;i<n;i++)
{
for(int j=;j<m;j++)
{
cin>>c;
if(c=='H') {house[++cnt_h].x=i;house[cnt_h].y=j;}
else if(c=='m') {people[++cnt_p].x=i;people[cnt_p].y=j;}
}
} int n=cnt_h;
int src=, dst=*n+;
t.init(dst+); for(int i=;i<=cnt_p;i++) t.AddEdge(src,i,,);
for(int i=;i<=cnt_h;i++) t.AddEdge(n+i,dst,,); for(int i=;i<=cnt_p;i++)
{
for(int j=;j<=cnt_h;j++)
{
int dis=abs(people[i].x-house[j].x)+abs(people[i].y-house[j].y);
t.AddEdge(i,n+j,,dis);
}
} printf("%d\n",t.MincostMaxdflow(src,dst));
}
return ;
}

POJ 2195 Going Home(费用流)的更多相关文章

  1. poj - 2195 Going Home (费用流 || 最佳匹配)

    http://poj.org/problem?id=2195 对km算法不理解,模板用的也不好. 下面是大神的解释. KM算法的要点是在相等子图中寻找完备匹配,其正确性的基石是:任何一个匹配的权值之和 ...

  2. POJ 2195 Going Home (费用流)

    题面 On a grid map there are n little men and n houses. In each unit time, every little man can move o ...

  3. POJ 2175 Evacuation Plan (费用流,负环,消圈法,SPFA)

    http://poj.org/problem?id=2175 Evacuation Plan Time Limit: 1000MS   Memory Limit: 65536K Total Submi ...

  4. POJ 2516 Minimum Cost (费用流)

    题面 Dearboy, a goods victualer, now comes to a big problem, and he needs your help. In his sale area ...

  5. Going Home POJ - 2195 (最小费用最大流)

    On a grid map there are n little men and n houses. In each unit time, every little man can move one ...

  6. POJ 3680 Intervals(费用流)

    Intervals Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 5762   Accepted: 2288 Descrip ...

  7. POJ 2175 Evacuation Plan 费用流 负圈定理

    题目给了一个满足最大流的残量网络,判断是否费用最小. 如果残量网络中存在费用负圈,那么不是最优,在这个圈上增广,增广1的流量就行了. 1.SPFA中某个点入队超过n次,说明存在负环,但是这个点不一定在 ...

  8. POJ 3680 Intervals(费用流+负权优化)

    [题目链接] http://poj.org/problem?id=3680 [题目大意] 有N个带权重的区间,现在要从中选取一些区间, 要求任意点都不被超过K个区间所覆盖,请最大化总的区间权重. [题 ...

  9. poj 2135 Farm Tour 费用流

    题目链接 给一个图, N个点, m条边, 每条边有权值, 从1走到n, 然后从n走到1, 一条路不能走两次,求最短路径. 如果(u, v)之间有边, 那么加边(u, v, 1, val), (v, u ...

  10. BZOJ3502PA2012Tanie linie&BZOJ2288[POJ Challenge]生日礼物——模拟费用流+链表+堆

    题目描述 n个数字,求不相交的总和最大的最多k个连续子序列. 1<= k<= N<= 1000000. 输入 输出 样例输入 5 2 7 -3 4 -9 5 样例输出 13   根据 ...

随机推荐

  1. video事件

    /** video播放器*/ * @ src: 指定所要嵌入视频.文档的URL. * @ poster: 视频预览图像 * @ autoplay: 视频自动播放 * @ loop: 循环播放 * @ ...

  2. oneThink发生错误,获取当前执行的SQL语句!

    echo D('AnswerInfoView')->getLastSql();die();

  3. Android官方架构组件介绍之LiveData

    LiveData LiveData是一个用于持有数据并支持数据可被监听(观察).和传统的观察者模式中的被观察者不一样,LiveData是一个生命周期感知组件,因此观察者可以指定某一个LifeCycle ...

  4. SQL取某个字符串最后一次出现的位置后面的字符串方法

    --sql怎么取某个字符串最后一次出现的位置后面的字符串 declare @s varchar(max); set @s = 'fj/2016815/2016081553677565.pdf'; se ...

  5. codeforces#505--C Plasticine Zebra

    C. Plasticine zebra time limit per test 1 second memory limit per test 256 megabytes input standard ...

  6. C/C++ 开放库

    C/C++ 开放库 1.Best C/C++ Network Library 2.A list of open source C++ libraries

  7. Django - ORM - 进阶

    一.多表操作 创建模型 实例:我们来假定下面这些概念,字段和关系 作者模型:一个作者有姓名和年龄. 作者详细模型:把作者的详情放到详情表,包含生日,手机号,家庭住址等信息.作者详情模型和作者模型之间是 ...

  8. Air Raid---hdu1151(最小路径覆盖)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1151 最小路径覆盖 == 顶点数 - 最大匹配. #include<stdio.h> #i ...

  9. 一个用于提取简体中文字符串中省,市和区并能够进行映射,检验和简单绘图的python模块

    简介 一个用于提取简体中文字符串中省,市和区并能够进行映射,检验和简单绘图的python模块. 举个例子: ["徐汇区虹漕路461号58号楼5楼", "泉州市洛江区万安塘 ...

  10. scrapy之定制命令

    单爬虫运行 import sys from scrapy.cmdline import execute if __name__ == '__main__': execute(["scrapy ...