HDU 1733 Escape(分层网络流)
来源:互联网 发布:淘宝客url转换 编辑:程序博客网 时间:2024/04/30 22:02
HDU 1733 Escape
题目链接
题意:给定一个图,#是墙,@是出口,.可以行走,X是人,每个时间每个格子只能站一个人,问最少需要多少时间能让人全部撤离(从出口出去)
思路:网络流,把每个结点每秒当成一个结点,这样枚举时间,每多一秒就在原来的网络上直接加一层继续增广即可,注意考虑方向的时候,要考虑上原地不动
代码:
#include <cstdio>#include <cstring>#include <queue>#include <algorithm>using namespace std;const int MAXNODE = 100005;const int MAXEDGE = 500005;typedef int Type;const Type INF = 0x3f3f3f3f;struct Edge {int u, v;Type cap, flow;Edge() {}Edge(int u, int v, Type cap, Type flow) {this->u = u;this->v = v;this->cap = cap;this->flow = flow;}};struct Dinic {int n, m, s, t;Edge edges[MAXEDGE];int first[MAXNODE];int next[MAXEDGE];bool vis[MAXNODE];Type d[MAXNODE];int cur[MAXNODE];vector<int> cut;Type flow;void init(int n) {this->n = n;memset(first, -1, sizeof(first));m = 0;flow = 0;}void add_Edge(int u, int v, Type cap) {edges[m] = Edge(u, v, cap, 0);next[m] = first[u];first[u] = m++;edges[m] = Edge(v, u, 0, 0);next[m] = first[v];first[v] = m++;}bool bfs() {memset(vis, false, sizeof(vis));queue<int> Q;Q.push(s);d[s] = 0;vis[s] = true;while (!Q.empty()) {int u = Q.front(); Q.pop();for (int i = first[u]; i != -1; i = next[i]) {Edge& e = edges[i];if (!vis[e.v] && e.cap > e.flow) {vis[e.v] = true;d[e.v] = d[u] + 1;Q.push(e.v);}}}return vis[t];}Type dfs(int u, Type a) {if (u == t || a == 0) return a;Type flow = 0, f;for (int &i = cur[u]; i != -1; i = next[i]) {Edge& e = edges[i];if (d[u] + 1 == d[e.v] && (f = dfs(e.v, min(a, e.cap - e.flow))) > 0) {e.flow += f;edges[i^1].flow -= f;flow += f;a -= f;if (a == 0) break;}}return flow;}Type Maxflow(int s, int t) {this->s = s; this->t = t;while (bfs()) {for (int i = 0; i < n; i++)cur[i] = first[i];flow += dfs(s, INF);}return flow;}} gao;#define MP(a,b) make_pair(a,b)const int N = 20;const int d[5][2] = {0, 1, 0, -1, 1, 0, -1, 0, 0, 0};int n, m;char str[N][N];typedef pair<int, int> pii;bool vis[N][N];bool bfs(int sx, int sy) {queue<pii> Q;memset(vis, false, sizeof(vis));vis[sx][sy] = true;Q.push(MP(sx, sy));while (!Q.empty()) {pii u = Q.front();if (str[u.first][u.second] == '@') return true;Q.pop();for (int i = 0; i < 4; i++) {int x = u.first + d[i][0];int y = u.second + d[i][1];if (x < 0 || x >= n || y < 0 || y >= m || vis[x][y] || str[x][y] == '#') continue;vis[x][y] = true;Q.push(MP(x, y));}}return false;}bool judge() {for (int i = 0; i < n; i++) {for (int j = 0; j < m; j++) {if (str[i][j] == 'X')if (!bfs(i, j)) return false;}}return true;}int main() {while (~scanf("%d%d", &n, &m)) {int tot = 0;int s = n * m * 2 * 100, t = n * m * 2 * 100 + 1;gao.init(n * m * 2 * 100 + 2);for (int i = 0; i < n; i++) {scanf("%s", str[i]);for (int j = 0; j < m; j++)if (str[i][j] == 'X') {gao.add_Edge(s, i * m + j, 1);tot++;}}if (!judge()) printf("-1\n");else {for (int ti = 0; ti <= 100; ti++) {for (int i = 0; i < n; i++) {for (int j = 0; j < m; j++) {if (str[i][j] == '#') continue;int uin = ti * n * m * 2 + i * m + j;int uout = uin + n * m;gao.add_Edge(uin, uout, 1);if (str[i][j] == '@') gao.add_Edge(uout, t, 1);for (int k = 0; k < 5; k++) {int x = i + d[k][0];int y = j + d[k][1];if (x < 0 || x >= n || y < 0 || y >= m || str[x][y] == '#') continue;int vin = (ti + 1) * n * m * 2 + x * m + y;int vout = vin + n * m;gao.add_Edge(uout, vin, 1);}}}if (gao.Maxflow(s, t) == tot) {printf("%d\n", ti);break;}}}}return 0;}
0 0
- HDU 1733 Escape(分层网络流)
- hdu 1733 Escape(分层最大流)
- HDU 1733 [Escape] 分层图网络流+枚举时间
- HDU 1733 [Escape] 分层图网络流+枚举时间
- hdu 1733 网络流 分层网络
- HDU - 1733 Escape(网络流层次模型)
- hdu 3036 Escape 网络流
- HDU 3605 Escape(网络流 + 状压简化 )
- HDU Escape (网络流,最大流)
- 【网络流】 HDU 3605 Escape 缩图
- HDU 3605Escape(缩点+网络流之最大流)
- HDU 3605 Escape 网络流最大流 合并点
- hdu 3605 Escape【图论-网络流-最大流-状态压缩】
- HDU 3605 Escape(网络流 + 状压简化 | 二分图多重匹配)
- HDU 3065 Escape(简单网络流 二分图多重匹配)
- HDU 3605 Escape(多重匹配||状压缩点的网络流)
- hdu 3605-Escape(网络流SAP模板+缩点)
- HDU 1733 escape(最大流 + dinic算法)
- linux系统下的病毒
- UINavigationController多视图控制器
- 《严蔚敏数据结构_08》
- PHP文件操作(三)PHP实现文件上传和下载
- eclipse配置PyDev插件
- HDU 1733 Escape(分层网络流)
- linkin大话数据结构--List
- eclipse 缓存
- ActivityGroup里面装的子Activity不执行OnActivityResult()的解决办法
- 吃苹果等于刷牙 美媒公布6个养生秘方
- 绝妙的取样(《编程珠玑(续)》第十三章)
- sk_buff结构分析
- 中国移动-北京移动2015年校招面试
- 鸡兔同笼