poj3621 Sightseeing Cows

177 篇文章 0 订阅
125 篇文章 0 订阅

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.

经典的01分数规划问题:最优比率生成环。
与他相似的问题还有poj2728最优比率生成树【见这里】
首先确定一点,得到的环一定是简单环。这一部分的说明放到最后。
这样的话,边的收益就可以看成是他终点的收益了。
设最优比率为r,满足r=Σv/Σc,(v表示收益,c表示费用)即r* Σc-Σv=0,Σ(r* c-v)=0,则对于任何可能的比率r’,都存在一种方案使Σ(r’ * c-v)<=0。对于任何非法的r’都有Σ(r’ * c-v)>0。
于是可以二分答案,验证mid时在以mid*c-v为边权的图上判断是否有负环即可。
最后说明为什么答案一定是简单环。假设最优解是由两个环相交而来,设交点收益为a,两个环记作1,2。
有(v1+v2+a)/(c1+c2)>(a+v1)/c1…(1)
(v1+v2+a)/(c1+c2)>(a+v2)/c2…(2)
化简得c1v2>v1c2+ac2…(3)
c2v1>v2c1+ac1…(4)
则c1v2>v1c2+ac2>v1c2>v2c1+ac1>v2c1,矛盾。
故答案一定是简单环。

#include<cstdio>
#include<cstring>
#include<queue>
#include<cmath>
using namespace std;
const double eps=1e-3;
int fir[1010],ne[5010],to[5010],cost[5010],val[1010],cnt[1010],m,n;
double dis[1010];
void init()
{
    int i,j,k,x,y,z;
    scanf("%d%d",&n,&m);
    for (i=1;i<=n;i++)
      scanf("%d",&val[i]);
    for (i=1;i<=m;i++)
    {
        scanf("%d%d%d",&x,&to[i],&cost[i]);
        ne[i]=fir[x];
        fir[x]=i;
    }
}
bool find(double x)
{
    queue<int> q;
    int i,j,k,p;
    for (i=1;i<=n;i++)
      q.push(i);
    memset(cnt,0,sizeof(cnt));
    memset(dis,0,sizeof(dis));
    while (!q.empty())
    {
        p=q.front();
        q.pop();
        for (i=fir[p];i;i=ne[i])
          if (x*cost[i]-val[to[i]]+dis[p]<dis[to[i]])
          {
            dis[to[i]]=x*cost[i]-val[to[i]]+dis[p];
            if (cnt[to[i]]==n-1) return 1;
            cnt[to[i]]++;
            q.push(to[i]);
          }
    }
    return 0;
}
void solve()
{
    int i,j,k;
    double x,y,z,l,r,mid;
    l=0;
    r=1e6;
    while (r-l>=eps)
    {
        mid=(l+r)/2;
        if (find(mid)) l=mid;
        else r=mid;
    }
    if (l<eps) printf("0\n");
    else printf("%.2f\n",l);
}
int main()
{
    init();
    solve();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值