zoukankan      html  css  js  c++  java
  • 洛谷 P2868 [USACO07DEC]观光奶牛Sightseeing Cows

    题目描述

    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.

    作为对奶牛们辛勤工作的回报,Farmer John决定带她们去附近的大城市玩一天。旅行的前夜,奶牛们在兴奋地讨论如何最好地享受这难得的闲暇。 很幸运地,奶牛们找到了一张详细的城市地图,上面标注了城市中所有L(2 <= L <= 1000)座标志性建筑物(建筑物按1..L顺次编号),以及连接这些建筑物的P(2 <= P <= 5000)条道路。按照计划,那天早上Farmer John会开车将奶牛们送到某个她们指定的建筑物旁边,等奶牛们完成她们的整个旅行并回到出发点后,将她们接回农场。由于大城市中总是寸土寸金,所有的道路都很窄,政府不得不把它们都设定为通行方向固定的单行道。 尽管参观那些标志性建筑物的确很有意思,但如果你认为奶牛们同样享受穿行于大城市的车流中的话,你就大错特错了。与参观景点相反,奶牛们把走路定义为无趣且令她们厌烦的活动。对于编号为i的标志性建筑物,奶牛们清楚地知道参观它能给自己带来的乐趣值F_i (1 <= F_i <= 1000)。相对于奶牛们在走路上花的时间,她们参观建筑物的耗时可以忽略不计。 奶牛们同样仔细地研究过城市中的道路。她们知道第i条道路两端的建筑物 L1_i和L2_i(道路方向为L1_i -> L2_i),以及她们从道路的一头走到另一头所需要的时间T_i(1 <= T_i <= 1000)。 为了最好地享受她们的休息日,奶牛们希望她们在一整天中平均每单位时间内获得的乐趣值最大。当然咯,奶牛们不会愿意把同一个建筑物参观两遍,也就是说,虽然她们可以两次经过同一个建筑物,但她们的乐趣值只会增加一次。顺便说一句,为了让奶牛们得到一些锻炼,Farmer John要求奶牛们参观至少2个建筑物。 请你写个程序,帮奶牛们计算一下她们能得到的最大平均乐趣值。

    输入输出格式

    输入格式:

     

    • 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

     

    输出格式:

     

    • 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.

     

    输入输出样例

    输入样例#1:
    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
    输出样例#1:
    6.00

     思路:根据题目描述,起点的位置是不确定的。但是,因为奶牛们还要回到起点,所以,奶牛们所走的路径是一个环,起点在哪里是不重要的。

    在奶牛们所走的环上,ans=所有的乐趣数/路上消耗的所有的时间。

    路上消耗的所有时间*ans=所有的乐趣数。

    路上消耗的所有时间*ans-所有的乐趣数=0;

    所以我们可以二分答案,ans,然后对于每一个跑n个spfa。

    当路上消耗的所有的时间*ans-所有的乐趣数<0。

    那就说明r还能取更大值,r有更大的解。

    错因:dis数组应该开double。不能置为l<r,因为如果是double,精度会爆,应该置为r-l>0.000001。

    #include<iostream>
    #include<queue>
    #include<cstdio>
    #include<algorithm>
    #include<cstring>
    #define MAXN 15001
    using namespace std;
    int n,m,tot;
    double w[MAXN],dis[MAXN];
    double l=0,r=1000000,ans,mid;
    int val[MAXN],vis[MAXN],num[MAXN];
    int to[MAXN],net[MAXN],cap[MAXN],head[MAXN];
    void add(int u,int v,int w){
        to[++tot]=v;net[tot]=head[u];cap[tot]=w;head[u]=tot;
    }
    bool spfa(int s){
        queue<int>que;
        memset(num,0,sizeof(num));
        memset(vis,0,sizeof(vis));
        memset(dis,0x3f,sizeof(dis));
        que.push(s);
        dis[s]=0;vis[s]=1;num[s]++;
        while(!que.empty()){
            int now=que.front();
            que.pop();
            vis[now]=0;
            for(int i=head[now];i;i=net[i]){
                if(dis[to[i]]>dis[now]+w[i]){
                    dis[to[i]]=dis[now]+w[i];
                    if(!vis[to[i]]){
                        vis[to[i]]=1;
                        num[to[i]]++;
                        que.push(to[i]);
                        if(num[to[i]]>n)    return true;
                    }
                }
            }
        }
        return false;
    }
    void work(){
        for(int i=1;i<=tot;i++)
            w[i]=(double)cap[i]*mid-val[to[i]];
    }
    bool check(){
        for(int i=1;i<=n;i++)
            if(spfa(i))    return true;
        return false;
    }
    int main(){
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++)    scanf("%d",&val[i]);
        for(int i=1;i<=m;i++){
            int u,v,w;
            scanf("%d%d%d",&u,&v,&w);
            add(u,v,w);
        }
        while(r-l>0.0000001){
            mid=(l+r)/2;
            work();
            if(check()){
                ans=mid;
                l=mid;
            }
            else r=mid;
        }
        printf("%.2lf",ans);
    }
    细雨斜风作晓寒。淡烟疏柳媚晴滩。入淮清洛渐漫漫。 雪沫乳花浮午盏,蓼茸蒿笋试春盘。人间有味是清欢。
  • 相关阅读:
    BZOJ3073 Journeys
    UOJ261 【NOIP2016】天天爱跑步 LCA+动态开点线段树
    [bzoj2654] tree 最小生成树kruskal+二分
    【ZJOI2007】【BZOJ1059】矩阵游戏 匈牙利算法
    [Bzoj 2427] [HAOI2010] 软件安装 tarjan缩点+树形DP
    [CQOI2011]放棋子--DP
    BZOJ 3990 排序
    (六)接入层:反向代理,接入层扩容,负载均衡
    (五)伪分布式:你以为,多机就是分布式?
    (四)容量设计:流量高低,对架构究竟有什么影响?
  • 原文地址:https://www.cnblogs.com/cangT-Tlan/p/7518091.html
Copyright © 2011-2022 走看看