前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >洛谷P4926 [1007]倍杀测量者(差分约束)

洛谷P4926 [1007]倍杀测量者(差分约束)

作者头像
attack
发布2019-03-12 16:39:34
4090
发布2019-03-12 16:39:34
举报
文章被收录于专栏:数据结构与算法

题意

题目链接

Sol

题目中的两个限制条件xian

\[A_i \geqslant (K_i - T)B_i\]

\[A_i(K_i + T) \geq B_i\]

我们需要让这两个至少有一个不满足

直接差分约束建边即可

这里要用到两个trick

  1. 若某个变量有固定取值的时候我们可以构造两个等式\(C_i - 0 \leqslant X, C_i - 0 \geqslant X\)。
  2. 乘法的大小判断可以取log变加法,因为\(y = log(x)\)也是个单调函数
代码语言:javascript
复制
#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;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019-03-03 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 题意
  • Sol
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档