BZOJ1738 [Usaco2005 mar]Ombrophobic Bovines 发抖的牛

先预处理出来每个点对之间的最短距离

然后二分答案,网络流判断是否可行就好了恩

  1 /**************************************************************
  2     Problem: 1738
  3     User: rausen
  4     Language: C++
  5     Result: Accepted
  6     Time:404 ms
  7     Memory:9788 kb
  8 ****************************************************************/
  9
 10 #include <cstdio>
 11 #include <cstring>
 12 #include <algorithm>
 13
 14 using namespace std;
 15 typedef long long ll;
 16 const int N = 405;
 17 const int M = N * N << 2;
 18 const int inf_int = 1e9;
 19 const ll inf_ll = (ll) 1e18;
 20
 21 inline int read();
 22
 23 struct edge {
 24     int next, to, f;
 25     edge(int _n = 0, int _t = 0, int _f = 0) : next(_n), to(_t), f(_f) {}
 26 } e[M];
 27
 28 int n, m, S, T;
 29 int a[N], b[N], sum;
 30 ll mp[N][N];
 31 int first[N], tot;
 32 int d[N];
 33
 34 inline void Add_Edges(int x, int y, int z) {
 35     e[++tot] = edge(first[x], y, z), first[x] = tot;
 36     e[++tot] = edge(first[y], x), first[y] = tot;
 37 }
 38 #define y e[x].to
 39 #define p q[l]
 40 bool bfs() {
 41     static int l, r, x, q[N];
 42     memset(d, -1, sizeof(d));
 43     d[q[1] = S] = 1;
 44     for (l = r = 1; l != r + 1; ++l)
 45         for (x = first[p]; x; x = e[x].next)
 46             if (!~d[y] && e[x].f) {
 47                 d[q[++r] = y] = d[p] + 1;
 48                 if (y == T) return 1;
 49             }
 50     return 0;
 51 }
 52 #undef p
 53
 54 int dfs(int p, int lim) {
 55   if (p == T || !lim) return lim;
 56   int x, tmp, rest = lim;
 57   for (x = first[p]; x && rest; x = e[x].next)
 58     if (d[y] == d[p] + 1 && ((tmp = min(e[x].f, rest)) > 0)) {
 59       rest -= (tmp = dfs(y, tmp));
 60       e[x].f -= tmp, e[x ^ 1].f += tmp;
 61       if (!rest) return lim;
 62     }
 63   if (rest) d[p] = -1;
 64   return lim - rest;
 65 }
 66 #undef y
 67
 68 int Dinic() {
 69   int res = 0;
 70   while (bfs())
 71     res += dfs(S, inf_int);
 72   return res;
 73 }
 74
 75 inline bool check(ll t) {
 76     static int i, j;
 77     memset(first, 0, sizeof(first)), tot = 1;
 78     for (i = 1; i <= n; ++i)
 79         Add_Edges(S, i * 2 - 1, a[i]), Add_Edges(i * 2, T, b[i]);
 80     for (i = 1 ; i <= n; ++i)
 81         for (j = 1; j <= n; ++j)
 82             if (mp[i][j] <= t) Add_Edges(i * 2 - 1, j * 2, inf_int);
 83     return Dinic() == sum;
 84 }
 85
 86 int main() {
 87     int i, j, k, x, y, z;
 88     ll l, r, mid;
 89     n = read(), m = read(), S = n * 2 + 1, T = S + 1;
 90     for (i = 1; i <= n; ++i)
 91         sum += (a[i] = read()), b[i] = read();
 92     for (i = 1; i <= n; ++i)
 93         for (j = 1; j <= n; ++j) mp[i][j] = i == j ? 0 : inf_ll;
 94     for (i = 1; i <= m; ++i) {
 95         x = read(), y = read(), z = read();
 96         if (mp[x][y] > z) mp[x][y] = mp[y][x] = z;
 97     }
 98     for (k = 1; k <= n; ++k)
 99         for (i = 1; i <= n; ++i)
100             for (j = 1; j <= n; ++j)
101                 mp[i][j] = min(mp[i][j], mp[i][k] + mp[k][j]);
102
103     l = 0, r = inf_ll;
104     while (l + 1 < r) {
105         mid = l + r >> 1;
106         if (check(mid)) r = mid;
107         else l = mid;
108     }
109     printf("%lld\n", r == inf_ll ? -1ll : r);
110     return 0;
111 }
112
113 inline int read() {
114     static int x;
115     static char ch;
116     x = 0, ch = getchar();
117     while (ch < ‘0‘ || ‘9‘ < ch)
118         ch = getchar();
119     while (‘0‘ <= ch && ch <= ‘9‘) {
120         x = x * 10 + ch - ‘0‘;
121         ch = getchar();
122     }
123     return x;
124 }

时间: 2024-10-15 17:11:24

BZOJ1738 [Usaco2005 mar]Ombrophobic Bovines 发抖的牛的相关文章

BZOJ 1738: [Usaco2005 mar]Ombrophobic Bovines 发抖的牛( floyd + 二分答案 + 最大流 )

一道水题WA了这么多次真是.... 统考终于完 ( 挂 ) 了...可以好好写题了... 先floyd跑出各个点的最短路 , 然后二分答案 m , 再建图. 每个 farm 拆成一个 cow 点和一个 shelter 点, 然后对于每个 farm x : S -> cow( x ) = cow( x ) 数量 , shelter( x ) -> T = shelter( x ) 容量 ; 对于每个dist( u , v ) <= m 的 cow( u ) -> shelter( v

BZOJ 1738: [Usaco2005 mar]Ombrophobic Bovines 发抖的牛

Description 约翰的牛们非常害怕淋雨,那会使他们瑟瑟发抖.他们打算安装一个下雨报警器,并且安排了一个撤退计划.他们需要计算最少的让所有牛进入雨棚的时间.    牛们在农场的F(1≤F≤200)个田地上吃草.有P(1≤P≤1500)条双向路连接着这些田地.路很宽,无限量的牛可以通过.田地上有雨棚,雨棚有一定的容量,牛们可以瞬间从这块田地进入这块田地上的雨棚    请计算最少的时间,让每只牛都进入雨棚. Input 第1行:两个整数F和P; 第2到F+1行:第i+l行有两个整数描述第i个田

【BZOJ1738】【POJ2391】【Usaco2005 mar】 Ombrophobic Bovines 发抖的牛 网络流

题意: 约翰的牛们非常害怕淋雨,那会使他们瑟瑟发抖.他们打算安装一个下雨报警器,并且安排了一个撤退计划.他们需要计算最少的让所有牛进入雨棚的时间.    牛们在农场的F(1≤F≤200)个田地上吃草.有P(1≤P≤1500)条双向路连接着这些田地.路很宽,无限量的牛可以通过.田地上有雨棚,雨棚有一定的容量,牛们可以瞬间从这块田地进入这块田地上的雨棚    请计算最少的时间,让每只牛都进入雨棚. input 第1行:两个整数F和P; 第2到F+1行:第i+l行有两个整数描述第i个田地,第一个表示田

POJ 2391 Ombrophobic Bovines

Ombrophobic Bovines Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18623   Accepted: 4057 Description FJ's cows really hate getting wet so much that the mere thought of getting caught in the rain makes them shake in their hooves. They h

1740: [Usaco2005 mar]Yogurt factory 奶酪工厂

1740: [Usaco2005 mar]Yogurt factory 奶酪工厂 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 119  Solved: 100[Submit][Status][Discuss] Description The cows have purchased a yogurt factory that makes world-famous Yucky Yogurt. Over the next N (1 <= N <= 10

Ombrophobic Bovines (poj 2391 网络流+二分+Floyd)

Language: Default Ombrophobic Bovines Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 15733   Accepted: 3434 Description FJ's cows really hate getting wet so much that the mere thought of getting caught in the rain makes them shake in th

1682: [Usaco2005 Mar]Out of Hay 干草危机

1682: [Usaco2005 Mar]Out of Hay 干草危机 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 391  Solved: 258[Submit][Status] Description The cows have run out of hay, a horrible event that must be remedied immediately. Bessie intends to visit the other farms

BZOJ 1739: [Usaco2005 mar]Space Elevator 太空电梯

题目 1739: [Usaco2005 mar]Space Elevator 太空电梯 Time Limit: 5 Sec  Memory Limit: 64 MB Description The cows are going to space! They plan to achieve orbit by building a sort of space elevator: a giant tower of blocks. They have K (1 <= K <= 400) differe

POJ 2391 Ombrophobic Bovines 不喜欢雨的奶牛 Floyd+二分枚举+最大流

题目链接:POJ 2391 Ombrophobic Bovines Ombrophobic Bovines Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 15006   Accepted: 3278 Description FJ's cows really hate getting wet so much that the mere thought of getting caught in the rain makes