题目链接

题意:

有一个花园,有些地方是草地,有些地方是洞,现在要在这个花园中修建一个泳池,泳池全部由洞构成。

把洞变成草地需要花费一定的费用,把草地变成洞也需要花费一定的费用,并且泳池的边缘的草地上必须修建防护栏,也需要一定的费用。

花园的边缘地带一定得是草地。

问修建这个泳池的最少花费。

思路:

由于是把洞和草地分开,那么就充当了一个天然的“割”。这个割把草地的点和洞的点分开。

所以从\(S\)向所有除边缘地带的草地连边,容量为对应的费用,表示这个点变成洞需要付出的代价;

从\(S\)向边缘地带的草地和洞连边,容量为inf,表示不可能变成洞,代价为无穷,边缘的洞的费用可以提前计算出来;

从所有的非边缘洞向\(T\)连边,容量为对应的费用,表示把这个点变成草地的代价;

然后相邻的所有点连边,容量为护栏的费用,表示分隔这两个点的代价;

然后求最大流即可,即是最小割,花费的最少费用。

“最小割建图时的边的容量表示割掉这条边需要付出的代价”

代码:

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int N = 55;
const int nei[4][2] = {{0,1},{1,0},{-1,0},{0,-1}}; struct edge
{
int u,v,cap;
edge(){}
edge(int u,int v,int cap):u(u),v(v),cap(cap){}
}; vector<edge> es; char s[N][N];
int mp[N][N];
vector<int> G[N*N];
int S,T; void adde(int u,int v,int cap)
{
es.push_back(edge(u,v,cap));
es.push_back(edge(v,u,0));
int sz = es.size();
G[u].push_back(sz-2);
G[v].push_back(sz-1);
} int dis[N*N],gap[N*N]; void bfs()
{
memset(dis,inf,sizeof(dis));
memset(gap,0,sizeof(gap));
dis[T] = 0;
gap[0] = 1;
queue<int> q;
q.push(T);
while (!q.empty())
{
int u = q.front();
q.pop();
for (int i = 0;i < G[u].size();i++)
{
edge &e = es[G[u][i]];
int v = e.v;
if (dis[v] >= inf)
{
dis[v] = dis[u] + 1;
q.push(v);
gap[dis[v]]++;
}
}
}
} int dfs(int u,int f)
{
if (u == T) return f;
int res = f;
for (int i = 0;i < G[u].size();i++)
{
edge &e = es[G[u][i]];
int v = e.v;
if (dis[u] == dis[v] + 1 && e.cap > 0)
{
int tmp = dfs(v,min(res,e.cap));
if (tmp)
{
res -= tmp;
e.cap -= tmp;
es[G[u][i]^1].cap += tmp;
}
if (!res)
{
return f;
}
}
}
if (!(--gap[dis[u]])) dis[S] = T + 1;
gap[++dis[u]]++;
return f - res;
} int isap()
{
int ans = 0;
bfs();
while (dis[S] < T + 1) ans += dfs(S,inf);
return ans;
} int main()
{
int t;
scanf("%d",&t);
while (t--)
{
int n,m;
S = 0;
memset(mp,-1,sizeof(mp));
es.clear();
for (int i = 0;i <= T;i++) G[i].clear();
scanf("%d%d",&m,&n);
T = n * m + 1;
int d,f,b;
scanf("%d%d%d",&d,&f,&b);
int ans = 0;
for (int i = 1;i <= n;i++)
{
scanf("%s",s[i] + 1);
}
for (int i = 1;i <= n;i++)
{
for (int j = 1;j <= m;j++)
{
mp[i][j] = (i-1) * m + j; if (i == 1 || j == m || i == n || j == 1)
{
if (s[i][j] == '.')
{
adde(S,mp[i][j],inf);
ans += f;
}
else
{
adde(S,mp[i][j],inf);
}
}
else
{
if (s[i][j] == '.')
{
adde(mp[i][j],T,f);
}
else
{
adde(S,mp[i][j],d);
}
}
}
}
for (int i = 1;i <= n;i++)
{
for (int j = 1;j <= m;j++)
{
for (int k = 0;k < 4;k++)
{
int x = i + nei[k][0],y = j + nei[k][1];
int id = mp[x][y];
if (id == -1) continue;
adde(mp[i][j],id,b);
}
}
}
ans += isap();
printf("%d\n",ans);
}
return 0;
} /*
3
3 3
5 5 1
#.#
#.#
###
5 4
1 8 1
#..##
##.##
#.#.#
#####
2 2
27 11 11
#.
.#
*/

uvalive 5905 Pool construction的更多相关文章

  1. UVALive 5905 Pool Construction 最小割,s-t割性质 难度:3

    https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...

  2. UVa1515 Pool construction(最小割)

    题目 Source https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_pr ...

  3. UVA 1515 Pool construction 最大流跑最小割

    Pool construction You are working for the International Company for Pool Construction, a constructio ...

  4. UVa 1515 (最小割) Pool construction

    题意: 输入一个字符矩阵,'.'代表洞,'#'代表草地.可以把草改成洞花费为d,或者把洞改成草花费为f,最后还要在草和洞之间修围栏花费为b. 但要保证最外一圈是草,求最小费用. 分析: 还不是特别理解 ...

  5. Uva1515 Pool construction

    Time Limit: 3000MS64bit IO Format: %lld & %llu 网络流 最小割 心生绝望,用了好久的网络流模板居然是错的. ↑居然之前还侥幸能过一堆(并不)题. ...

  6. UVA 1515 Pool construction 水塘(最大流,经典)

    题意: 给一个h*w的矩阵,每个格子中是'#'和'.'两个符号之一,分别代表草和洞.现在要将洞给围起来(将草和洞分离),每条边需花费b元(即将一个洞包起来需要4边,将2个连续的洞包起来需要6边,省了2 ...

  7. UVA-1515 Pool construction (最小割)

    题目大意:有一块地,分成nxm块.有的块上长着草,有的块上是荒地.将任何一块长着草的块上的草拔掉都需要花费d个力气,往任何一块荒地上种上草都需要花费f个力气,在草和荒地之间架一个篱笆需要花费b个力气, ...

  8. UVa 1515 - Pool construction(最小割)

    链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  9. UVA1515 Pool construction (最小割模型)

    如果不允许转化'#'和'.'的话,那么可以直接在'#'和'.'之间连容量为b的边,把所有'#'和一个源点连接, 所有'.'和一个汇点连接,流量不限,那么割就是建围栏(分割'#'和'.')的花费. 问题 ...

随机推荐

  1. mgo连接池

    package main import ( "log" "sync" "time" "gopkg.in/mgo.v2" ...

  2. 【POJ - 1950】Dessert(dfs)

    -->Dessert Descriptions: 给你一个数N(3<=N<=15);每个数之间有三种运算符“‘+’,‘-’,‘.’”.输出和值等于零的所有的运算情况及次数num,如果 ...

  3. C学习笔记-小程序(长期更新)

    产生随机数 int t = (int)time(NULL); srand(t); int num = rand() % 10; 利用keybd_event函数自动打印,mouse_event函数保存文 ...

  4. C学习笔记-数据类型

    常量 在程序中不可变化的量,也就不可赋值 常用两种定义方式,#define和const 另外还有一个字符串常量 define #define MAX 100 #define在预编译的时候,其实也是做的 ...

  5. springboot使用elasticsearch的客户端操作eslaticsearch

    一  ES客户端 ES提供多种不同的客户端: 1.TransportClient ES提供的传统客户端,官方计划8.0版本删除此客户端. 2.RestClient RestClient是官方推荐使用的 ...

  6. freeRTOS学习8-21

    不能再中断服务程序调用该函数 应该调用xQueueSendFromISR()

  7. Win32汇编语言语法基础

    汇编语言(assembly language)是一种用于电子计算机.微处理器.微控制器或其他可编程器件的低级语言,亦称为符号语言.在汇编语言中,用助记符(Mnemonics)代替机器指令的操作码,用地 ...

  8. djang部署vue项目

    1,将vue项目npm run build 在此之前需要修改打包后的js,css文件路径: 需新建vue.config.js 在文件中添加: module.exports = { // 输出目录 as ...

  9. 转:GitHub团队项目合作流程

    转自:https://www.cnblogs.com/schaepher/p/4933873.html GitHub团队项目合作流程   已在另一篇博客中写出关于以下问题的解决,点此进入: 同步团队项 ...

  10. vue入门:(计算属性和侦听器)

    methods watch computed 一.methods-方法 在数据渲染是需要基于多个数据时第一种方法,可以采用methods属性中的方法计算返回值来实现,先来看示例: <div id ...