CF444A DZY Loves Physics

以前的$C$题难度也不会写啊。

结论:答案一定是两个点一条边所构成的子图。

证明:

    假设有点$x, y$由边权为$p$的边连接,点$y, z$由边权为$q$的两条边连接,只需证明在$frac{x + y}{p},frac{y + z}{q},frac{x + y + z}{p + q}$中,$frac{x + y + z}{p + q}$取不到最大值即可。

    假如$frac{x + y + z}{p + q}$能取到最大值,有:

      $frac{x + y + z}{p + q} geq frac{x + y}{p}$
      $frac{x + y + z}{p + q} geq frac{y + z}{q}$

    $p, q$都是正数,乘过去消掉同类项,有:

      $pz geq qx + qy$

      $qx geq py + pz$

    要使两式同时成立,要有:

    $py + pz leq qx leq pz - qy$

    能看出不可能了。

枚举边就好了。

时间复杂度$O(n + m)$。

Code:

#include <cstdio>
#include <cstring>
using namespace std;
typedef double db;

const int N = 505;

int n, m, a[N];
db ans = 0;

template <typename T>
inline void chkMax(T &x, T y) {
    if(y > x) x = y;
}

inline void read(int &X) {
    X = 0; char ch = 0; int op = 1;
    for(; ch > '9'|| ch < '0'; ch = getchar())
        if(ch == '-') op = -1;
    for(; ch >= '0' && ch <= '9'; ch = getchar())
        X = (X << 3) + (X << 1) + ch - 48;
    X *= op;
}

int main() {
    read(n), read(m);
    for(int i = 1; i <= n; i++) read(a[i]);
    for(int x, y, v, i = 1; i <= m; i++) {
        read(x), read(y), read(v);
        chkMax(ans, 1.0 * (a[x] + a[y]) / v);
    }
    
    printf("%.10f
", ans);
    return 0;
}
View Code
原文地址:https://www.cnblogs.com/CzxingcHen/p/9738778.html