Going Home POJ - 2195 费用流板子题
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 题意没什么好说的 看样例就知道题意了 直接套板子
#include <cstdio>
#include <cstring>
#include <queue>
#include <cmath>
#include <algorithm>
#include <set>
#include <iostream>
#include <map>
#include <stack>
#include <string>
#include <vector>
#define pi acos(-1.0)
#define eps 1e-6
#define fi first
#define se second
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define bug printf("******\n")
#define mem(a,b) memset(a,b,sizeof(a))
#define fuck(x) cout<<"["<<x<<"]"<<endl
#define f(a) a*a
#define sf(n) scanf("%d", &n)
#define sff(a,b) scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define pf printf
#define FRE(i,a,b) for(i = a; i <= b; i++)
#define FREE(i,a,b) for(i = a; i >= b; i--)
#define FRL(i,a,b) for(i = a; i < b; i++)
#define FRLL(i,a,b) for(i = a; i > b; i--)
#define FIN freopen("DATA.txt","r",stdin)
#define lowbit(x) x&-x
#pragma comment (linker,"/STACK:102400000,102400000") using namespace std;
const int maxn = 1e5 + ;
typedef long long LL;
const int MX = ;
const int inf = 0x3f3f3f3f;
const int MXE = MX * MX * ;
struct MinCost_MaxFlow {
struct Edge {
int v, w, nxt;
int cost;
} E[MXE];
int head[MX], tot, level[MX], pre[MX], d[MX];
bool vis[MX];
void init() {
memset(head, -, sizeof(head));
tot = ;
}
void add(int u, int v, int w, int cost) {
E[tot].v = v;
E[tot].w = w;
E[tot].cost = cost;
E[tot].nxt = head[u];
head[u] = tot++;
E[tot].v = u;
E[tot].w = ;
E[tot].cost = -cost;
E[tot].nxt = head[v];
head[v] = tot++;
}
bool spfa(int s, int t) {
memset(vis, , sizeof(vis));
memset(d, 0x3f, sizeof(d));
memset(pre, -, sizeof(pre));
queue<int>q;
q.push(s);
d[s] = ;
vis[s] = ;
while (!q.empty()) {
int u = q.front();
q.pop();
vis[u] = ;
for (int i = head[u]; ~i; i = E[i].nxt) {
int w = E[i].w, v = E[i].v, cost = E[i].cost;
if (w > && d[v] > d[u] + cost) {
d[v] = d[u] + cost;
pre[v] = i;
if (!vis[v]) {
q.push(v);
vis[v] = ;
}
}
}
}
//如果是最小费用可行流则要这一句(要求费用最小,不要求流量最大)
//if (d[t] > 0) return false;
return pre[t] != -;
}
int solve(int s, int t, int &cost) {
int flow = ;
cost = ;
while (spfa(s, t)) {
int minFlow = inf;
for (int i = pre[t]; ~i; i = pre[E[i ^ ].v])
minFlow = min(minFlow, E[i].w);
for (int i = pre[t]; ~i; i = pre[E[i ^ ].v]) {
cost += minFlow * E[i].cost;
E[i].w -= minFlow;
E[i ^ ].w += minFlow;
}
flow += minFlow;
}
return flow;
}
} F;
int n, m;
struct Point {
int x, y;
Point (int x, int y): x(x), y(y) {}
};
int cal(Point a, Point b) {
return abs(a.x - b.x) + abs(a.y - b.y);
}
char tu[][];
vector<Point>men;
vector<Point>home;
int main() {
while(~sff(n, m), n + m) {
F.init();
men.clear();
home.clear();
for (int i = ; i < n ; i++) {
scanf("%s", tu[i]);
for (int j = ; j < m ; j++) {
if (tu[i][j] == 'm') men.push_back(Point(i, j));
if (tu[i][j] == 'H') home.push_back(Point(i, j));
}
}
int s=,len1=men.size(),len2=home.size(),t;
t=len1+len2+;
for (int i= ;i<len1 ;i++)
for (int j= ;j<len2 ;j++)
F.add(i+,j++len1,,cal(men[i],home[j]));
for (int i= ;i<=len1 ;i++) F.add(,i,,);
for (int i= ;i<=len2 ;i++) F.add(i+len1,t,,);
int cost = ;
F.solve(s, t, cost);
printf("%d\n", cost);
}
return ;
}
Going Home POJ - 2195 费用流板子题的更多相关文章
- Going Home POJ - 2195(费用流)
就是一个简单题 四个月前a的一道题,今天又看到了,再a一遍吧. 好吧 我想多了 用了bfs求最短路 其实不用的 因为没有障碍物 #include <iostream> #include ...
- poj 2516 (费用流)
题意:有N个供应商,M个店主,K种物品.每个供应商对每种物品的的供应量已知,每个店主对每种物品的需求量的已知,从不同的供应商运送不同的货物到不同的店主手上需要不同的花费,又已知从供应商m送第k种货物的 ...
- poj 2175 费用流消圈
题意抽象出来就是给了一个费用流的残存网络,判断该方案是不是最优方案,如果不是,还要求给出一个更优方案. 在给定残存网络上检查是否存在负环即可判断是否最优. 沿负环增广一轮即可得到更优方案. 考虑到制作 ...
- HDU 3376 && 2686 方格取数 最大和 费用流裸题
题意: 1.一个人从[1,1] ->[n,n] ->[1,1] 2.仅仅能走最短路 3.走过的点不能再走 问最大和. 对每一个点拆点限流为1就可以满足3. 费用流流量为2满足1 最大费用流 ...
- Lunch Time(费用流变型题,以时间为费用)
Lunch Time http://acm.hdu.edu.cn/showproblem.php?pid=4807 Time Limit: 4000/2000 MS (Java/Others) ...
- Coding Contest(费用流变形题,double)
Coding Contest http://acm.hdu.edu.cn/showproblem.php?pid=5988 Time Limit: 2000/1000 MS (Java/Others) ...
- POJ 1087 最大流裸题 + map
A Plug for UNIX Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 15597 Accepted: 5308 ...
- POJ 3686 The Windy's(思维+费用流好题)
The Windy's Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 5362 Accepted: 2249 Descr ...
- 网络流板子/费用流板子 2018南京I题+2016青岛G题
2018南京I题: dinic,链式前向星,数组队列,当前弧优化,不memset全部数组,抛弃满流点,bfs只找一条增广路,每次多路增广 #include <bits/stdc++.h> ...
随机推荐
- KVM嵌套虚拟化
1. 检查环境 $ grep -E 'svm|vmx' /proc/cpuinfo ~]# lsmod | grep kvm kvm_intel 170181 0 kvm ...
- solidity 十六进制字符串转十六进制bytes
pragma solidity ^0.4.16; contract Metadata { // 十六进制字符串转换成bytes function hexStr2bytes(string data)re ...
- leetcode个人题解——#22 Generate Parentheses
思路: 递归解决,如果左括号个数小于右括号或者左括号数小于总括号对数,则生成一个左括号,如果左括号数大于右括号,生成一个右括号. class Solution { public: vector< ...
- java一些面试题
java虚拟机 什么时候会触发full gc System.gc()方法的调用 老年代空间不足 永生区空间不足(JVM规范中运行时数据区域中的方法区,在HotSpot虚拟机中又被习惯称为永生代或者永生 ...
- 基于spring-boot、spring-cloud的websocket服务器多点负载均衡改造
背景 为应对更多用户使用socket的场景,准备对存放websocket的服务器进行多点搭建并配置负载均衡. 问题 服务器上了多点负载均衡以后,基于socket的部分功能发生了有规律的失效,查看后台日 ...
- codeforces 301D Yaroslav and Divisors(树状数组)
Yaroslav has an array p = p1, p2, ..., pn (1 ≤ pi ≤ n), consisting of n distinct integers. Also, he ...
- java知乎爬虫
好久没写博客了,前阵子项目忙着上线,现在有点空闲,就把最近写的一个爬虫和大家分享下,统计结果放在了自己买的阿里云服务器上(点此查看效果),效果如下: 程序是在工作之余写的,用了java 的webmgi ...
- 计算器软件实现系列(六)windowform窗体+SQL+策略模式
一 整体概述 这个计算器软件的功能和以前的功能基本上一样,只不过是数据的保存形式发生了变化,,以前用的是txt文件保存,现在更正用SQL数据库,现在更改了以前的文件保存形式,是三层架构中数据层的更换, ...
- iOS开发UUIView动画方法总结
#动画设置 UIView动画实现 @interface ViewController () @property (weak, nonatomic) IBOutlet UIView *myView; @ ...
- iOS音频播放概述
在iOS系统中apple对音频播放需要的操作进行了封装并提供了不同层次的接口 下面对其中的中高层接口进行功能说明: Audio File Services:读写音频数据,可以完成播放流程中的第2步: ...