标签:style blog http io color ar os sp for
题意:一些牛棚,有a只牛,现在下雨,每个牛棚容量量变成b,现在有一些道路连接了牛棚,问下雨后牛走到其他牛棚,使得所有牛都有地方躲雨,最后一只牛要走多久
思路:二分答案,然后最大流去判断,建图的方式为,牛棚拆点,源点连向入点,容量为a,出点连向汇点容量为b,中间入点和出点之间根据二分的值判断哪些边是可以加入的
代码:
#include <cstdio> #include <cstring> #include <queue> #include <algorithm> using namespace std; const int MAXNODE = 405; const int MAXEDGE = 100005; 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; typedef long long ll; const int N = 205; const int M = 1505; int f, p, f1[N], f2[N], sum; ll g[N][N]; bool judge(ll len) { gao.init(f * 2 + 2); for (int i = 1; i <= f; i++) { gao.add_Edge(0, i, f1[i]); gao.add_Edge(f + i, f * 2 + 1, f2[i]); } for (int i = 1; i <= f; i++) { for (int j = 1; j <= f; j++) { if (g[i][j] > len) continue; gao.add_Edge(i, f + j, INF); } } return gao.Maxflow(0, f * 2 + 1) == sum; } int main() { while (~scanf("%d%d", &f, &p)) { sum = 0; for (int i = 1; i <= f; i++) { scanf("%d%d", &f1[i], &f2[i]); sum += f1[i]; for (int j = 1; j <= f; j++) { if (i == j) g[i][j] = 0; else g[i][j] = 100000000000000000LL; } } int u, v; ll w; while (p--) { scanf("%d%d%lld", &u, &v, &w); g[u][v] = g[v][u] = min(g[u][v], w); } ll l = 0, r = 0; for (int k = 1; k <= f; k++) { for (int i = 1; i <= f; i++) { for (int j = 1; j <= f; j++) { g[i][j] = min(g[i][j], g[i][k] + g[k][j]); if (g[i][j] != 100000000000000000) r = max(g[i][j], r); } } } if (!judge(r)) { printf("-1\n"); continue; } while (l < r) { ll mid = (l + r) / 2; if (judge(mid)) r = mid; else l = mid + 1; } printf("%lld\n", l); } return 0; }
POJ 2391 Ombrophobic Bovines(最大流+拆点)
标签:style blog http io color ar os sp for
原文地址:http://blog.csdn.net/accelerator_/article/details/40892321