洛谷P2868 [USACO07DEC]观光奶牛 Sightseeing Cows

题目描述

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 ≤ L ≤ 1000) major landmarks (conveniently numbered 1.. L) and the P (2 ≤ P ≤ 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 Fi (1 ≤ Fi ≤ 1000) for each landmark i.

The cows also know about the cowpaths. Cowpath i connects landmark L1i to L2i (in the direction L1i -> L2i ) and requires time Ti (1 ≤ Ti ≤ 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 <= L <= 1000)座标志性建筑物(建筑物按1..L顺次编号),以及连接这些建筑物的P(2 <= P <= 5000)条道路。按照计划,那天早上Farmer John会开车将奶牛们送到某个她们指定的建筑物旁边,等奶牛们完成她们的整个旅行并回到出发点后,将她们接回农场。由于大城市中总是寸土寸金,所有的道路都很窄,政府不得不把它们都设定为通行方向固定的单行道。 尽管参观那些标志性建筑物的确很有意思,但如果你认为奶牛们同样享受穿行于大城市的车流中的话,你就大错特错了。与参观景点相反,奶牛们把走路定义为无趣且令她们厌烦的活动。对于编号为i的标志性建筑物,奶牛们清楚地知道参观它能给自己带来的乐趣值F_i (1 <= F_i <= 1000)。相对于奶牛们在走路上花的时间,她们参观建筑物的耗时可以忽略不计。 奶牛们同样仔细地研究过城市中的道路。她们知道第i条道路两端的建筑物 L1_i和L2_i(道路方向为L1_i -> L2_i),以及她们从道路的一头走到另一头所需要的时间T_i(1 <= T_i <= 1000)。 为了最好地享受她们的休息日,奶牛们希望她们在一整天中平均每单位时间内获得的乐趣值最大。当然咯,奶牛们不会愿意把同一个建筑物参观两遍,也就是说,虽然她们可以两次经过同一个建筑物,但她们的乐趣值只会增加一次。顺便说一句,为了让奶牛们得到一些锻炼,Farmer John要求奶牛们参观至少2个建筑物。 请你写个程序,帮奶牛们计算一下她们能得到的最大平均乐趣值。

输入输出格式

输入格式:

  • Line 1: Two space-separated integers: L and P
  • Lines 2..L+1: Line i+1 contains a single one integer: Fi
  • Lines L+2..L+P+1: Line L+i+1 describes cow path i with three space-separated integers: L1i , L2i , and Ti

输出格式:

  • 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:

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

图论 分数规划 最优比率环

试图确认一下自己还会不会这么个东西

确认的结果是已经不会了(cry)

设一段路程的收益是F,花费是dis,则比率为$\frac{\sum F}{\sum dis}=r$ ,我们要找出最大的r

二分答案r,将每条边的边权修改为 “目的地的收益f - 边长度dis*r”,然后SPFA检查图上是否有负环,有负环则r可以增大。

SPFA找环要用DFS

SPFA 之后记得还原vis[]

 1 #include<iostream>
 2 #include<algorithm>
 3 #include<cstring>
 4 #include<cstdio>
 5 #include<cmath>
 6 using namespace std;
 7 const double eps=1e-5;
 8 const int mxn=100010;
 9 int read(){
10     int x=0,f=1;char ch=getchar();
11     while(ch<‘0‘ || ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}
12     while(ch>=‘0‘ && ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();}
13     return x*f;
14 }
15 struct edge{
16     int v,nxt,w;
17     double c;
18 }e[mxn<<1];
19 int hd[mxn],mct=0;
20 void add_edge(int u,int v,int w){
21     e[++mct].v=v;e[mct].nxt=hd[u];hd[u]=mct;e[mct].w=w;return;
22 }
23 bool vis[mxn];
24 double dis[mxn];
25 bool SPFA(int u){
26     vis[u]=1;
27     for(int i=hd[u];i;i=e[i].nxt){
28         int v=e[i].v;
29         if(dis[v]>dis[u]+e[i].c){
30             dis[v]=dis[u]+e[i].c;
31             if(vis[v] || SPFA(v)){
32                 vis[u]=0;return 1;
33             }
34         }
35     }
36     vis[u]=0;
37     return 0;
38 }
39 int n,m;
40 int f[mxn];
41 void restore(double r){
42     for(int i=1;i<=mct;i++)
43         e[i].c=(double)e[i].w*r-f[e[i].v];
44     return;
45 }
46 bool check(){
47     for(int i=1;i<=n;i++)
48         if(SPFA(i))return 1;
49     return 0;
50 }
51 int main(){
52 //    freopen("testdata.in","r",stdin);
53     int i,j;
54     int u,v,w;
55     n=read();m=read();
56     for(i=1;i<=n;i++)
57         f[i]=read();
58     for(i=1;i<=m;i++){
59         u=read();v=read();w=read();
60         add_edge(u,v,w);
61     }
62     double l=0,r=100000;
63     while(r-l>eps){
64         double mid=(l+r)/2;
65         restore(mid);
66         if(check()){
67             l=mid;
68         }else r=mid;
69     }
70     printf("%.2f\n",l);
71     return 0;
72 }
时间: 2024-10-29 19:07:55

洛谷P2868 [USACO07DEC]观光奶牛 Sightseeing Cows的相关文章

洛谷 P2868 [USACO07DEC]观光奶牛Sightseeing Cows

题目描述 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 ≤ L ≤ 1000) major landma

[USACO07DEC]观光奶牛Sightseeing Cows

[USACO07DEC]观光奶牛Sightseeing Cows 题目描述 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

洛谷 P2915 【[USACO08NOV]奶牛混合起来Mixed Up Cows】

类似于n皇后的思想,只要把dfs表示放置情况的数字压缩成一个整数,就能实现记忆化搜索了. 一些有关集合的操作: {i}在集合S内:S&(1<<i)==1: 将{i}加入集合S:S=S|(1<<i): 集合S内包含了{0,1,2,...,n-2,n-1}:S==(1<<n)-1: 1 #include <cstdio> 2 #include <cstring> 3 #include <cmath> 4 using namespa

贪心 洛谷P2870 [USACO07DEC]最佳牛线,黄金Best Cow Line, Gold

[USACO07DEC]最佳牛线,黄金Best Cow Line, Gold 题目描述 FJ is about to take his N (1 ≤ N ≤ 30,000) cows to the annual"Farmer of the Year" competition. In this contest every farmer arranges his cows in a line and herds them past the judges. The contest organ

洛谷 P1535 游荡的奶牛

P1535 游荡的奶牛 题目描述 Searching for the very best grass, the cows are travelling about the pasture which is represented as a grid with N rows and M columns (2 <= N <= 100; 2 <= M <= 100). Keen observer Farmer John has recorded Bessie's position as

洛谷 1345 [USACO5.4]奶牛的电信Telecowmunication

题目描述 农夫约翰的奶牛们喜欢通过电邮保持联系,于是她们建立了一个奶牛电脑网络,以便互相交流.这些机器用如下的方式发送电邮:如果存在一个由c台电脑组成的序列a1,a2,...,a(c),且a1与a2相连,a2与a3相连,等等,那么电脑a1和a(c)就可以互发电邮. 很不幸,有时候奶牛会不小心踩到电脑上,农夫约翰的车也可能碾过电脑,这台倒霉的电脑就会坏掉.这意味着这台电脑不能再发送电邮了,于是与这台电脑相关的连接也就不可用了. 有两头奶牛就想:如果我们两个不能互发电邮,至少需要坏掉多少台电脑呢?请

动态规划 洛谷P1868 饥饿的奶牛

P1868 饥饿的奶牛 题目描述 有一条奶牛冲出了围栏,来到了一处圣地(对于奶牛来说),上面用牛语写着一段文字. 现用汉语翻译为: 有N个区间,每个区间x,y表示提供的x~y共y-x+1堆优质牧草.你可以选择任意区间但不能有重复的部分. 对于奶牛来说,自然是吃的越多越好,然而奶牛智商有限,现在请你帮助他. 输入输出格式 输入格式: 第一行,N,如题 接下来N行,每行一个数x,y,如题 输出格式: 一个数,最多的区间数 输入输出样例 输入样例#1: 3 1 3 7 8 3 4 输出样例#1: 5

洛谷——P2872 [USACO07DEC]道路建设Building Roads

P2872 [USACO07DEC]道路建设Building Roads 题目描述 Farmer John had just acquired several new farms! He wants to connect the farms with roads so that he can travel from any farm to any other farm via a sequence of roads; roads already connect some of the farms

? 洛谷 P3056 [USACO12NOV]笨牛Clumsy Cows

P3056 [USACO12NOV]笨牛Clumsy Cows 题目描述 Bessie the cow is trying to type a balanced string of parentheses into her new laptop, but she is sufficiently clumsy (due to her large hooves) that she keeps mis-typing characters. Please help her by computing th