跳到正文

题解

题解 P4926 【[1007]倍杀测量者】

UCanHexAKing约 7 分钟
原文

这是 2026年2月21日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

发布
2021年1月21日
收录
2026年2月21日
更新
2026年11月10日
篇幅
约 7 分钟
题目
P4926

版本

  1. v2当前
  2. v1

分析

这道题显然不好直接求出 TT 值。

显然,如果 T=T′T=T' 时有人nz,那么当 T<T′T<T' 的时候肯定也有人 nz,说明这道题的答案具有单调性,可以考虑二分转换成判断问题。

接下来需要实现函数 check(mid)check(mid)。

不妨假设 T=midT=mid 时没有人女装,为了方便,设第 ii 个选手的成绩是 scoreiscore_i,那么会有一些限制如下:

对于每一个 o=1o=1 的 flag,有:scorea≥scoreb×(mid−k)score_a \ge score_b \times (mid-k)。

对于每一个 o=2o=2 的 flag,有:scorea×(mid+k)>scorebscore_a \times (mid+k) > score_b。

只需知道是否存在满足条件的 scorescore(别忘了还有一些 scorescore 已知)。

变一下形:scoreascoreb≥(mid−k)\dfrac{score_a}{score_b} \ge (mid-k) 和 scoreascoreb>1mid+k\dfrac{score_a}{score_b}>\dfrac{1}{mid+k}

反正这道题的精度要求不高,直接都当做大于等于好像也没什么问题事实上是我不知道差分约束怎么处理严格不等号

那么对于 scorexscorey≥z\dfrac{score_x}{score_y}\ge z,由 yy 向 xx 连一条权值为 zz 的边,对于已知 scorescore 的点 ii,由虚拟节点 0(强制让 score0=1score_0=1)向 ii 连 scoreiscore_i 的边,再由 ii 向 0 连 1scorei\dfrac{1}{score_i}的边,如果存在一个环上面的边权乘积大于1就不能满足,也就是会有人 nz;否则就可能没有人 nz。

解决

Talk is cheap, show me your code!(\texttt{Talk is cheap, show me your code!(}

C++
// Problem: P4926 [1007]倍杀测量者
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P4926
// Memory Limit: 125 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>

using namespace std;

const int N = 1005, M = 2005;
int n, m, c, tot;
double dis[N], edge[M];
int nxt[M], ver[M], head[N];
int cnt[N], val[N];
int score[N];
char vis[N];
queue <int> Q;

struct NZ_Flag {
    int op, a, b, k;
}flag[N];

inline char SPFA() {
    memset(vis, 1, sizeof(vis));
    memset(cnt, 0, sizeof(cnt));
    memset(val, 0, sizeof(val));
    for (int i = 0; i <= n; i++) dis[i] = 1, Q.push(i);
    while (!Q.empty()) {
        int cur = Q.front();
        Q.pop();
        vis[cur] = 0;
        for (int i = head[cur]; i; i = nxt[i]) {
            int to = ver[i];
            double w = edge[i];
            if (dis[to] < dis[cur] * w) {
                dis[to] = dis[cur] * w, val[to] = val[cur] + 1;
                if (val[to] > n)
                    return 1;
                if (!vis[to]) {
                    vis[to] = 1, Q.push(to);
                    cnt[to]++;
                    if (cnt[to] > n)
                        return 1;
                }
            }
        }
    }
    return 0;
}

template <class file>
inline void read(file &ret) {
    ret = 0;
    char ch = getchar();
    while (ch < '0' || ch > '9') ch = getchar();
    while (ch >= '0' && ch <= '9') ret = ret * 10 + ch - '0', ch = getchar();
}

inline void addedge(int x, int y, double z) { 
    ver[++tot] = y, edge[tot] = z;
    nxt[tot] = head[x], head[x] = tot; 
}

inline char check(double mid) {
    memset(head, 0, sizeof(head)), tot = 0;
    for (int i = 1; i <= m; i++) {
        if (flag[i].op == 1)
            addedge(flag[i].b, flag[i].a, flag[i].k - mid);
        else
            addedge(flag[i].b, flag[i].a, 1.0 / (mid + flag[i].k));
    }
    for (int i = 1; i <= n; i++)
        if (score[i])
            addedge(0, i, score[i]), addedge(i, 0, 1.0 / score[i]);
    return SPFA();
}

int main() {
    read(n), read(m), read(c);
    double l = 1e-6, r = 10, ans = -1;
    for (int i = 1; i <= m; i++) {
        read(flag[i].op), read(flag[i].a), read(flag[i].b), read(flag[i].k);
        if (flag[i].op == 1)
            r = min(r, 1.0 * flag[i].k);
    }
    while (c--) {
        int x, y;
        read(x), read(y);
        score[x] = y;
    }
    for (int i = 1; i <= 50; i++) {
        double mid = (l + r) / 2;
        if (check(mid))
            l = ans = mid;
        else
            r = mid;
    }
    printf("%.4lf\n", ans);
    return 0;
}

THE END