Description
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.
题意
给n个点,m条有向边。每个点有对应点值a,每条边有对应权值b。
求一个环,使得
λ=∑a∑b
有最大值。并求出这个最大值。
题解
0/1分数规划。
详细解释请看这: poj2976:dropping tests(0/1分数规划)
显然的0/1分数规划方程。
枚举
λ
,并判断是否可行。
注意可行的条件必须是个环,这个环上的边(x->y)权值v=cost[x->y]-
λ
*val[x]。
因为要求最大值,所以应该找出一条正环,若存在,则猜测值偏小,否则猜测值偏大。判断环可以用SPFA,因为判断负环更方便,所以边权取相反数,即边(x->y)权值v= λ *val[x]-cost[x->y]。
spfa判负环
根据spfa 广搜时的性质可得,第一次更新的点上边数为1,第二次更新的点上边数为2,只有在边数为1的点处理完并弹出后才会处理边数为二的点。
因此,若没有负环,一个点最多入队n次,因为最短边数不可能超过n。
所以只需记录每个点的入队次数并判断即可。
Code
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<vector>
using namespace std;
const int Maxn=1e3+50,Maxm=5e3+50;
const double eps=1e-4;
const int INF=0x3f3f3f3f;
inline int read()
{
char ch=getchar();int i=0,f=1;
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){i=(i<<3)+(i<<1)+ch-'0';ch=getchar();}
return i*f;
}
int n,m;
double l,r;
double val[Maxn],dis[Maxn];
int cnt[Maxn],que[Maxn*Maxn],head,tail;
vector< pair<int,double> >edge[Maxn];
bool exi[Maxn];
inline bool check(double k)
{
fill(dis+1,dis+Maxn,INF);
memset(cnt,0,sizeof(cnt));
memset(exi,0,sizeof(exi));
head=0,tail=1;
que[tail]=1;
cnt[1]=1;
exi[1]=1;
dis[1]=0;
while(head<tail)
{
int now=que[++head];
for(int e=edge[now].size()-1;e>=0;e--)
{
int v=edge[now][e].first;
double w=k*edge[now][e].second-val[now];
if(dis[now]+w<dis[v])
{
dis[v]=dis[now]+w;
if(!exi[v])
{
exi[v]=1;
que[++tail]=v;
cnt[v]++;
if(cnt[v]>=n)return 1;
}
}
}
exi[now]=0;
}
return 0;
}
int main()
{
n=read(),m=read();
for(int i=1;i<=n;i++)
{
val[i]=read();
r+=val[i];
}
for(int i=1;i<=m;i++)
{
int x=read(),y=read();
double z=read();
edge[x].push_back(make_pair(y,z));
}
while(r-l>eps)
{
double mid=(r+l)/2.0;
if(check(mid))l=mid;
else r=mid;
}
printf("%.2f",r);
}