POJ 3621 Sightseeing Cows 最大密度环 01分数规划

来源:互联网 发布:淘宝3c认证证书 编辑:程序博客网 时间:2024/05/12 09:43

最大密度环 01分数规划

   首先的一个结论就是,不会存在环套环的问题,即最优的方案一定是一个单独的环,而不是大环套着小环的形式。这个的证明其实非常的简单,大家可以自己想一下(提示,将大环上的收益和记为x1,花费为y1,小环上的为x2,y2。重叠部分的花费为S。表示出来分类讨论即可)。有了这个结论,我们就可以将花费和收益都转移到边上来了,因为答案最终一定是一个环,所以我们将每一条边的收益规定为其终点的收益,这样一个环上所有的花费和收益都能够被正确的统计。

     解决了蛋疼的问题之后,就是01分数规划的部分了,我们只需要计算出D数组后找找有没有正权环即可,不过这样不太好,不是我们熟悉的问题,将D数组全部取反之后,问题转换为查找有没有负权环,用spfa或是bellman_ford都可以。这道题目就是典型的不适合用Dinkelbach,记录一个负权环还是比较麻烦的,所以二分搞定。




Sightseeing Cows
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 7927 Accepted: 2673

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 73010105101 2 32 3 23 4 53 5 24 5 55 1 35 2 2

Sample Output

6.00

Source

USACO 2007 December Gold

[Submit]   [Go Back]   [Status]   [Discuss]




#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <queue>#include <cmath>using namespace std;const int maxn=5500;const double eps=1e-7;int n,m;double fun[maxn/5];struct Edge{    int to,next;    double w;}edge[maxn*2];int Adj[maxn/5],Size;void init(){    memset(Adj,-1,sizeof(Adj));Size=0;}void add_edge(int u,int v,double w){    edge[Size].to=v;    edge[Size].w=w;    edge[Size].next=Adj[u];    Adj[u]=Size++;}double dist[maxn/5];int cq[maxn/5];bool inq[maxn/5];bool spfa(double r){    for(int i=0;i<=n+1;i++) dist[i]=1e30;    memset(cq,0,sizeof(cq));    memset(inq,false,sizeof(inq));    dist[1]=0; queue<int> q;    inq[1]=true; q.push(1); cq[1]=1;    while(!q.empty())    {        int u=q.front(); q.pop();        inq[u]=false;        for(int i=Adj[u];~i;i=edge[i].next)        {            int v=edge[i].to;            double W=edge[i].w*r-fun[v];            if(dist[v]>dist[u]+W)            {                dist[v]=dist[u]+W;                if(!inq[v])                {                    inq[v]=true;                    cq[v]++;                    if(cq[v]>=n) return true;                    q.push(v);                }            }        }    }    return false;}int main(){    while(scanf("%d%d",&n,&m)!=EOF)    {        init();        for(int i=1;i<=n;i++)            scanf("%lf",fun+i);        for(int i=0;i<m;i++)        {            int a,b,c;            scanf("%d%d%d",&a,&b,&c);            add_edge(a,b,c);        }        double low=0.,high=1000.,mid,ans;        while(fabs(low-high)>eps)        {            mid=(low+high)/2.;            if(spfa(mid))            {                ans=low=mid;            }            else high=mid;        }        printf("%.2lf\n",ans);    }    return 0;}


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 钢琴跨八度手短怎么办 弹钢琴手指不灵活怎么办呢? 理发剪不锋利了怎么办 室外宽带线断了怎么办 接宽带光纤太短怎么办 装修光纤网线太短怎么办 宽带入户线断了怎么办 电信有无线没网怎么办 墙里的网线断了怎么办 墙里网线断了怎么办 3根网线断了怎么办 剪了层次的头发怎么办 小米6充电线坏了怎么办 小米6导航信号弱怎么办 麦多多充不了电怎么办 一加数据线坏了怎么办 小米耳机泡水了怎么办 公司拖欠工资公司破产了怎么办 苹果x外壳掉漆怎么办 手机壳按键很硬怎么办 棉质白衣服染色怎么办 白棉t恤混洗染色怎么办 包包被衣服染色了怎么办 白色衣服染了菜汁怎么办 一加3t屏幕刺眼怎么办 怀孕吃了好多杏怎么办 门破了个洞怎么办 钢圈轮毂刮花了怎么办 瓷砖用刀子划了怎么办 陶瓷洗手台裂了怎么办 洗车泵水管坏了怎么办 印胶浆里面渗入了发泡浆怎么办? 管子断在水管里怎么办 衣服上的织带缩水怎么办 真丝衣服拔缝了怎么办 顾客说衣服太花怎么办 铝和碱反应变黑怎么办 40度高温多肉怎么办 沾到医用蓝药水怎么办? 裤子弄上泡沫胶怎么办 苍蝇纸粘衣服上怎么办