题目链接
题目描述
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 L1iL1i to L2iL2i (in the direction L1iL1i -> L2iL2i ) 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.
输入描述:
* 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: L1iL1i , L2iL2i , 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.
一开始看到这题的时候,会往Desert King【01分数规划+最有比例生成树】上面去想因为这里约束了上线,同时求的是一个比例值,那么我们可以同样的额推一下,就会得到∑w-x*∑e>0这样的一个递推式,然后,我们去spfa上面维护一个入队次数,也就是维护有没有正环这样的一个东西。
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
//#define INF 0x3f3f3f3f
#define INF 1e9 + 7.
#define efs 1e-6
#define HalF (l + r)>>1
#define lsn rt<<1
#define rsn rt<<1|1
#define Lson lsn, l, mid
#define Rson rsn, mid+1, r
#define QL Lson, ql, qr
#define QR Rson, ql, qr
#define myself rt, l, r
#define MP(a, b) make_pair(a, b)
#define MP3(a, b, c) MP(MP(a, b), c)
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int maxN = 1005, maxE = 1e4 + 7;
int N, M, head[maxN], cnt, du[maxN], used[maxN];
double a[maxN];
struct Eddge
{
int nex, to;
double val;
Eddge(int a=-1, int b=0, double c=0.):nex(a), to(b), val(c) {}
}edge[maxE];
inline void addEddge(int u, int v, double w)
{
edge[cnt] = Eddge(head[u], v, w);
head[u] = cnt++;
}
queue<int> Q;
bool inque[maxN];
double dist[maxN];
inline bool spfa(double x)
{
for(int i=1; i<=N; i++) { used[i] = 0; dist[i] = -INF; inque[i] = false; }
while(!Q.empty()) Q.pop();
bool flag = false;
for(int i=1; i<=N; i++)
{
if(!du[i])
{
flag = true;
inque[i] = true;
Q.push(i);
used[i]++;
dist[i] = a[i];
}
}
if(!flag)
{
Q.push(1);
inque[1] = true;
used[1]++;
dist[1] = a[1];
}
while(!Q.empty())
{
int u = Q.front(); Q.pop(); inque[u] = false;
for(int i=head[u], v; ~i; i=edge[i].nex)
{
v = edge[i].to;
double w = edge[i].val, r = a[v] - x * w;
if(dist[v] < dist[u] + r)
{
dist[v] = dist[u] + r;
if(!inque[v])
{
inque[v] = true;
Q.push(v);
if(++used[v] > N) return true;
}
}
}
}
return false;
}
int main()
{
scanf("%d%d", &N, &M);
for(int i=1; i<=N; i++) head[i] = -1;
memset(inque, false, sizeof(inque));
for(int i=1; i<=N; i++) scanf("%lf", &a[i]);
for(int i=1, u, v; i<=M; i++)
{
double w;
scanf("%d%d%lf", &u, &v, &w);
addEddge(u, v, w);
du[v]++;
}
while(!Q.empty()) Q.pop();
bool flag = false;
for(int i=1; i<=N; i++)
{
if(!du[i])
{
flag = true;
inque[i] = true;
Q.push(i);
}
}
if(!flag)
{
Q.push(1);
inque[1] = true;
}
double ans = 0., l = 0., r = 1000., mid = 0.;
while(fabs(r - l) > efs)
{
mid = (l + r) / 2.;
if(spfa(mid))
{
l = mid;
ans = mid;
}
else r = mid;
}
printf("%.2lf\n", ans);
return 0;
}