标签:style blog http io color ar os sp for
题意:给定一个图,#是墙,@是出口,.可以行走,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; }
标签:style blog http io color ar os sp for
原文地址:http://blog.csdn.net/accelerator_/article/details/40989037