POJ3621 Sightseeing Cows
Posted autoint
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ3621 Sightseeing Cows相关的知识,希望对你有一定的参考价值。
Sightseeing Cows
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 12362 | Accepted: 4197 |
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.
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.
Sample Input
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
Sample Output
6.00
Source
给定一个n(2 <= n <= 1000)个点,m(2 <= m <= 5000)条边的有向图,给定每个点的点值f(i)和每条边的权值w(i),求一个环使得路径上点权和除以边权和最大。
简单来说就是最优比率生成环。
题解
01分数规划问题,按套路二分答案。
现在问题转化为了判断可行。把边权变成\(mid*T-F\),这样可行的话一个环的和<0,问题转化成了判断负环。
而如何判断负环呢?可以记录入队次数,也可以记录最短路边数。而后一种效率更高。
时间复杂度\(O(nm\log v)\)
#include<iostream>
#include<cstring>
#include<queue>
#define rg register
#define il inline
#define co const
template<class T>il T read(){
rg T data=0,w=1;rg char ch=getchar();
for(;!isdigit(ch);ch=getchar())if(ch=='-') w=-w;
for(;isdigit(ch);ch=getchar()) data=data*10+ch-'0';
return data*w;
}
template<class T>il T read(rg T&x) {return x=read<T>();}
typedef long long ll;
using namespace std;
co int N=1e3+1,M=5e3+1;
co double eps=1e-6;
int n,m,c[N],f[N],x[M],y[M],z[M];
int Head[N],Edge[M],Next[M],tot;
double Leng[M],d[N];
bool v[N];
il void add(int x,int y,double z){
Edge[++tot]=y,Leng[tot]=z,Next[tot]=Head[x],Head[x]=tot;
}
bool judge(double w){
tot=0,memset(Head,0,sizeof Head);
for(int i=1;i<=m;++i) add(x[i],y[i],w*z[i]-f[x[i]]);
queue<int> q;
for(int i=1;i<=n;++i) q.push(i),d[i]=0,v[i]=1;
memset(c,0,sizeof c);
while(q.size()){
int x=q.front();q.pop();
v[x]=0;
for(int i=Head[x];i;i=Next[i]){
int y=Edge[i];
if(d[y]>d[x]+Leng[i]){
d[y]=d[x]+Leng[i];
if((c[y]=c[x]+1)>=n) return 1;
if(!v[y]) q.push(y),v[y]=1;
}
}
}
return 0;
}
int main(){
read(n),read(m);
for(int i=1;i<=n;++i) read(f[i]);
for(int i=1;i<=m;++i) read(x[i]),read(y[i]),read(z[i]);
double l=0,r=1000;
while(r-l>eps){
double mid=(l+r)/2;
if(judge(mid)) l=mid;
else r=mid;
}
printf("%.2f",l);
return 0;
}
以上是关于POJ3621 Sightseeing Cows的主要内容,如果未能解决你的问题,请参考以下文章
POJ 3621 Sightseeing Cows | 01分数规划
POJ 3621 Sightseeing Cows 01分数规划+spfa判正环