POJ-3621: Sightseeing Cows【最优比率生成环】

Sightseeing Cows
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 10276 Accepted: 3510

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


题意:n个点,m条边,每个点有自己的值p[i],m条单向边,每条边有自己的值val[i],求一个环,环中的点的值之和比上边值的和最大,即Σp[i]/Σval[i]最大,求这个最大值。


之前写过的01分数规划的更高级的应用吧……中间的check函数变成一个spfa,用于判断是否存在一个具有更优答案的环。【简单01分数规划中,check函数用于判断是否存在一个更优解,若存在就继续二分,这个地方也是类似的】

要得到Σp[i]/Σval[i]的最大值,那么设有函数f[i]=val[i]*r-p[i],若f[i]<=0,则有p[i]/val[i]>=r

f[i]<=0,那么就是说存在一个负环,判断是否存在负环,即为cnt[i]>=n?存在:不存在;


#include<stdio.h>
#include<string.h>
#include<queue>
#define eps 0.0000001
using namespace std;
const int maxn=1111;
double p[maxn]; //point
double dis[maxn];
int cnt[maxn],vis[maxn];
int pos[maxn];
int num;
int n,m;
struct node
{
    int en,next;
    double val;
    node(int en=0,int next=0,double val=0.0):en(en),next(next),val(val){}
}E[maxn*10];
void add(int st,int en,double val)
{
    E[num]=node(en,pos[st],val);
    pos[st]=num++;
}
void init()
{
    memset(dis,0x43,sizeof(dis));
    memset(cnt,0,sizeof(cnt));
    memset(vis,0,sizeof(vis));
}
int spfa(double r) //f[i]=val[i]*r-p[i] f[i]<=0 -> p[i]/val[i]>=r
{                //若有负环出现,则说明有f[i]<=0
    queue<int>q;
    init();
    dis[1]=0;
    cnt[1]++;
    q.push(1);
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        vis[u]=0;
        for(int i=pos[u];i!=-1;i=E[i].next)
        {
            int v=E[i].en;
            double val=E[i].val*r-p[v];
            if(dis[v]>dis[u]+val)
            {
                dis[v]=dis[u]+val;
                if(!vis[v])
                {
                    vis[v]=1;
                    cnt[v]++;
                    if(cnt[v]>=n) return 1;
                    q.push(v);
                }
            }
        }
    }
    return 0;
}
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        num=0;
        memset(pos,-1,sizeof(pos));
        for(int i=1;i<=n;i++)
            scanf("%lf",&p[i]); //point
        int x,y;
        double z;
        while(m--)
        {
            scanf("%d%d%lf",&x,&y,&z);
            add(x,y,z);
        }
        double l=0,r=10000;
        while(r-l>=eps)
        {
            double mid=(l+r)/2.0;
            if(spfa(mid))
                l=mid;
            else
                r=mid;
        }
        printf("%.2f\n",l);
    }
    return 0;
}


  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值