[USACO07DEC] Sightseeing Cows G

发布时间 2023-12-30 16:08:17作者: onlyblues

[USACO07DEC] Sightseeing Cows G

题目描述

Farmer John has decided to reward his cows for their hard work by taking them on a tour of the big city! The cows must decide how best to spend their free time.

Fortunately, they have a detailed city map showing the $L$ $(2 \leq L \leq 1000)$ major landmarks (conveniently numbered $1\ldots L$) and the $P$ $(2 \leq P \leq 5000)$ unidirectional cow paths that join them. Farmer John will drive the cows to a starting landmark of their choice, from which they will walk along the cow paths to a series of other landmarks, ending back at their starting landmark where Farmer John will pick them up and take them back to the farm. Because space in the city is at a premium, the cow paths are very narrow and so travel along each cow path is only allowed in one fixed direction.

While the cows may spend as much time as they like in the city, they do tend to get bored easily. Visiting each new landmark is fun, but walking between them takes time. The cows know the exact fun values $F_i$ $(1 \leq F_i \leq 1000)$ for each landmark $i$.

The cows also know about the cowpaths. Cowpath $i$ connects landmark ${L1}_i$ to ${L2}_i$ (in the direction ${L1}_i \to {L2}_i$ ) and requires time $T_i$ $(1 \leq T_i \leq 1000)$ to traverse.

In order to have the best possible day off, the cows want to maximize the average fun value per unit time of their trip. Of course, the landmarks are only fun the first time they are visited; the cows may pass through the landmark more than once, but they do not perceive its fun value again. Furthermore, Farmer John is making the cows visit at least two landmarks, so that they get some exercise during their day off.

Help the cows find the maximum fun value per unit time that they can achieve.

作为对奶牛们辛勤工作的回报,$Farmer\ John$ 决定带她们去附近的大城市玩一天。旅行的前夜,奶牛们在兴奋地讨论如何最好地享受这难得的闲暇。  
很幸运地,奶牛们找到了一张详细的城市地图,上面标注了城市中所有 $L(2\leqslant L\leqslant1000)$ 座标志性建筑物(建筑物按 $1\dots L$ 顺次编号),以及连接这些建筑物的 $P(2\leqslant P\leqslant5000)$ 条道路。按照计划,那天早上 $Farmer\ John$ 会开车将奶牛们送到某个她们指定的建筑物旁边,等奶牛们完成她们的整个旅行并回到出发点后,将她们接回农场。由于大城市中总是寸土寸金,所有的道路都很窄,政府不得不把它们都设定为通行方向固定的单行道。  
尽管参观那些标志性建筑物的确很有意思,但如果你认为奶牛们同样享受穿行于大城市的车流中的话,你就大错特错了。与参观景点相反,奶牛们把走路定义为无趣且令她们厌烦的活动。对于编号为 $i$ 的标志性建筑物,奶牛们清楚地知道参观它能给自己带来的乐趣值 $F_i (1\leqslant F_i\leqslant1000)$。相对于奶牛们在走路上花的时间,她们参观建筑物的耗时可以忽略不计。  
奶牛们同样仔细地研究过城市中的道路。她们知道第i条道路两端的建筑物 $L1_i$ 和 $L2_i$(道路方向为 $L1_i  \rightarrow L2_i$),以及她们从道路的一头走到另一头所需要的时间 $T_i(1\leqslant T_i\leqslant1000)$。  
为了最好地享受她们的休息日,奶牛们希望她们在一整天中平均每单位时间内获得的乐趣值最大。当然咯,奶牛们不会愿意把同一个建筑物参观两遍,也就是说,虽然她们可以两次经过同一个建筑物,但她们的乐趣值只会增加一次。顺便说一句,为了让奶牛们得到一些锻炼,$Farmer\ John$ 要求奶牛们参观至少 $2$ 个建筑物。  
请你写个程序,帮奶牛们计算一下她们能得到的最大平均乐趣值。

输入格式

* Line $1$: Two space-separated integers: $L$ and $P$

* Lines $2 \ldots L+1$: Line $i+1$ contains a single one integer: $F_i$

* Lines $L+2 \ldots L+P+1$: Line $L+i+1$ describes cow path $i$ with three space-separated integers: $L1_i$ , $L2_i$ , and $T_i$

输出格式

* Line $1$: A single number given to two decimal places (do not perform explicit rounding), the maximum possible average fun per unit time, or $0$ if the cows cannot plan any trip at all in accordance with the above rules.

样例 #1

样例输入 #1

5 7
30
10
10
5
10
1 2 3
2 3 2
3 4 5
3 5 2
4 5 5
5 1 3
5 2 2

样例输出 #1

6.00

 

解题思路

  题目大意是给定一张有向图,每个点有权值 $f_i$,每条边有权值 $t_i$。求图中的一个环使得“环上各点的权值和”除以“环上各边的权值和”最大。

  考虑二分答案,假设二分的值是 $\text{mid}$。用 $\frac{\sum{f_i}}{\sum{t_i}}$ 来表示环中各点与各边的权值和的比值,如果发现某个环满足 $\frac{\sum{f_i}}{\sum{t_i}} > \text{mid}$,则说明答案在右半边区间。对式子推导得到 $\sum{\text{mid} \cdot t_i} - \sum{f_i} < 0$,把环中的点权赋到其出边上(环上的边),那么式子就会变成 $\sum{\text{mid} \cdot t_i - f_i} < 0$,其中可以把 $\text{mid} \cdot t_i - f_i$ 看作是环中边的权值。

  上式说明对于二分值 $\text{mid}$,如果图中存在负环则答案在右半边区间,否则在左半边区间。因此在 $\text{check}$ 函数中,只需对这个图用 spfa 跑最短路找负环即可。

  另外二分的区间是 $[0, 1000]$,右边界的上界是当环中所有点的权值取最大值 $1000$,边的权值取最小值 $1$ 的情况。

  AC 代码如下,最坏情况下的时间复杂度为 $O(nm \log{1000})$:

#include <bits/stdc++.h>
using namespace std;

const int N = 1010, M = 5010;

int n, m;
int w[N];
int head[N], e[M], wt[M], ne[M], idx;
double dist[N];
bool vis[N];
int cnt[N];

void add(int u, int v, int w) {
    e[idx] = v, wt[idx] = w, ne[idx] = head[u], head[u] = idx++;
}

bool check(double mid) {
    queue<int> q;
    for (int i = 1; i <= n; i++) {
        q.push(i);
        cnt[i] = 0;
        vis[i] = true;
    }
    while (!q.empty()) {
        int u = q.front();
        q.pop();
        vis[u] = false;
        for (int i = head[u]; i != -1; i = ne[i]) {
            double t = mid * wt[i] - w[u];
            if (dist[e[i]] > dist[u] + t) {
                dist[e[i]] = dist[u] + t;
                cnt[e[i]] = cnt[u] + 1;
                if (cnt[e[i]] >= n) return true;
                if (!vis[e[i]]) {
                    vis[e[i]] = true;
                    q.push(e[i]);
                }
            }
        }
    }
    return false;
}

int main() {
    scanf("%d %d", &n, &m);
    for (int i = 1; i <= n; i++) {
        scanf("%d", w + i);
    }
    memset(head, -1, sizeof(head));
    while (m--) {
        int u, v, w;
        scanf("%d %d %d", &u, &v, &w);
        add(u, v, w);
    }
    double l = 0, r = 1000;
    while (r - l > 1e-6) {
        double mid = (l + r) / 2;
        if (check(mid)) l = mid;
        else r = mid;
    }
    printf("%.2f", r);
    
    return 0;
}

 

参考资料

  AcWing 361. 观光奶牛(算法提高课):https://www.acwing.com/video/561/