POJ2195&&HDU1533(KB11-D 最小费用最大流)
Going Home
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 23515 | Accepted: 11853 |
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
Source
//2017-08-24
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>
#include <cmath> using namespace std; const int N = ;
const int M = ;
const int INF = 0x3f3f3f3f;
int head[N], tot;
struct Edge{
int to, next, c, w;//c为容量,w为单位费用
}edge[M]; void add_edge(int u, int v, int c, int w){
edge[tot].c = c;
edge[tot].w = w;
edge[tot].to = v;
edge[tot].next = head[u];
head[u] = tot++; edge[tot].c = ;
edge[tot].w = -w;
edge[tot].to = u;
edge[tot].next = head[v];
head[v] = tot++;
} bool vis[N];
int pre[N], dis[N];//pre记录路径,dis记录到源点的最小花费
struct MinCostMaxFlow{
int S, T;
int flow, cost;
void init(int _S, int _T){
S = _S;
T = _T;
tot = ;
memset(head, -, sizeof(head));
}
bool spfa(){
memset(vis, , sizeof(vis));
memset(dis, INF, sizeof(dis));
dis[S] = ;
vis[S] = ;
queue<int> que;
que.push(S);
while(!que.empty()){
int u = que.front();
que.pop();
for(int i = head[u]; i != -; i = edge[i].next){
int v = edge[i].to;
if(edge[i].c > && dis[v] > dis[u]+edge[i].w){
dis[v] = dis[u] + edge[i].w;
pre[v] = i;
if(!vis[v]){
vis[v] = true;
que.push(v);
}
}
}
vis[u] = ;
}
return dis[T] != INF;
}
int dfs(int &flow){
int u, p, sum = INF, ans = ;
for(u = T; u != S; u = edge[p^].to){
//记录路径上的最小流值
p = pre[u];
sum = min(sum, edge[p].c);
}
for(u = T; u != S; u = edge[p^].to){
p = pre[u];
edge[p].c -= sum;
edge[p^].c += sum;
ans += sum*edge[p].w;
}
flow += sum;
return ans;
}
int maxflow(){
cost = , flow = ;
while(spfa()){//寻找增广路并增广
cost += dfs(flow);
}
return cost;
}
}mcmf; string grid[N];
int x[N], y[N]; int main()
{
std::ios::sync_with_stdio(false);
//freopen("inputD.txt", "r", stdin);
int n, m;
while(cin>>n>>m && (n || m)){
int cnt_m = , cnt_h = ;
int s = N-, t = N-;
mcmf.init(s, t);
for(int i = ; i < n; i++){
cin>>grid[i];
for(int j = ; j < m; j++){
if(grid[i][j] == 'H'){
add_edge(s, cnt_h, , );
x[cnt_h] = i;
y[cnt_h++] = j;
}
}
}
for(int i = ; i < n; i++){
for(int j = ; j < m; j++){
if(grid[i][j] == 'm'){
add_edge(cnt_h+cnt_m, t, , );
for(int k = ; k < cnt_h; k++){
add_edge(k, cnt_h+cnt_m, , abs(i-x[k])+abs(j-y[k]));
}
cnt_m++;
}
}
}
cout<<mcmf.maxflow()<<endl;
} return ;
}
POJ2195&&HDU1533(KB11-D 最小费用最大流)的更多相关文章
- hdu1533 Going Home 最小费用最大流 构造源点和汇点
Going Home Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- POJ2195 Going Home —— 最大权匹配 or 最小费用最大流
题目链接:https://vjudge.net/problem/POJ-2195 Going Home Time Limit: 1000MS Memory Limit: 65536K Total ...
- POJ2195:Going Home (最小费用最大流)
Going Home Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 26212 Accepted: 13136 题目链接 ...
- [hdu1533]二分图最大权匹配 || 最小费用最大流
题意:给一个n*m的地图,'m'表示人,'H'表示房子,求所有人都回到房子所走的距离之和的最小值(距离为曼哈顿距离). 思路:比较明显的二分图最大权匹配模型,将每个人向房子连一条边,边权为曼哈顿距离的 ...
- 最小费用最大流 POJ2195-Going Home
网络流相关知识参考: http://www.cnblogs.com/luweiseu/archive/2012/07/14/2591573.html 出处:優YoU http://blog.csdn. ...
- POJ 2195 Going Home 【最小费用最大流】
题目链接:http://poj.org/problem?id=2195 Time Limit: 1000MS Memory Limit: 65536K Total Submissions:2715 ...
- [板子]最小费用最大流(Dijkstra增广)
最小费用最大流板子,没有压行.利用重标号让边权非负,用Dijkstra进行增广,在理论和实际上都比SPFA增广快得多.教程略去.转载请随意. #include <cstdio> #incl ...
- bzoj1927最小费用最大流
其实本来打算做最小费用最大流的题目前先来点模板题的,,,结果看到这道题二话不说(之前打太多了)敲了一个dinic,快写完了发现不对 我当时就这表情→ =_=你TM逗我 刚要删突然感觉dinic的模 ...
- ACM/ICPC 之 卡卡的矩阵旅行-最小费用最大流(可做模板)(POJ3422)
将每个点拆分成原点A与伪点B,A->B有两条单向路(邻接表实现时需要建立一条反向的空边,并保证环路费用和为0),一条残留容量为1,费用为本身的负值(便于计算最短路),另一条残留容量+∞,费用为0 ...
- HDU5900 QSC and Master(区间DP + 最小费用最大流)
题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5900 Description Every school has some legends, ...
随机推荐
- sed 横排扩展
sed "$!N;s/\n/KEY/" FILENAME
- pm2 官方文档 学习笔记
一.安装 1.安装 npm install pm2 -g 2.更新 npm install pm2 -g && pm2 update pm2 update 是为了刷新 PM2 的守护进 ...
- JS获取浏览器URL中查询字符串的参数
首先要知道Location这个对象以及这个对象中的一些属性: href:设置或返回完整的url.如本博客首页返回http://www.cnblogs.com/wymninja/ host:设置或返回主 ...
- JAVA虚拟机体系结构JAVA虚拟机的生命周期
一个运行时的Java虚拟机实例的天职是:负责运行一个java程序.当启动一个Java程序时,一个虚拟机实例也就诞生了.当该程序关闭退出,这个虚拟机实例也就随之消亡.如果同一台计算机上同时运行三个Jav ...
- Java Web(四) 一次性验证码的代码实现
其实实现代码的逻辑非常简单,真的超级超级简单. 1.在登录页面上login.jsp将验证码图片使用标签<img src="xxx">将绘制验证码图片的url给它 2.在 ...
- POJ 2491
#include<iostream>#include<stdio.h>#include<string>#define MAXN 400using namespace ...
- div在页面垂直居中方法---增强改进版
div在页面垂直居中方法---改进版 .wrap{ background: #ffffff; position:absolute; margin:auto; top:; bottom:; left:; ...
- windows 系统安装git的方法
windows 系统安装git的方法 msysgit是Windows版的Git,从https://git-for-windows.github.io下载 安装默认步骤,一步步安装即可 安装完成后,在开 ...
- Android从零开始
Android开发环境的安装 1 IDE Android可以使用开发的IDE有Eclipse 或者 Android Studio.Android Studio还处于v 0.1.x版本,是early a ...
- dubbo-001--前言
1,前言: 第一次了解dubbo是在15年刚工作的时候在瑞友,项目是给凯撒旅游公司签证部做一个签证系统,项目需要调用一些凯撒的方法,比如权限.部门信息,跟签证相关的一些如签证国家城市.领区啥玩意的,等 ...