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

设一段路程的收益是F,花费是dis,则比率为

,我们要找出最大的r

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

SPFA找环要用DFS

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define maxn 1010
int n,m,a[maxn],head[maxn],num,start;
double ans;
bool vis[maxn];
struct node{
int to,pre,v;
}e[];
void Insert(int from,int to,int v){
e[++num].to=to;
e[num].v=v;
e[num].pre=head[from];
head[from]=num;
}
void dfs(int now,int s1,int s2,int cnt){
if(now==start&&cnt>=){
ans=max(ans,(double)s1/(double)s2);
return;
}
for(int i=head[now];i;i=e[i].pre){
int to=e[i].to;
if(vis[to])continue;
vis[to]=;
dfs(to,s1+a[to],s2+e[i].v,cnt+);
vis[to]=;
}
}
int main(){
freopen("Cola.txt","r",stdin);
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)scanf("%d",&a[i]);
int x,y,z;
for(int i=;i<=m;i++){
scanf("%d%d%d",&x,&y,&z);
Insert(x,y,z);
}
for(start=;start<=n;start++){
dfs(start,,,);
}
printf("%.2lf",ans);
}

10分 暴力

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
const double eps=1e-;
const int mxn=;
int read(){
int x=,f=;char ch=getchar();
while(ch<'' || ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>='' && ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
struct edge{
int v,nxt,w;
double c;
}e[mxn<<];
int hd[mxn],mct=;
void add_edge(int u,int v,int w){
e[++mct].v=v;e[mct].nxt=hd[u];hd[u]=mct;e[mct].w=w;return;
}
bool vis[mxn];
double dis[mxn];
bool SPFA(int u){
vis[u]=;
for(int i=hd[u];i;i=e[i].nxt){
int v=e[i].v;
if(dis[v]>dis[u]+e[i].c){
dis[v]=dis[u]+e[i].c;
if(vis[v] || SPFA(v)){
vis[v]=;return ;
}
}
}
vis[u]=;
return ;
}
int n,m;
int f[mxn];
void restore(double r){
for(int i=;i<=mct;i++)
e[i].c=(double)e[i].w*r-f[e[i].v];
return;
}
bool check(){
for(int i=;i<=n;i++)
if(SPFA(i))return ;
return ;
}
int main(){
int i,j;
int u,v,w;
n=read();m=read();
for(i=;i<=n;i++)
f[i]=read();
for(i=;i<=m;i++){
u=read();v=read();w=read();
add_edge(u,v,w);
}
double l=,r=,ans;
while(r-l>eps){
double mid=(l+r)/;
restore(mid);
if(check()){
ans=mid;
l=mid;
}else r=mid;
}
printf("%.2f\n",ans);
return ;
}

100分 分数规划

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

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

    题目描述 Farmer John has decided to reward his cows for their hard work by taking them on a tour of the ...

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

    题目描述 Farmer John has decided to reward his cows for their hard work by taking them on a tour of the ...

  3. 洛谷P2868 [USACO07DEC]观光奶牛Sightseeing Cows(01分数规划)

    题意 题目链接 Sol 复习一下01分数规划 设\(a_i\)为点权,\(b_i\)为边权,我们要最大化\(\sum \frac{a_i}{b_i}\).可以二分一个答案\(k\),我们需要检查\(\ ...

  4. 洛谷 P2868 [USACO07DEC]观光奶牛Sightseeing Cows 题解

    题面 这道题是一道标准的01分数规划: 但是有一些细节可以优化: 不难想到要二分一个mid然后判定图上是否存在一个环S,该环是否满足∑i=1t(Fun[vi]−mid∗Tim[ei])>0 但是 ...

  5. POJ3621或洛谷2868 [USACO07DEC]观光奶牛Sightseeing Cows

    一道\(0/1\)分数规划+负环 POJ原题链接 洛谷原题链接 显然是\(0/1\)分数规划问题. 二分答案,设二分值为\(mid\). 然后对二分进行判断,我们建立新图,没有点权,设当前有向边为\( ...

  6. 洛谷 2868 [USACO07DEC]观光奶牛Sightseeing Cows

    题目戳这里 一句话题意 L个点,P条有向边,求图中最大比率环(权值(Fun)与长度(Tim)的比率最大的环). Solution 巨说这是0/1分数规划. 话说 0/1分数规划 是真的难,但貌似有一些 ...

  7. P2868 [USACO07DEC]观光奶牛Sightseeing Cows

    P2868 [USACO07DEC]观光奶牛Sightseeing Cows [](https://www.cnblogs.com/images/cnblogs_com/Tony-Double-Sky ...

  8. [USACO07DEC]观光奶牛Sightseeing Cows 二分答案+判断负环

    题目描述 Farmer John has decided to reward his cows for their hard work by taking them on a tour of the ...

  9. Luogu 2868 [USACO07DEC]观光奶牛Sightseeing Cows

    01分数规划复习. 这东西有一个名字叫做最优比率环. 首先这个答案具有单调性,我们考虑如何检验. 设$\frac{\sum_{i = 1}^{n}F_i}{\sum_{i = 1}^{n}T_i} = ...

随机推荐

  1. fusioncharts 用法实例

    支持xml格式和json格式的数据. 用法很简单. 1.需要引入FusionCharts.js. 2.html中定义个id="chart"的div <div id=" ...

  2. Machine Learning in Action(4) Logistic Regression

    从这节算是开始进入“正规”的机器学习了吧,之所以“正规”因为它开始要建立价值函数(cost function),接着优化价值函数求出权重,然后测试验证.这整套的流程是机器学习必经环节.今天要学习的话题 ...

  3. Java基础教程:对象比较排序

    Java基础教程:对象比较排序 转载请标明出处:http://blog.csdn.net/wangtaocsdn/article/details/71500500 有时候需要对对象列表或数组进行排序, ...

  4. vue 的基本语法

    一 . Vue 的介绍 1 . 前端的三大框架 (可以去 GitHub 查看三个框架的 star 星) vue   :  作者尤雨溪, 渐进式的JavaScript 框架 react :  Faceb ...

  5. IOS 获取 文件(UIImage) 创建时间

    Image 在手机里 如果是手机拍的那么能使用ALAssetsLibrary获取一系列 图片的信息 颜色.GPS.尺寸.创建日期等 使用ALAssetsLibrary导入框架AssetsLibrary ...

  6. Sqooop- 使用Sqoop进行数据的导入导出

    Sqoop是Apache旗下的一个开源框架,专门用来做数据的导入和导出. 官网:https://sqoop.apache.org/ Sqoop的安装非常简单,只需要把下载下来的tar包解压设置两个环境 ...

  7. Android 7.1 GUI系统-窗口管理WMS-Surface管理(四)

    Surface的管理 Surface是窗口能真正显示到物理屏幕上的基础,由surfaceflinger管理,可以通过WindowStateAnimator.java中的变量mDrawState来查看每 ...

  8. packettotal.com - PacketTotal - A Free Online PCAP Analysis Engine

    找相似的pcap文件:https://packettotal.com/app/analysis?id=aff96903aae4b73364e027c8a3e85060&name=similar ...

  9. Posix线程编程指南(2)

    这是一个关于Posix线程编程的专栏.作者在阐明概念的基础上,将向您详细讲述Posix线程库API.本文是第2篇将向您讲述线程的创建与取消. 一.概念及作用在单线程程序中,我们经常要用到"全 ...

  10. BZOJ_2111_[ZJOI2010]Perm 排列计数_树形DP+组合数学

    Description 称一个1,2,...,N的排列P1,P2...,Pn是Magic的,当且仅当2<=i<=N时,Pi>Pi/2. 计算1,2,...N的排列中有多少是Magic ...