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, ...
随机推荐
- 【062有新题】OCP 12c 062出现大量之前没有的新考题-16
choose one Which users are created and can be used for database and host management of your DBaaS da ...
- django 中 Oauth2 实现第三方登陆
django 中 Oauth2 实现第三方登陆 python网站第三方登录,social-auth-app-django模块, social-auth-app-django模块是专门用于Django的 ...
- java String 内存模型
关于java的内存模型,参照以下的一篇文章: https://isudox.com/2016/06/22/memory-model-of-string-in-java-language/
- BitArray源码解析
BitArray是C# System.Collections内置的集合,用于帮助进行位运算. BitArray的使用示例 // 创建两个大小为 8 的点阵列 BitArray ba1 = new Bi ...
- Linux 下创建 sftp 用户并限定目录
Linux 下创建 sftp 用户并限定目录 1.创建 sftpUser 用户组 [root@XXX ~]# groupadd sftpUser 2.创建 sftpUser 用户并指定目录 [root ...
- DockPanel与GeckoFX、ChrominumFX、CefSharp结合使用问题
在使用DockPanel与ChrominumFx时,当在以下条件下拖动窗体时,会发生ChromiumWebBrowser崩溃的情况,此种情况也会在DockPanel与GeckoFX或CefSharp结 ...
- 通过修改CR0寄存器绕过SSDT驱动保护
为了安全起见,Windows XP及其以后的系统将一些重要的内存页设置为只读属性,这样就算有权力访问该表也不能随意对其修改,例如SSDT.IDT等.但这种方法很容易被绕过,我们只要将这些部分修改为可写 ...
- Linux 部署 ASP.NET Core 的一些问题记录
异常错误: 关闭 IP6 #修改 vi /etc/sysctl.conf # 添加如下三条设置 net.ipv6.conf.all.disable_ipv6 = 1 net.ipv6.co ...
- centos7 安装配置postgresql
考:https://www.linuxidc.com/Linux/2017-10/147536.htm http://blog.51cto.com/12482328/2090844 https://w ...
- ASP.NET MVC View中的标签(tag)
在编辑View的时候会用到各种HTML标签,如<a>,<input>,<p>等待,这些标签在ASP.NET MVC中都有对应的编程语法,它叫Razor,它是帮助我们 ...