[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 JohnFarmer\ JohnFarmer John决定带她们去附近的大城市玩一天。旅行的前夜,奶牛们在兴奋地讨论如何最好地享受这难得的闲暇。
很幸运地,奶牛们找到了一张详细的城市地图,上面标注了城市中所有L(2⩽L⩽1000)L(2\leqslant L\leqslant1000)L(2⩽L⩽1000)座标志性建筑物(建筑物按1…L1\dots L1…L顺次编号),以及连接这些建筑物的P(2⩽P⩽5000)P(2\leqslant P\leqslant5000)P(2⩽P⩽5000)条道路。按照计划,那天早上Farmer JohnFarmer\ JohnFarmer John会开车将奶牛们送到某个她们指定的建筑物旁边,等奶牛们完成她们的整个旅行并回到出发点后,将她们接回农场。由于大城市中总是寸土寸金,所有的道路都很窄,政府不得不把它们都设定为通行方向固定的单行道。
尽管参观那些标志性建筑物的确很有意思,但如果你认为奶牛们同样享受穿行于大城市的车流中的话,你就大错特错了。与参观景点相反,奶牛们把走路定义为无趣且令她们厌烦的活动。对于编号为iii的标志性建筑物,奶牛们清楚地知道参观它能给自己带来的乐趣值Fi(1⩽Fi⩽1000)F_i (1\leqslant F_i\leqslant1000)Fi(1⩽Fi⩽1000)。相对于奶牛们在走路上花的时间,她们参观建筑物的耗时可以忽略不计。
奶牛们同样仔细地研究过城市中的道路。她们知道第i条道路两端的建筑物L1iL1_iL1i和L2iL2_iL2i(道路方向为L1i→L2iL1_i \rightarrow L2_iL1i→L2i),以及她们从道路的一头走到另一头所需要的时间Ti(1⩽Ti⩽1000)T_i(1\leqslant T_i\leqslant1000)Ti(1⩽Ti⩽1000)。
为了最好地享受她们的休息日,奶牛们希望她们在一整天中平均每单位时间内获得的乐趣值最大。当然咯,奶牛们不会愿意把同一个建筑物参观两遍,也就是说,虽然她们可以两次经过同一个建筑物,但她们的乐趣值只会增加一次。顺便说一句,为了让奶牛们得到一些锻炼,Farmer JohnFarmer\ JohnFarmer John要求奶牛们参观至少222个建筑物。
请你写个程序,帮奶牛们计算一下她们能得到的最大平均乐趣值。
输入输出格式
输入格式:
* 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.
输入输出样例
6.00
二分答案:
我们要求的是:
假设当前答案为 x;
如果可以更新,即:
然后将每一条边的边权设为:
用 spfa 看是否有负环即可;
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<bitset>
#include<ctime>
#include<deque>
#include<stack>
#include<functional>
#include<sstream>
//#include<cctype>
//#pragma GCC optimize(2)
using namespace std;
#define maxn 200005
#define inf 0x7fffffff
//#define INF 1e18
#define rdint(x) scanf("%d",&x)
#define rdllt(x) scanf("%lld",&x)
#define rdult(x) scanf("%lu",&x)
#define rdlf(x) scanf("%lf",&x)
#define rdstr(x) scanf("%s",x)
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int U;
#define ms(x) memset((x),0,sizeof(x))
const long long int mod = 1e9 + 7;
#define Mod 1000000000
#define sq(x) (x)*(x)
#define eps 1e-3
typedef pair<int, int> pii;
#define pi acos(-1.0)
//const int N = 1005;
#define REP(i,n) for(int i=0;i<(n);i++)
typedef pair<int, int> pii;
inline ll rd() {
ll x = 0;
char c = getchar();
bool f = false;
while (!isdigit(c)) {
if (c == '-') f = true;
c = getchar();
}
while (isdigit(c)) {
x = (x << 1) + (x << 3) + (c ^ 48);
c = getchar();
}
return f ? -x : x;
} ll gcd(ll a, ll b) {
return b == 0 ? a : gcd(b, a%b);
}
int sqr(int x) { return x * x; } /*ll ans;
ll exgcd(ll a, ll b, ll &x, ll &y) {
if (!b) {
x = 1; y = 0; return a;
}
ans = exgcd(b, a%b, x, y);
ll t = x; x = y; y = t - a / b * y;
return ans;
}
*/ int n, m;
int a[maxn];
struct node {
int u, v, w;
int nxt;
}edge[maxn];
int head[maxn];
double dis[maxn];
bool vis[maxn];
int num[maxn];
int tot; void addedge(int u, int v, int w) {
edge[++tot].u = u; edge[tot].v = v; edge[tot].w = w;
edge[tot].nxt = head[u]; head[u] = tot;
}
bool check(double x) {
queue<int>q;
for (int i = 1; i <= n; i++) {
q.push(i); vis[i] = num[i] = 1;
dis[i] = 0.0;
}
while (!q.empty()) {
int u = q.front(); q.pop();
vis[u] = 0;
for (int i = head[u]; i; i = edge[i].nxt) {
int v = edge[i].v;
if (dis[v] > dis[u] + x * edge[i].w - (double)a[u]) {
dis[v] = dis[u] + x * edge[i].w - (double)a[u];
if (!vis[v]) {
q.push(v); vis[v] = 1;
num[v]++;
if (num[v] >= n)return true;
}
}
}
}
return false;
} int main() {
//ios::sync_with_stdio(0);
rdint(n); rdint(m);
for (int i = 1; i <= n; i++)rdint(a[i]);
for (int i = 1; i <= m; i++) {
int u, v, w; rdint(u); rdint(v); rdint(w);
addedge(u, v, w);
}
double l = 0, r = 1e5+1;
for (int i = 1; i <= 100; i++) {
double mid = (l + r) / 2.0;
if (check(mid)) {
l = mid;
}
else r = mid;
}
printf("%.2lf\n", 1.0*l);
return 0;
}
[USACO07DEC]观光奶牛Sightseeing Cows 二分答案+判断负环的更多相关文章
- P2868 [USACO07DEC]观光奶牛Sightseeing Cows
P2868 [USACO07DEC]观光奶牛Sightseeing Cows [](https://www.cnblogs.com/images/cnblogs_com/Tony-Double-Sky ...
- 洛谷P2868 [USACO07DEC]观光奶牛Sightseeing Cows
P2868 [USACO07DEC]观光奶牛Sightseeing Cows 题目描述 Farmer John has decided to reward his cows for their har ...
- 洛谷 P2868 [USACO07DEC]观光奶牛Sightseeing Cows
题目描述 Farmer John has decided to reward his cows for their hard work by taking them on a tour of the ...
- 洛谷P2868 [USACO07DEC]观光奶牛 Sightseeing Cows
题目描述 Farmer John has decided to reward his cows for their hard work by taking them on a tour of the ...
- POJ3621或洛谷2868 [USACO07DEC]观光奶牛Sightseeing Cows
一道\(0/1\)分数规划+负环 POJ原题链接 洛谷原题链接 显然是\(0/1\)分数规划问题. 二分答案,设二分值为\(mid\). 然后对二分进行判断,我们建立新图,没有点权,设当前有向边为\( ...
- 洛谷P2868 [USACO07DEC]观光奶牛Sightseeing Cows(01分数规划)
题意 题目链接 Sol 复习一下01分数规划 设\(a_i\)为点权,\(b_i\)为边权,我们要最大化\(\sum \frac{a_i}{b_i}\).可以二分一个答案\(k\),我们需要检查\(\ ...
- Luogu 2868 [USACO07DEC]观光奶牛Sightseeing Cows
01分数规划复习. 这东西有一个名字叫做最优比率环. 首先这个答案具有单调性,我们考虑如何检验. 设$\frac{\sum_{i = 1}^{n}F_i}{\sum_{i = 1}^{n}T_i} = ...
- 洛谷 2868 [USACO07DEC]观光奶牛Sightseeing Cows
题目戳这里 一句话题意 L个点,P条有向边,求图中最大比率环(权值(Fun)与长度(Tim)的比率最大的环). Solution 巨说这是0/1分数规划. 话说 0/1分数规划 是真的难,但貌似有一些 ...
- 洛谷 P2868 [USACO07DEC]观光奶牛Sightseeing Cows 题解
题面 这道题是一道标准的01分数规划: 但是有一些细节可以优化: 不难想到要二分一个mid然后判定图上是否存在一个环S,该环是否满足∑i=1t(Fun[vi]−mid∗Tim[ei])>0 但是 ...
随机推荐
- 使用WindowsAPI获取录音音频的方法
这篇文章主要介绍了使用WindowsAPI获取录音音频的方法,非常实用的功能,需要的朋友可以参考下 本文实例介绍了使用winmm.h进行音频流的获取的方法,具体步骤如下: 一.首先需要包含以下引用对象 ...
- xdebug浏览器调试参数
XDEBUG_SESSION_START=phpstorm-xdebug 找到对应PHP版本的 Xdebug ,后面带 TS 的为线程安全,本机环境为 win7 64 + php-5.5.1-Win3 ...
- LOJ2305 「NOI2017」游戏
「NOI2017」游戏 题目背景 狂野飙车是小 L 最喜欢的游戏.与其他业余玩家不同的是,小 L 在玩游戏之余,还精于研究游戏的设计,因此他有着与众不同的游戏策略. 题目描述 小 L 计划进行$n$场 ...
- bzoj 3926: 诸神眷顾的幻想乡 广义后缀自动机
题目: Description 幽香是全幻想乡里最受人欢迎的萌妹子,这天,是幽香的2600岁生日,无数幽香的粉丝到了幽香家门前的太阳花田上来为幽香庆祝生日. 粉丝们非常热情,自发组织表演了一系列节目给 ...
- 第k小和(搜索)
Description [问题描述] 从n个数中选若干(至少1)个数求和,求所有方案中第k小的和(和相同但取法不同的视为不同方案).[输入格式] 第一行输入2个正整数n,k. 第二行输入这 ...
- JavaScript运行机制与setTimeout
前段时间,老板交给了我一个任务:通过setTimeout来延后网站某些复杂资源的请求.正好借此机会,将JavaScript运行机制和setTimeout重新认真思考一遍,并将我对它们的理解整理如下. ...
- TFS自定义开发中的反射应用
最近CM(Configuration Management) 的同事在自定义开发TFS的过程中遇到一个问题. 领导要求快速开发一个工具, 可以自动连接TFS,然后自动Check out一些word文件 ...
- vs2015解决fopen、fscanf 要求替换为fopen_s、fscanf_s的办法
在工程项目设置一下就行:项目属性 -- C/C++-- 预处理器 -- 预处理器定义,添加:_CRT_SECURE_NO_WARNINGS
- HTTP 2 VS HTTP 1.1
提升H5应用加载速度的方式有很多,比如缓存.cdn加速.代码压缩合并和图片压缩等技术. 今天介绍的是HTTP 2.0
- SharePoint Project Server List 列表CURD操作使用rest api接口
//#region 界面交互代码 var issuesUtils = issuesUtils || {}; (function () { /** * 点击问题提处理方案按钮事件 */ issuesUt ...