POJ - 3621 Sightseeing Cows【0/1分数规划】【最优比率环】

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 } )
其中 { v i } 为环上结点的集合, { e i } 为环上的边的集合

仿照0/1分数规划模型
不难想到要二分一个mid然后判定图上是否存在一个环S
使得 i = 1 t F u n [ v i ] i = 1 t T i m [ e i ] > m i d
即该环是否满足 i = 1 t ( F u n [ v i ] m i d T i m [ e i ] ) > 0
若是则 L = m i d ,否则 R = m i d

但是这样在图中判定显然很麻烦
所以我们试着把上式两边同时乘-1
变成 i = 1 t ( m i d T i m [ e i ] F u n [ v i ] ) < 0
显然这样我们就把问题转化成了判定图中是否存在负环

到了这里方法已经很显然了
对于一条入点为 u i ,出点为 v i 的边 e i
我们把这条边的边权看作 m i d T i m [ e i ] F u n [ u i ]
然后在图上判负环,若有负环则 L = m i d ,否则 R = m i d

扫描二维码关注公众号,回复: 2198006 查看本文章

#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;
}

猜你喜欢

转载自blog.csdn.net/niiick/article/details/80931758