标签:sof bfs while ring can rds target hdu case
题意:有一些蜥蜴在一个迷宫里面,有一个跳跃力表示能跳到多远的柱子,然后每根柱子最多被跳一定次数。求这些蜥蜴还有多少是不管怎样都逃不出来的。
思路:把柱子拆点建图跑最大流就可以。还是挺明显的
代码:
#include <cstdio> #include <cstring> #include <cmath> #include <queue> #include <algorithm> using namespace std; const int MAXNODE = 805; 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; void init(int n) { this->n = n; memset(first, -1, sizeof(first)); m = 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; Type flow = 0; while (bfs()) { for (int i = 0; i < n; i++) cur[i] = first[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); } } } gao; const int N = 25; int T, n, m; double d; char str[N]; int main() { int cas = 0; scanf("%d", &T); while (T--) { scanf("%d%lf", &n, &d); gao.init(n * 20 * 2 + 2); int s = n * 20 * 2, t = n * 20 * 2 + 1; for (int i = 0; i < n; i++) { scanf("%s", str); m = strlen(str); for (int j = 0; j < m; j++) { if (str[j] != ‘0‘) gao.add_Edge(i * m + j, i * m + j + n * m, str[j] - ‘0‘); if (i - d < 0 || i + d >= n || j - d < 0 || j + d >= m) gao.add_Edge(i * m + j + n * m, t, INF); } } int tot = 0; for (int i = 0; i < n; i++) { scanf("%s", str); m = strlen(str); for (int j = 0; j < m; j++) { if (str[j] == ‘L‘) { tot++; gao.add_Edge(s, i * m + j, 1); } } } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { for (int x = 0; x < n; x++) { for (int y = 0; y < m; y++) { int dx = i - x; int dy = j - y; double dis = sqrt(dx * dx * 1.0 + dy * dy); if (dis > d) continue; gao.add_Edge(i * m + j + n * m, x * m + y, INF); } } } } printf("Case #%d: ", ++cas); int ans = tot - gao.Maxflow(s, t); if (ans == 0) printf("no "); else printf("%d ", ans); if (ans <= 1) printf("lizard was "); else printf("lizards were "); printf("left behind.\n"); } return 0; }
HDU 2732 Leapin' Lizards(拆点+最大流)
标签:sof bfs while ring can rds target hdu case
原文地址:http://www.cnblogs.com/ljbguanli/p/7000989.html