POJ-2195(最小费用最大流+MCMF算法)
Going Home
POJ-2195
- 这题使用的是最小费用流的模板。
- 建模的时候我的方法出现错误,导致出现WA,根据网上的建图方法没错。
- 这里的建图方法是每次到相邻点的最大容量为INF,而花费为1,因为花费等于距离。但是需要增加一个源点和一个汇点,然后将每个人和源点相连,每个房子和汇点相连,容量都为1,费用都为0.
#include<iostream>
#include<algorithm>
#include<cstring>
#include<queue>
#include<stack>
#include<cstdio>
using namespace std;
#define INF 0x3f3f3f3f
#define M(a, b) memset(a, b, sizeof(a))
const int N = 1e4 + 5;
const int dx[] = {-1, 1, 0, 0};
const int dy[] = {0, 0, -1, 1};
int n, m;
struct Edge {
int from, to, cap, flow, cost;
};
struct MCMF {
int n, m;
vector<Edge> edges;
vector<int> G[N];
int d[N], inq[N], p[N], a[N];
void init(int n) {
this->n = n;
for (int i = 0; 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, 0, cost});
edges.push_back(Edge{to, from, 0, 0, -cost});
m = edges.size();
G[from].push_back(m-2); G[to].push_back(m-1);
}
bool spfa(int s, int t, int &flow, int &cost) {
M(inq, 0); M(d, INF);
d[s] = 0; inq[s] = 1; p[s] = 0; a[s] = INF;
queue<int> q;
q.push(s);
while (!q.empty()) {
int x = q.front(); q.pop();
inq[x] = 0;
for (int i = 0; i < G[x].size(); ++i) {
Edge &e = edges[G[x][i]];
if (d[e.to] > d[x] + e.cost && e.cap > e.flow) {
d[e.to] = d[x] + e.cost;
p[e.to] = G[x][i];
a[e.to] = min(a[x], e.cap-e.flow);
if (inq[e.to]) continue;
q.push(e.to); inq[e.to] = 1;
}
}
}
if (d[t] == INF) return false;
flow += a[t];
cost += d[t] * a[t];
int u = t;
while (u != s) {
edges[p[u]].flow += a[t];
edges[p[u]^1].flow -= a[t];
u = edges[p[u]].from;
}
return true;
}
int Mincost(int s, int t) {
int flow = 0, cost = 0;
while (spfa(s, t, flow, cost));
return cost;
}
}solver;
char str[205][205];
bool check(int x, int y) {
if (x>0 && x<=n && y>0 && y<=m) return 1;
return 0;
}
struct node{
int x;
int y;
int num;
};
node men[N];
node hou[N];
char ma[N][N];
int main() {
while (scanf("%d%d", &n, &m), n&&m) {
solver.init(n*m+1);
for (int i = 1; i <= n; ++i) {
scanf("%s", str[i]+1);
for (int j = 1; j <= m; ++j) {
if (str[i][j]=='H') solver.AddEdge((i-1)*m+j, n*m+1, 1, 0);
if (str[i][j]=='m') solver.AddEdge(0, (i-1)*m+j, 1, 0);
for (int k = 0; k < 4; ++k) {
int nx = i+dx[k], ny = j+dy[k];
if (check(nx, ny)) solver.AddEdge((i-1)*m+j, (nx-1)*m+ny, INF, 1);
}
}
}
printf("%d\n", solver.Mincost(0, n*m+1));
//--------------------------------version2
// int n1=201;
// solver.init(2*n1+1);
// int ansh=n,k2=0;
// int ansm=0,k1=0;
// for(int i=0;i<n;i++){
// scanf("%s", ma[i]);
// for(int j=0;j<m;j++){
// if(ma[i][j]=='m'){//人
// ++ansm;
// men[k1].x=i;
// men[k1].y=j;
// men[k1++].num=ansm;
// solver.AddEdge(0,ansm,1,0);
// }else if(ma[i][j]=='H'){
// ++ansh;
// hou[k2].x=i;
// hou[k2].y=j;
// hou[k2++].num=ansh;
// solver.AddEdge(ansh,2*n1+1,1,0);
// }
// }
// }
// for(int i=0;i<k1;i++){
// for(int j=0;j<k2;j++){
// int disc=abs(men[i].x-hou[j].x)+abs(men[i].y-hou[j].y);
// solver.AddEdge(men[i].num,hou[j].num,1,disc);
// }
// }
// printf("%d\n", solver.Mincost(0, n1*2+1));
}
return 0;
}
POJ-2195(最小费用最大流+MCMF算法)的更多相关文章
- poj 2195 最小费用最大流模板
/*Source Code Problem: 2195 User: HEU_daoguang Memory: 1172K Time: 94MS Language: G++ Result: Accept ...
- POJ - 2195 最小费用最大流
题意:每个人到每个房子一一对应,费用为曼哈顿距离,求最小的费用 题解:单源点汇点最小费用最大流,每个人和房子对于建边 #include<map> #include<set> # ...
- POJ-2516(最小费用最大流+MCMF算法)
Minimum Cost POJ-2516 题意就是有n个商家,有m个供货商,然后有k种商品,题目求的是满足商家的最小花费供货方式. 对于每个种类的商品k,建立一个超级源点和一个超级汇点.每个商家和源 ...
- POJ 2516 最小费用最大流
每一种货物都是独立的,分成k次最小费用最大流即可! 1: /** 2: 因为e ==0 所以 pe[v] pe[v]^1 是两条相对应的边 3: E[pe[v]].c -= aug; E[pe[v]^ ...
- poj 3422(最小费用最大流)
题目链接:http://poj.org/problem?id=3422 思路:求从起点到终点走k次获得的最大值,最小费用最大流的应用:将点权转化为边权,需要拆点,边容量为1,费用为该点的点权,表示该点 ...
- 把人都送到房子里的最小花费--最小费用最大流MCMF
题意:http://acm.hdu.edu.cn/showproblem.php?pid=1533 相邻的容量为inf,费用为1,S到m容量为1,费用为0 ,H到T容量为1,费用为0. 建图跑-最小费 ...
- POJ 2135 最小费用最大流 入门题
Farm Tour Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19207 Accepted: 7441 Descri ...
- POJ 2195 - Going Home - [最小费用最大流][MCMF模板]
题目链接:http://poj.org/problem?id=2195 Time Limit: 1000MS Memory Limit: 65536K Description On a grid ma ...
- poj 2135最小费用最大流
最小费用最大流问题是经济学和管理学中的一类典型问题.在一个网络中每段路径都有"容量"和"费用"两个限制的条件下,此类问题的研究试图寻找出:流量从A到B,如何选择 ...
随机推荐
- codeforces626D . Jerry's Protest (概率)
Andrew and Jerry are playing a game with Harry as the scorekeeper. The game consists of three rounds ...
- tju3243 Blocked Road
There are N seaside villages on X island, numbered from 1 to N. N roads are built to connect all of ...
- hdu3669 Cross the Wall
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 327680/327680 K (Java/Others) Total Submissi ...
- Java基础(第一期)
Java基础 1.注释 Java中注释有三种: 单行注释 // 多行注释 /* */ 文本注释(用的较少) /** */ 书写注释是一个非常好的习惯 BAT 平时写代码一定要注意规范 //有趣的代码注 ...
- 洛谷 P4143 采集矿石 后缀数组
题目背景 ZRQ 成功从坍塌的洞穴中逃了出来.终于,他看到了要研究的矿石.他想挑一些带回去完成任务. 题目来源:Zhang_RQ哦对了 \(ZRQ\) 就他,嗯 题目描述 ZRQ 发现这里有 \(N\ ...
- 微服务架构Day02-SpringBoot日志slf4j
日志框架 日志门面(接口,日志抽象层 ) 日志实现 JCL(Jakarta Commons Logging).slf4j(Simple Logging Facade for Java).jboss-l ...
- scu-4445
Right turn frog is trapped in a maze. The maze is infinitely large and divided into grids. It also c ...
- Leetcode(20)-有效的括号
给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效. 有效字符串需满足: 左括号必须用相同类型的右括号闭合. 左括号必须以正确的顺序闭合. 注意空字符串可被认 ...
- js optional chaining operator
js optional chaining operator js 可选链 可选链操作符( ?. )允许读取位于连接对象链深处的属性的值,而不必明确验证链中的每个引用是否有效. ?. 操作符的功能类似于 ...
- OOP & 模块化, 多态, 封装
OOP 面向对象编程 (OOP) 是用抽象方式创建基于现实世界模型的一种编程模式.它使用先前建立的范例,包括模块化,多态和封装几种技术. 在 OOP 中,每个对象能够接收消息,处理数据和发送消息给其他 ...