UVA 1515Pool construction
来源:互联网 发布:h5手机制作软件 编辑:程序博客网 时间:2024/05/29 16:34
题目大意:给出一个字符矩阵,其中有草地‘#’和洞‘.’可以将草改成洞花费d,也可以把洞改成草花费f,最后需要在草和洞之间修围栏,费用b,要求用最少的费用,要求第一行/列和最后一行/列必须为草
解题思路:先判断边界,如果为‘.’可以先填为‘#’并增加费用,之后构建网络流,做最小割问题,将源点s与所有‘#’相连,容量为d,代表要必须要割掉这条边才能将‘#’变为‘.’同理,将所有‘.’与t相连,容量f,然后将所有相邻格子连俩条方向不同的线,容量为b,表示若俩格子一个为‘#’一个为‘.’时,需要切割掉其中一条,最后求用最大流算法求出最小割就是答案
#include <iostream>#include <cstring>#include <queue>#include <vector>#include <algorithm>using namespace std;#define INF 0x3f3f3f3f#define maxn 4000int x, y, d, f, b;int result;int s, t;char ip[maxn][maxn];int dir[4][2] = {{-1, 0}, {0, -1}, {0, 1}, {1, 0}};struct Edge { int from, to, cap, flow; Edge(int from, int to, int cap, int flow):from(from), to(to), cap(cap), flow(flow){}};vector<Edge> edges;vector<int> G[maxn];void addEdge(int from, int to, int cap) { edges.push_back(Edge(from, to, cap, 0)); edges.push_back(Edge(to, from, 0, 0)); int i = edges.size(); G[from].push_back(i-2); G[to].push_back(i-1);}int EK() { int re = 0; int flo[maxn]; int pre[maxn]; while(1) { memset(flo, 0, sizeof(flo)); flo[s] = INF; queue<int> que; que.push(s); while(!que.empty()) { int po = que.front(); que.pop(); for(int i = 0; i < G[po].size(); i++) { Edge& ed = edges[G[po][i]]; if(!flo[ed.to] && ed.cap > ed.flow ) { flo[ed.to] = min(ed.cap - ed.flow, flo[po]); pre[ed.to] = G[po][i]; que.push(ed.to); } } if(flo[t]) break; } if(!flo[t]) break; for(int i = t; i != s; i = edges[pre[i]].from) { edges[pre[i]].flow += flo[t]; edges[pre[i]^1].flow -= flo[t]; } re += flo[t]; } return re;}void init() { s = 0; t = x * y + 1; result = 0; edges.clear(); for(int i = 0; i <= x*y+2; i++) G[i].clear(); for(int i = 1; i <= x; i++) { for(int j = 1; j <= y; j++) { if(i != x && j != 1 && i != 1 && j != y) { if(ip[i][j] == '#') { addEdge(s, (i-1)*y+j, d); } else { addEdge((i-1)*y+j, t, f); } } else { if(ip[i][j] == '.') { result += f; } addEdge(s, (i-1)*y+j, INF); } for(int k = 0; k < 4; k++) { int ci = i + dir[k][0]; int cj = j + dir[k][1]; if(ci <= 0 || ci > x || cj <= 0 || cj > y) continue; addEdge((i-1)*y+j, (ci-1)*y+cj, b); } } }}int main() { int n; cin >> n; while(n--) { cin >> y >> x; cin >> d >> f >> b; getchar(); for(int i = 1; i <= x; i++) { cin >> ip[i]+1; } init(); result += EK(); cout << result << endl; } return 0;}
阅读全文
0 0
- Uva-1515-Pool construction
- UVa 1515 Pool construction
- UVA 1515 Pool construction
- UVA 1515Pool construction
- UVA 1515 Pool construction
- 最小割 UVA 1515 Pool construction
- uva 1515 Pool construction(最大流)
- UVA 1515 Pool construction(网络流)
- UVA 1515 Pool construction [最小割]
- UVA 1515 Pool construction(最小割)
- uva 1515 Pool construction(最小割)
- 1515 - Pool construction
- 1515 - Pool construction(最大流最小割模型)
- 【网络流】- LA5905-Pool construction
- UVa 10720 - Graph Construction
- UVa 10720 - Graph Construction
- uva 10720 - Graph Construction
- uva 10720 - Graph Construction
- OracleRAC集群SCAN说明
- tensorflow-mnist数据集训练
- 序列化
- layuicms 后台管理系统中,右侧子窗口的链接在tab选项卡中打开
- 正则表达式 ---判断非空
- UVA 1515Pool construction
- 全栈和前后端分析
- 让职场新人速成的5大狠招!
- PhantomJS API 第一篇
- 分享一个很好用的 输出日志的类
- 快捷键
- 自定义的一种猜大小的游戏
- FlyMcu串口ISP下载STM32程序教程
- 浦刷微信公众号收银宝获取支付二维码