uvalive 5905 Pool construction
题意:
有一个花园,有些地方是草地,有些地方是洞,现在要在这个花园中修建一个泳池,泳池全部由洞构成。
把洞变成草地需要花费一定的费用,把草地变成洞也需要花费一定的费用,并且泳池的边缘的草地上必须修建防护栏,也需要一定的费用。
花园的边缘地带一定得是草地。
问修建这个泳池的最少花费。
思路:
由于是把洞和草地分开,那么就充当了一个天然的“割”。这个割把草地的点和洞的点分开。
所以从\(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的更多相关文章
- UVALive 5905 Pool Construction 最小割,s-t割性质 难度:3
https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...
- UVa1515 Pool construction(最小割)
题目 Source https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_pr ...
- UVA 1515 Pool construction 最大流跑最小割
Pool construction You are working for the International Company for Pool Construction, a constructio ...
- UVa 1515 (最小割) Pool construction
题意: 输入一个字符矩阵,'.'代表洞,'#'代表草地.可以把草改成洞花费为d,或者把洞改成草花费为f,最后还要在草和洞之间修围栏花费为b. 但要保证最外一圈是草,求最小费用. 分析: 还不是特别理解 ...
- Uva1515 Pool construction
Time Limit: 3000MS64bit IO Format: %lld & %llu 网络流 最小割 心生绝望,用了好久的网络流模板居然是错的. ↑居然之前还侥幸能过一堆(并不)题. ...
- UVA 1515 Pool construction 水塘(最大流,经典)
题意: 给一个h*w的矩阵,每个格子中是'#'和'.'两个符号之一,分别代表草和洞.现在要将洞给围起来(将草和洞分离),每条边需花费b元(即将一个洞包起来需要4边,将2个连续的洞包起来需要6边,省了2 ...
- UVA-1515 Pool construction (最小割)
题目大意:有一块地,分成nxm块.有的块上长着草,有的块上是荒地.将任何一块长着草的块上的草拔掉都需要花费d个力气,往任何一块荒地上种上草都需要花费f个力气,在草和荒地之间架一个篱笆需要花费b个力气, ...
- UVa 1515 - Pool construction(最小割)
链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- UVA1515 Pool construction (最小割模型)
如果不允许转化'#'和'.'的话,那么可以直接在'#'和'.'之间连容量为b的边,把所有'#'和一个源点连接, 所有'.'和一个汇点连接,流量不限,那么割就是建围栏(分割'#'和'.')的花费. 问题 ...
随机推荐
- 20190526 - CentOS 7 中 安装 MySQL 8 并授权 root 远程访问
1. CentOS 7 中 安装 MySQL 8 CentOS 7 中内置 MariaDB 建议升级一下用,性能好很多.但如果一定要用 MySQL 8,就得自己装. 坦白的说,Oracle 升级 My ...
- 【VS开发】recv函数函数返回值说明(特别有利于工程调试)
recv函数 int recv( SOCKET s, char FAR *buf, int len, int flags); 不论是客户还是服务器应用程序都用recv函数从TCP连接的另一端接收数据. ...
- elasticsearch 常用查询 + 删除索引 + 集群状态诊断
1.多条件查询 curl -X POST \ http://10.0.0.42:9200/addressbook_user/_search \ -H 'cache-control: no-cache' ...
- pynput模块—键盘鼠标操作和监听
pynput.mouse:包含控制和监控鼠标或者触摸板的类. pynput.keyboard:包含控制和监控键盘的类. 上面提到的子包都已被引入到pynput库中.要使用上面的子包,从pynput中引 ...
- [Comet OJ - Contest #6 D][48D 2280]另一道树题_并查集
另一道树题 题目大意: 数据范围: 题解: 这个题第一眼能发现的是,我们的答案分成两种情况. 第一种是在非根节点汇合,第二种是在根节点汇合. 尝试枚举在第几回合结束,假设在第$i$回合结束的方案数为$ ...
- python类学习
创建关于汽车的类 class Cars(): def __init__(self, brand, country): self.brand = brand self.country = country ...
- Prefix to Infix Conversion
Infix : An expression is called the Infix expression if the operator appears in between the operands ...
- 外边距margin的叠加问题
下午在看<css禅意花园>,书中提到了外边距重叠,于是去网上搜索了一下资料. 写了一个小例子做测试.发现网上的有些总结与我的测试不符,索性就自己总结了╮(╯▽╰)╭ <!DOCTYP ...
- 洛谷P1603 斯诺登的密码(水题
不知道什么时候打开的,随手做掉了,没什么用...大概又熟悉了一下map吧...大概........一开始还因为没读清题没把非正规的英文表示数字存进去wa了...orz最近状态不行 题目描述 题目描述 ...
- SDL2 程序 编译 错误 及 解决方案
main函数应写为int main( int argc, char* args[] )而不是int main()形式 链接库时应注意顺序 mingw32;SDL2main;SDL2; ...