洛谷P4926 [1007]倍杀测量者(差分约束)
程序员文章站
2022-06-04 20:09:32
题意 "题目链接" Sol 题目中的两个限制条件xian $$A_i \geqslant (K_i T)B_i$$ $$A_i(K_i + T) \geq B_i$$ 我们需要让这两个至少有一个不满足 直接差分约束建边即可 这里要用到两个trick 1. 若某个变量有固定取值的时候我们可以构造两个等 ......
题意
sol
题目中的两个限制条件xian
\[a_i \geqslant (k_i - t)b_i\]
\[a_i(k_i + t) \geq b_i\]
我们需要让这两个至少有一个不满足
直接差分约束建边即可
这里要用到两个trick
若某个变量有固定取值的时候我们可以构造两个等式\(c_i - 0 \leqslant x, c_i - 0 \geqslant x\)。
乘法的大小判断可以取log变加法,因为\(y = log(x)\)也是个单调函数
#include<bits/stdc++.h> #define mp(x, y) make_pair(x, y) #define fi first #define se second #define ll long long template <typename a, typename b> inline bool chmin(a &a, b b){if(a > b) {a = b; return 1;} return 0;} template <typename a, typename b> inline bool chmax(a &a, b b){if(a < b) {a = b; return 1;} return 0;} using namespace std; const int maxn = 4001, inf = 1e9; const double eps = 1e-5; inline int read() { char c = getchar(); int x = 0, f = 1; while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();} while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar(); return x * f; } int n, m, k; struct edge { int v, op; double k, w; }; vector<edge> v[maxn]; void addedge(int x, int y, double w, int opt, double k) { v[x].push_back({y, opt, k, w}); } double dis[maxn]; bool vis[maxn]; int times[maxn]; bool spfa(double add) { queue<int> q; q.push(n + 1); for(int i = 0; i <= n; i++) dis[i] = -1e18, vis[i] = times[i] = 0; dis[n + 1] = 0; ++times[n + 1]; while(!q.empty()) { int p = q.front(); q.pop(); vis[p] = 0; for(auto &x : v[p]) { int opt = x.op, to = x.v; double k = x.k, w; if(opt == 0) w = x.w; else if(opt == 1) w = log2(k - add); else w = -log2(k + add); if(dis[to] < dis[p] + w) { dis[to] = dis[p] + w; if(!vis[to]) { q.push(to); vis[to] = 1; ++times[to]; if(times[to] >= n + 1) return 0; } } } } return 1; } signed main() { n = read(); m = read(); k = read(); double l = 0, r = 10; for(int i = 1; i <= m; i++) { int opt = read(), x = read(), y = read(); double k = read(); addedge(y, x, 0, opt, k); if(opt == 1) chmin(r, k); } for(int i = 1; i <= k; i++) { int c = read(); double x = read(); addedge(0, c, log2(x), 0, 0); addedge(c, 0, -log2(x), 0, 0); } for(int i = 0; i <= n; i++) addedge(n + 1, i, 0, 0, 0); if(spfa(0)) return puts("-1"), 0; while(r - l > eps) { double mid = (r + l) / 2; if(spfa(mid)) r = mid; else l = mid; } printf("%lf", l); return 0; }