POJ 2195 Going Home(费用流)
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(费用流)的更多相关文章
- poj - 2195 Going Home (费用流 || 最佳匹配)
http://poj.org/problem?id=2195 对km算法不理解,模板用的也不好. 下面是大神的解释. KM算法的要点是在相等子图中寻找完备匹配,其正确性的基石是:任何一个匹配的权值之和 ...
- 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 ...
- POJ 2175 Evacuation Plan (费用流,负环,消圈法,SPFA)
http://poj.org/problem?id=2175 Evacuation Plan Time Limit: 1000MS Memory Limit: 65536K Total Submi ...
- POJ 2516 Minimum Cost (费用流)
题面 Dearboy, a goods victualer, now comes to a big problem, and he needs your help. In his sale area ...
- 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 ...
- POJ 3680 Intervals(费用流)
Intervals Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 5762 Accepted: 2288 Descrip ...
- POJ 2175 Evacuation Plan 费用流 负圈定理
题目给了一个满足最大流的残量网络,判断是否费用最小. 如果残量网络中存在费用负圈,那么不是最优,在这个圈上增广,增广1的流量就行了. 1.SPFA中某个点入队超过n次,说明存在负环,但是这个点不一定在 ...
- POJ 3680 Intervals(费用流+负权优化)
[题目链接] http://poj.org/problem?id=3680 [题目大意] 有N个带权重的区间,现在要从中选取一些区间, 要求任意点都不被超过K个区间所覆盖,请最大化总的区间权重. [题 ...
- poj 2135 Farm Tour 费用流
题目链接 给一个图, N个点, m条边, 每条边有权值, 从1走到n, 然后从n走到1, 一条路不能走两次,求最短路径. 如果(u, v)之间有边, 那么加边(u, v, 1, val), (v, u ...
- BZOJ3502PA2012Tanie linie&BZOJ2288[POJ Challenge]生日礼物——模拟费用流+链表+堆
题目描述 n个数字,求不相交的总和最大的最多k个连续子序列. 1<= k<= N<= 1000000. 输入 输出 样例输入 5 2 7 -3 4 -9 5 样例输出 13 根据 ...
随机推荐
- hdu4028 The time of a day[map优化dp]
The time of a day Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others ...
- glassfish任意文件读取漏洞解析
一.背景: glassfish是一款java编写的跨平台的开源的应用服务器. 二.漏洞原理: 与宽字节SQL注入一致,都是由于unicode编码歧义导致的.具体payload如下构造: http:// ...
- [转帖]双剑合璧:CPU+GPU异构计算完全解析
引用自:http://tech.sina.com.cn/mobile/n/2011-06-20/18371792199.shtml 这篇文章写的深入浅出,把异构计算的思想和行业趋势描述的非常清楚,难得 ...
- java如何随机生成定长的字符串
小数,字符串.时间等示例代码 String base = abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 public c ...
- 无法远程访问Mysql
1.故障状态 [root@server02 ~]# mysql -utuser -h192. -p Enter password: ERROR (HY000): Can't connect to My ...
- Thinkphp --- 路由定义
thinkPHP的路由: thinkphp下的 conf 下可以进行配置:(154行) /* 系统变量名称设置 */ 'VAR_MODULE' => 'm', // 默认模块获取变量 'VAR_ ...
- 【php】---mysql---基本操作及使用---【巷子】
1.数据库简介 (1).什么是数据库? 一个文件 一个文件夹 一个u盘 一个硬盘......都叫做数据库 存放数据的仓库 (2).常见的数据库? mySql sql ...
- java8新增的日期时间包
Clock clock=Clock.systemUTC(); System.out.println("当前时刻为:"+clock.instant()); System.out.pr ...
- vue 学习报错 Newline required at end of file but not found
着不敢了,原因竟然是需要在js css等后面再加一行(空行) Newline required at end of file but not found 翻译:文件末尾需要换行符,但找不到 如下面两处 ...
- K-均值聚类(K-means)算法
https://www.cnblogs.com/ybjourney/p/4714870.html 最近在看<机器学习实战>这本书,因为自己本身很想深入的了解机器学习算法,加之想学pytho ...