题意
Sol
题目中的两个限制条件相当于是
[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;
}