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.
####Input
- 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
Output
- 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.
0/1分数规划经典题
为方便描述,记环
S
=
(
{
v
i
}
,
{
e
i
}
)
S=(\{v_i\},\{e_i\})
S=({vi},{ei}),
其中
{
v
i
}
\{v_i\}
{vi}为环上结点的集合,
{
e
i
}
\{e_i\}
{ei}为环上的边的集合
仿照0/1分数规划模型
不难想到要二分一个mid然后判定图上是否存在一个环S
使得
∑
i
=
1
t
F
u
n
[
v
i
]
∑
i
=
1
t
T
i
m
[
e
i
]
>
m
i
d
\frac{\sum_{i=1}^tFun[v_i]}{\sum_{i=1}^tTim[e_i]}>mid
∑i=1tTim[ei]∑i=1tFun[vi]>mid
即该环是否满足
∑
i
=
1
t
(
F
u
n
[
v
i
]
−
m
i
d
∗
T
i
m
[
e
i
]
)
>
0
\sum_{i=1}^t(Fun[v_i]-mid*Tim[e_i])>0
∑i=1t(Fun[vi]−mid∗Tim[ei])>0
若是则
L
=
m
i
d
L=mid
L=mid,否则
R
=
m
i
d
R=mid
R=mid
但是这样在图中判定显然很麻烦
所以我们试着把上式两边同时乘-1
变成
∑
i
=
1
t
(
m
i
d
∗
T
i
m
[
e
i
]
−
F
u
n
[
v
i
]
)
<
0
\sum_{i=1}^t(mid*Tim[e_i]-Fun[v_i])<0
∑i=1t(mid∗Tim[ei]−Fun[vi])<0
显然这样我们就把问题转化成了判定图中是否存在负环
到了这里方法已经很显然了
对于一条入点为
u
i
u_i
ui,出点为
v
i
v_i
vi的边
e
i
e_i
ei
我们把这条边的边权看作
m
i
d
∗
T
i
m
[
e
i
]
−
F
u
n
[
u
i
]
mid*Tim[e_i]-Fun[u_i]
mid∗Tim[ei]−Fun[ui]
然后在图上判负环,若有负环则
L
=
m
i
d
L=mid
L=mid,否则
R
=
m
i
d
R=mid
R=mid
#include<iostream>
#include<map>
#include<algorithm>
#include<queue>
#include<cstring>
#include<cstdio>
using namespace std;
typedef double dd;
int read()
{
int f=1,x=0;
char ss=getchar();
while(ss<'0'||ss>'9'){if(ss=='-')f=-1;ss=getchar();}
while(ss>='0'&&ss<='9'){x=x*10+ss-'0';ss=getchar();}
return f*x;
}
const int maxn=1010;
int n,m;
int a[maxn];
struct node{int v,dis,nxt;}E[5010];
int head[maxn],tot;
int vis[maxn],num[maxn];
dd d[maxn];
void add(int u,int v,int dis)
{
E[++tot].nxt=head[u];
E[tot].v=v; E[tot].dis=dis;
head[u]=tot;
}
int check(dd x)
{
queue<int> q;
for(int i=1;i<=n;++i)//因为图不一定连通,所以初始所有结点入队
{
q.push(i);
d[i]=0; vis[i]=num[i]=1;
}
while(!q.empty())
{
int u=q.front();
q.pop(); vis[u]=0;
for(int i=head[u];i;i=E[i].nxt)
{
int v=E[i].v; dd dis=(dd)E[i].dis;
if(d[v]>d[u]+x*dis-(dd)a[u])//边权为mid*Tim[e_i]-Fun[u_i]
{
d[v]=d[u]+x*dis-(dd)a[u];
if(!vis[v])
{
q.push(v); vis[v]=1;
if(++num[v]>=n) return 1;
}
}
}
}
return 0;
}
int main()
{
n=read();m=read();
for(int i=1;i<=n;++i)a[i]=read();
for(int i=1;i<=m;++i)
{
int u=read(),v=read(),dis=read();
add(u,v,dis);
}
dd L=0,R=1000010,mid;
while(R-L>1e-4)
{
mid=(L+R)/2;
if(check(mid))L=mid;
else R=mid;
}
printf("%.2lf",L);
return 0;
}