题意

给你一个\(N×M\)的草地,有高地有低地。

收割机从低地走到高地或者从高地走到低地都要花费用\(A\),你可以花费用\(B\)把一块高地变成低地,或者把一块低地变成高地。收割机每行每列都是必须要跑一趟的。

求最小花费。

解析

\(S\)向低地、高地向\(T\)建权为\(B\)的边,相邻的地之间建边权为\(A\)的边。

然后求最小割。

相同类型的地之间为什么也要建边呢?因为类型是可以改变的。

#include <bits/stdc++.h>
#define FOPI freopen("in.txt", "r", stdin)
#define FOPO freopen("out.txt", "w", stdout)
using namespace std;
typedef long long LL;
const int inf = 0x3f3f3f3f;
const int maxn = 50 * 50 + 1000;
const int maxm = 1e5 + 100; struct Edge
{
int to, next, cap, flow;
}edge[maxm]; int tot;
int head[maxn]; void init()
{
tot = 2;
memset(head, -1, sizeof(head));
} void build(int u, int v, int w, int rw = 0)
{
edge[tot].to = v; edge[tot].cap = w; edge[tot].flow = 0;
edge[tot].next = head[u]; head[u] = tot++; edge[tot].to = u; edge[tot].cap = 0; edge[tot].flow = 0;
edge[tot].next = head[v]; head[v] = tot++;
} int Q[maxn];
int dep[maxn], cur[maxn], sta[maxn]; bool bfs(int s, int t, int n)
{
int front = 0, tail = 0;
memset(dep, -1, sizeof(dep[0]) * (n+1));
dep[s] = 0;
Q[tail++] = s;
while(front < tail)
{
int u = Q[front++];
for (int i = head[u]; i != -1; i = edge[i].next)
{
int v = edge[i].to;
if (edge[i].cap > edge[i].flow && dep[v] == -1)
{
dep[v] = dep[u] + 1;
if (v == t) return true;
Q[tail++] = v;
}
}
}
return false;
} LL dinic(int s, int t, int n)
{
LL maxflow = 0;
while(bfs(s, t, n))
{
for (int i = 0; i < n; i++) cur[i] = head[i];
int u = s, tail = 0;
while(cur[s] != -1)
{
if (u == t)
{
int tp = inf;
for (int i = tail-1; i >= 0; i--)
tp = min(tp, edge[sta[i]].cap - edge[sta[i]].flow); //if (tp >= inf) return -1;
maxflow += tp; for (int i = tail-1; i >= 0; i--)
{
edge[sta[i]].flow += tp;
edge[sta[i]^1].flow -= tp;
if (edge[sta[i]].cap - edge[sta[i]].flow == 0) tail = i;
}
u = edge[sta[tail]^1].to;
}
else if (cur[u] != -1 && edge[cur[u]].cap > edge[cur[u]].flow
&& dep[u]+1 == dep[edge[cur[u]].to])
{
sta[tail++] = cur[u];
u = edge[cur[u]].to;
}
else
{
while(u != s && cur[u] == -1) u = edge[sta[--tail]^1].to;
cur[u] = edge[cur[u]].next;
}
}
}
return maxflow;
} int n, m, A, B;
int S, T;
char a[maxn][maxn]; int id(int i, int j) { return (i-1)*m + j; } int main()
{
//FOPI;
init(); scanf("%d%d%d%d", &n, &m, &A, &B);
S = 0, T = n*m+1; for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
{
scanf(" %c", &a[i][j]);
if (a[i][j] == '.') build(S, id(i, j), B);
else build(id(i, j), T, B); if (i > 1) build(id(i, j), id(i-1, j), A);
if (i < n) build(id(i, j), id(i+1, j), A);
if (j > 1) build(id(i, j), id(i, j-1), A);
if (j < m) build(id(i, j), id(i, j+1), A);
} LL ans = dinic(S, T, T+1);
printf("%lld\n", ans);
}

Gym - 101128F Landscaping(网络流)的更多相关文章

  1. Gym 101128F Landscaping(网络流)题解

    题意:n*m的地,从有高地和低地,从高地走到低地或者从低地走到高地花费a,把高地和低地互相改造一次花费b.现在要走遍每一行每一列,问最小花费 思路:超级源点连接所有低地,容量b:所有地向四周建边,容量 ...

  2. 【最小割】【Dinic】Gym - 101128F - Landscaping

    http://blog.csdn.net/lxy767087094/article/details/68942422 #include<cstdio> #include<cstrin ...

  3. Landscaping Gym - 101128F (网络流)

    Problem F: Landscaping \[ Time Limit: 1 s \quad Memory Limit: 256 MiB \] 题意 题意是给出一个\(n*m\)的格子,其中一些是低 ...

  4. Gym 101128F Sheldon Numbers(网络流)

    [题目链接] http://codeforces.com/gym/101128/attachments [题目大意] 给出一张地图,分为高地和低地,高低地的交界线上划有红线, 现在要开小车跨过每条红线 ...

  5. C - Portals Gym - 102006C (网络流最小割)

    题目链接:https://cn.vjudge.net/contest/283918#problem/C 题目大意:T个测试数据,然后给你一个字符串,每一个字符串包括‘s’,‘t’,‘o’,‘#’,‘. ...

  6. 【bzoj4439】[Swerc2015]Landscaping 网络流最小割

    题目描述 FJ有一块N*M的矩形田地,有两种地形高地(用‘#’表示)和低地(用‘.’表示) FJ需要对每一行田地从左到右完整开收割机走到头,再对每一列从上到下完整走到头,如下图所示 对于一个4*4的田 ...

  7. codeforces gym 100357 J (网络流)

    题目大意 有n种物品,m种建筑,p个人. n,m,p∈[1,20] 每种建筑需要若干个若干种物品来建造.每个人打算建造一种建筑,拥有一些物品. 主角需要通过交易来建造自己的建筑,交易的前提是对方用多余 ...

  8. Gym 102007I 二分 网络流

    题意:给你一张图,每个城市有一些人,有不超过10个城市有避难所,避难所有容量上限,问最快多久可以让所有人进入避难所? 思路:二分时间,对于每个时间跑一遍最大流,判断最大流是不是人数即可.我们还需要用二 ...

  9. Gym - 100203I I WIN 网络流

    Gym - 100203I  I WIN 题意:一个n*m的矩阵包含W,I,N三种字符,问相邻的字符最多能组成不重叠的WIN. 思路:比赛的时候没有发现是网络流,,居然一度以为是二分图匹配,,写了一下 ...

随机推荐

  1. Shell笔试题3

    1.查找当前目录中所有大于500M的文件,把这些文件名写到一个文本文件中,并统计其个数.find ./ -size +500M -type f | tee file_list | wc -l 2.在目 ...

  2. 开源的SSH框架优缺点分析

    开源是3个框架共有的优点 Struts2框架(MVC框架)的优点如下: 1) 实现了MVC模式,层次结构清晰,使程序员只需关注业务逻辑的实现: 2) 丰富的标签库,大大提高了开发的效率: 3) Str ...

  3. CI框架更新与删除

    $this->load->database();        // $query=$this->db->get('t_repayments');        // $res ...

  4. C#中 String和string的区别

    string 是 System.String的别名,所以相当于声明了 using string = System.String 再者,string是C#的关键字,String是System下的类名.

  5. 【Android开发笔记】杂项

    Android studio shift+enter : start new line Theme 将     <style name="AppBaseTheme" pare ...

  6. PHP函数:mysql_fetch_assoc指针重置

    本文目前主要讨论mysql_fetch_assoc“指针”如何重置的问题 要了解mysql_fetch_assoc,先看看它与mysql_fetch_row和mysql_fetch_array的关系. ...

  7. Google Authenticator加强ssh安全

    一.安装依赖包 软件包可以在这个地址下载:https://pan.baidu.com/s/1r0CmwbtCfNiBqU9rh_TxtA yum -y install pam-devel tar jx ...

  8. linux 命令——18 locate (转)

    locate 让使用者可以很快速的搜寻档案系统内是否有指定的档案.其方法是先建立一个包括系统内所有档案名称及路径的数据库,之后当寻找时就只需查询这个数据库,而不必实际深入档案系统之中了.在一般的 di ...

  9. 【洛谷2522】[HAOI2011] Problem b(莫比乌斯反演)

    点此看题面 大致题意: 求\(\sum_{x=a}^b\sum_{y=c}^d[gcd(x,y)==k]\). 关于另一道题目 在看这篇博客之前,如果你做过一道叫做[BZOJ1101][POI2007 ...

  10. geoNear查询 near查询的升级版

    geoNear查询可以看作是near查询点进化版 geoNear查询使用runCommand命令进行使用,常用使用如下: db.runCommand({ geoNear:<collection& ...