HDU - 1733 Escape(网络流层次模型)
来源:互联网 发布:云存储软件 编辑:程序博客网 时间:2024/05/18 02:34
题目大意:给出一张地图,每个位置每个时刻只能有一个人,现在问这些人都跑到门那边的最短时间
解题思路:网络流的层次模型,因为每个点每个时刻只能有一个人,所以拆成边,容量为1
再把每个结点每秒当成一个结点,接着枚举时间
连边的话有几种状态,一种是走到相邻格子的,如果能走的话,就把该点的出点连向下一个格子的下一秒的时态的入点
有可能该点的人不走,所以该点的出点要连接到该点的下一时刻的入点
#include <cstdio>#include <cstring>#include <algorithm>#include <vector>#include <queue>using namespace std;const int MAXNODE = (1 << 17) + 10;const int MAXEDGE = MAXNODE * 10;typedef int Type;const Type INF = 0x3f3f3f3f;struct Edge{ int u, v, next; Type cap, flow; Edge() {} Edge(int u, int v, Type cap, Type flow, int next) : u(u), v(v), cap(cap), flow(flow), next(next){}};struct Dinic{ int n, m, s, t; Edge edges[MAXEDGE]; int head[MAXNODE]; int cur[MAXNODE]; bool vis[MAXNODE]; Type d[MAXNODE]; vector<int> cut; void init(int n) { this->n = n; memset(head, -1, sizeof(head)); m = 0; } void AddEdge(int u, int v, Type cap) { edges[m] = Edge(u, v, cap, 0, head[u]); head[u] = m++; edges[m] = Edge(v, u, 0, 0, head[v]); head[v] = m++; } bool BFS() { memset(vis, 0, sizeof(vis)); queue<int> Q; Q.push(s); d[s] = 0; vis[s] = 1; while (!Q.empty()) { int u = Q.front(); Q.pop(); for (int i = head[u]; ~i; i = edges[i].next) { 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 = edges[i].next) { 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; Type flow = 0; while (BFS()) { for (int i = 0; i < n; i++) cur[i] = head[i]; flow += DFS(s, INF); } return flow; } void Mincut() { cut.clear(); for (int i = 0; i < m; i += 2) { if (vis[edges[i].u] && !vis[edges[i].v]) cut.push_back(i); } }}dinic;#define maxn 20struct Node { int x, y; Node() {} Node(int x, int y): x(x), y(y) {}};int n, m, num;char map[maxn][maxn];int dir[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};bool vis[maxn][maxn];void init() { for (int i = 0; i < n; i++) scanf("%s", map[i]);}bool bfs(int i, int j) { queue<Node> Q; memset(vis, 0, sizeof(vis)); Q.push(Node(i, j)); vis[i][j] = true; while (!Q.empty()) { Node t = Q.front(); Q.pop(); if (map[t.x][t.y] == '@') return true; for (int i = 0; i < 4; i++) { int tx = t.x + dir[i][0]; int ty = t.y + dir[i][1]; if (tx < 0 || tx >= n || ty < 0 || ty >= m || vis[tx][ty] || map[tx][ty] == '#') continue; vis[tx][ty] = true; Q.push(Node(tx, ty)); } } return false;}bool judge() { for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) if (map[i][j] == 'X' && !bfs(i, j)) return false; return true;}void solve() { if (!judge()) { printf("-1\n"); return ; } int source = n * m * 2 * 100, sink = n * m * 2 * 100+ 1; dinic.init(sink + 1); int tx, ty; num = 0; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) if (map[i][j] == 'X') { dinic.AddEdge(source, i * m + j, 1); num++; } int Time = 0; while (1) { for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) { if (map[i][j] == '#') continue; int in = Time * n * m * 2 + i * m + j; int out = in + n * m; dinic.AddEdge(in, out, 1); dinic.AddEdge(out, out + n * m, 1); if (map[i][j] == '@') dinic.AddEdge(out, sink, 1); for (int k = 0; k < 4; k++) { int tx = i + dir[k][0]; int ty = j + dir[k][1]; if (tx < 0 || tx >= n || ty < 0 || ty >= m || map[tx][ty] == '#') continue; dinic.AddEdge(out, (Time + 1) * n * m * 2 + tx * m + ty, 1); } } int Maxflow = dinic.Maxflow(source, sink); if (Maxflow == num) { printf("%d\n", Time); return ; } num -= Maxflow; Time++; }}int main() { while (scanf("%d%d", &n, &m) != EOF) { init(); solve(); } return 0;}
0 0
- HDU - 1733 Escape(网络流层次模型)
- HDU 1733 Escape(分层网络流)
- hdu 3036 Escape 网络流
- HDU 1733 [Escape] 分层图网络流+枚举时间
- HDU 1733 [Escape] 分层图网络流+枚举时间
- HDU Escape (网络流,最大流)
- 【网络流】 HDU 3605 Escape 缩图
- windows,网络层次模型
- HDU 3605 Escape 网络流最大流 合并点
- hdu 3605 Escape【图论-网络流-最大流-状态压缩】
- HDU 3605 Escape(网络流 + 状压简化 )
- hdu 3605-Escape(网络流SAP模板+缩点)
- iOS网络编程层次模型
- HDU 1733 escape(最大流 + dinic算法)
- hdu 1733 Escape(分层最大流)
- 【网络流】hdu1733 Escape
- HDU3605 Escape【网络流】
- hdu 1733 Escape
- linux用户管理命令(添加,删除,修改)
- Inno setup 简单打包教程
- C++中const 详解
- Caused by: javax.el.PropertyNotFoundException: Property 'title' not found on type java.lang.String
- POJ 1019:Number Sequence 二分查找
- HDU - 1733 Escape(网络流层次模型)
- OPC 通信预安装软件介绍(西门子PLC与上位机PC之间的通信)
- 接口和抽象类有什么区别
- vector的创建和使用
- HDU 5428 The Factor
- hdu 5431 AB String
- 在 SystemUI 的 Statusbar 中 添加 ethernet status icon
- js中常见数据类型输出情况
- 水桶定律