BZOJ3993 [SDOI2015]星际战争

二分答案。。。然后最大流验证是否可行。。。

没了,好水啊QAQ

  1 /**************************************************************
  2     Problem: 3993
  3     User: rausen
  4     Language: C++
  5     Result: Accepted
  6     Time:40 ms
  7     Memory:1156 kb
  8 ****************************************************************/
  9  
 10 #include <cstdio>
 11 #include <cstring>
 12 #include <algorithm>
 13  
 14 using namespace std;
 15 typedef double lf;
 16 const int N = 105;
 17 const lf inf = 1e10;
 18 const lf eps = 1e-7;
 19 const int M = N * N << 1;
 20  
 21 inline int read();
 22  
 23 struct edge {
 24     int next, to;
 25     lf f;
 26     edge(int _n = 0, int _t = 0, lf _f = 0.0) : next(_n), to(_t), f(_f) {}
 27 } e[M];
 28  
 29 int n, m, S, T;
 30 int b[N];
 31 int first[N], tot = 1;
 32 int q[N], d[N];
 33  
 34 inline void Add_Edges(int x, int y, lf z) {
 35     e[++tot] = edge(first[x], y, z), first[x] = tot;
 36     e[++tot] = edge(first[y], x, 0), first[y] = tot;
 37 }
 38  
 39 #define y e[x].to
 40 #define p q[l]
 41 bool bfs() {
 42     static int l, r, x;
 43     memset(d, -1, sizeof(d));
 44     d[q[1] = S] = 1;
 45     for (l = r = 1; l != r + 1; ++l)
 46         for (x = first[p]; x; x = e[x].next)
 47             if (!~d[y] && e[x].f > eps) {
 48                 d[q[++r] = y] = d[p] + 1;
 49                 if (y == T) return 1;
 50             }
 51     return 0;
 52 }
 53 #undef p
 54  
 55 lf dfs(int p, lf lim) {
 56   if (p == T || !lim) return lim;
 57   int x;
 58   lf tmp, rest = lim;
 59   for (x = first[p]; x && rest > eps; x = e[x].next) 
 60     if (d[y] == d[p] + 1 && ((tmp = min(e[x].f, rest)) > eps)) {
 61       rest -= (tmp = dfs(y, tmp));
 62       e[x].f -= tmp, e[x ^ 1].f += tmp;
 63       if (!rest) return lim;
 64     }
 65   if (rest > eps) d[p] = -1;
 66   return lim - rest;
 67 }
 68  
 69 inline bool check(lf t) {
 70     static int x;
 71     for (x = 2; x <= tot; x += 2)
 72         e[x].f += e[x ^ 1].f, e[x ^ 1].f = 0;
 73     for (x = first[S]; x; x = e[x].next)
 74         e[x].f = b[y - n] * t;
 75     while (bfs()) dfs(S, inf);
 76     for (x = first[T]; x; x = e[x].next)
 77         if (e[x ^ 1].f > eps) return 0;
 78     return 1;
 79 }
 80 #undef y
 81  
 82 int main() {
 83     int i, j;
 84     lf l, r, mid;
 85     n = read(), m = read();
 86     S = n + m + 1, T = S + 1;
 87     for (i = 1; i <= n; ++i)
 88         Add_Edges(i, T, read());
 89     for (i = 1; i <= m; ++i)
 90         b[i] = read(), Add_Edges(S, n + i, 0);
 91     for (i = 1; i <= m; ++i)
 92         for (j = 1; j <= n; ++j)
 93             if (read() == 1) Add_Edges(n + i, j, inf);
 94     l = 0, r = 5e6;
 95     while (r - l > eps) {
 96         mid = (l + r) / 2.0;
 97         if (check(mid)) r = mid;
 98         else l = mid;
 99     }
100     printf("%.6lf
", (l + r) / 2.0);
101     return 0;
102 }
103  
104 inline int read() {
105     static int x;
106     static char ch;
107     x = 0, ch = getchar();
108     while (ch < '0' || '9' < ch)
109         ch = getchar();
110     while ('0' <= ch && ch <= '9') {
111         x = x * 10 + ch - '0';
112         ch = getchar();
113     }
114     return x;
115 }
116 
View Code
By Xs酱~ 转载请说明 博客地址:http://www.cnblogs.com/rausen
原文地址:https://www.cnblogs.com/rausen/p/4451902.html