首页 > 其他分享 >[USACO07DEC] Sightseeing Cows G

[USACO07DEC] Sightseeing Cows G

时间:2023-12-30 16:11:23浏览次数:37  
标签:leq int mid cows they Cows 奶牛 USACO07DEC Sightseeing

[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/

标签:leq,int,mid,cows,they,Cows,奶牛,USACO07DEC,Sightseeing
From: https://www.cnblogs.com/onlyblues/p/17936483.html

相关文章

  • P9194 [USACO23OPEN] Triples of Cows P 题解
    Description给定一棵初始有\(n\)个点的树。在第\(i\)天,这棵树的第\(i\)个点会被删除,所有与点\(i\)直接相连的点之间都会两两连上一条边。你需要在每次删点发生前,求出满足\((a,b)\)之间有边,\((b,c)\)之间有边且\(a\not=c\)的有序三元组\((a,b,c)\)对数。\(n\leq2......
  • USACO 2023 US Open Platinum Triples of Cows
    洛谷传送门LOJ传送门hottea.一次删点操作的影响太大了,考虑添加虚点以减小影响(相同的套路在CF1882E2TwoPermutations(HardVersion)也出现过)。具体而言,我们把第\(i\)条边\((u,v)\)变成\((u,n+i),(v,n+i)\)。称编号\(\len\)的点为黑点,编号\(>n\)的点......
  • 【POJ 3275】Ranking the Cows 题解(传递闭包)
    农夫约翰的N头奶牛(1≤N≤1000)产奶率各不相同,FJ希望根据这些比率从最快的奶牛到最慢的奶牛订购奶牛。FJ已经比较了M(1≤M≤10000)对奶牛的产奶率。他想列出另外C对奶牛的列表,这样,如果他现在比较这些C对奶牛,他肯定能够推断出所有N头牛的正确顺序。请帮助他确定C的最小值,这样的列表是可......
  • POJ 2186-Popular Cows ---强连通分量
    本题让求有多少点 是图中所有点都可到达改点的定理:在一个有向图中,如果有一个节点的出度为0,并且仅有一个这样的点,则该图中所有的点都可到达该点先求出图的强连通分量,然后将每个强连通分量化为一个层次,求是否存在一个强连通分量,该分量的出度为一,并且仅有一个这样的分量,则该连通分量......
  • 【LuoGu】3047 Nearby Cows G ——两次DFS+树上DP
    [USACO12FEB]NearbyCowsG题目描述给你一棵\(n\)个点的树,点带权,对于每个节点求出距离它不超过\(k\)的所有节点权值和\(m_i\)。输入格式第一行两个正整数\(n,k\)。接下来\(n-1\)行,每行两个正整数\(u,v\),表示\(u,v\)之间有一条边。最后\(n\)行,每行一个非负整数......
  • 『题解』JOISC2022B 京都観光 (Sightseeing in Kyoto)
    AtCoder题目链接Luogu题目链接观察题目,不自觉地想到了dp,但是再一看\(\text{1e5}\)数据范围,意识到大概是\(2^{\text{1e5}}\)的复杂度,绝望了……然后就很自然地想到了最优策略。(思路很巧妙但是我当时没想到。)考虑有三行(或三列),分别记为\(i,j,k\),如果\(j>i\landj>......
  • P3047 [USACO12FEB] Nearby Cows G
    #include<iostream>#include<vector>usingnamespacestd;constintN=100010,M=30;intn,m;intw[N];vector<int>g[N];intf[N][M],ans[N][M];voidDP1(intu,intfa){ for(inti=0;i<=m;i++)f[u][i]=w[u]; for(intx:g......
  • [USACO06FEB]Treats for the Cows G/S
    [USACO06FEB]TreatsfortheCowsG/S题目描述FJhaspurchasedN(1<=N<=2000)yummytreatsforthecowswhogetmoneyforgivingvastamountsofmilk.FJsellsonetreatperdayandwantstomaximizethemoneyhereceivesoveragivenperiodtime.Th......
  • Codeforces Round #225 (Div. 2)-C. Milking cows
    原题链接C.Milkingcowstimelimitpertestmemorylimitpertestinputoutputn cowssittinginarow,numberedfrom 1 to nIahubcandecidetheorderinwhichhemilksthecows.Buthemustmilkeachcowex......
  • (输出路径搜索)[USACO06OCT] Cows on Skates G
    题目描述本题使用SpecialJudge。FarmerJohn把农场划分为了一个 r 行 c 列的矩阵,并发现奶牛们无法通过其中一些区域。此刻,Bessie位于坐标为 (1,1)(1,1) 的区域,并想到坐标为 (,)(r,c) 的牛棚享用晚餐。她知道,以她所在的区域为起点,每次移动至相邻的四个区域之一,总有......