• 洛谷 P2935 [USACO09JAN]最好的地方Best Spot


    题目描述

    Bessie, always wishing to optimize her life, has realized that she really enjoys visiting F (1 <= F <= P) favorite pastures F_i of the P (1 <= P <= 500; 1 <= F_i <= P) total pastures (conveniently

    numbered 1..P) that compose Farmer John's holdings.

    Bessie knows that she can navigate the C (1 <= C <= 8,000) bidirectional cowpaths (conveniently numbered 1..C) that connect various pastures to travel to any pasture on the entire farm. Associated with each path P_i is a time T_i (1 <= T_i <= 892) to traverse that path (in either direction) and two path endpoints a_i and b_i (1 <= a_i <= P; 1 <= b_i <= P).

    Bessie wants to find the number of the best pasture to sleep in so that when she awakes, the average time to travel to any of her F favorite pastures is minimized.

    By way of example, consider a farm laid out as the map below shows, where *'d pasture numbers are favorites. The bracketed numbers are times to traverse the cowpaths.

    
                1*--[4]--2--[2]--3
                         |       |
                        [3]     [4]
                         |       |
                         4--[3]--5--[1]---6---[6]---7--[7]--8*
                         |       |        |         |
                        [3]     [2]      [1]       [3]
                         |       |        |         |
                        13*      9--[3]--10*--[1]--11*--[3]--12*

    The following table shows distances for potential 'best place' of pastures 4, 5, 6, 7, 9, 10, 11, and 12:

          * * * * * * Favorites * * * * * *
     Potential      Pasture Pasture Pasture Pasture Pasture Pasture     Average
    Best Pasture       1       8      10      11      12      13        Distance
    ------------      --      --      --      --      --      --      -----------
        4              7      16       5       6       9       3      46/6 = 7.67
        5             10      13       2       3       6       6      40/6 = 6.67
        6             11      12       1       2       5       7      38/6 = 6.33
        7             16       7       4       3       6      12      48/6 = 8.00
        9             12      14       3       4       7       8      48/6 = 8.00
       10             12      11       0       1       4       8      36/6 = 6.00 ** BEST
       11             13      10       1       0       3       9      36/6 = 6.00
       12             16      13       4       3       0      12      48/6 = 8.00
    

    Thus, presuming these choices were the best ones (a program would have to check all of them somehow), the best place to sleep is pasture 10.

    约翰拥有P(1<=P<=500)个牧场.贝茜特别喜欢其中的F个.所有的牧场 由C(1 < C<=8000)条双向路连接,第i路连接着ai,bi,需要1(1<=Ti< 892)单 位时间来通过.

    作为一只总想优化自己生活方式的奶牛,贝茜喜欢自己某一天醒来,到达所有那F个她喜欢的 牧场的平均需时最小.那她前一天应该睡在哪个牧场呢?请帮助贝茜找到这个最佳牧场.

    此可见,牧场10到所有贝茜喜欢的牧场的平均距离最小,为最佳牧场.

    输入输出格式

    输入格式:

    • Line 1: Three space-separated integers: P, F, and C

    • Lines 2..F+1: Line i+2 contains a single integer: F_i

    • Lines F+2..C+F+1: Line i+F+1 describes cowpath i with three

    space-separated integers: a_i, b_i, and T_i

    输出格式:

    • Line 1: A single line with a single integer that is the best pasture in which to sleep. If more than one pasture is best, choose the smallest one.

    输入输出样例

    输入样例#1:
    13 6 15 
    11 
    13 
    10 
    12 
    8 
    1 
    2 4 3 
    7 11 3 
    10 11 1 
    4 13 3 
    9 10 3 
    2 3 2 
    3 5 4 
    5 9 2 
    6 7 6 
    5 6 1 
    1 2 4 
    4 5 3 
    11 12 3 
    6 10 1 
    7 8 7 
    
    输出样例#1:
    10 
    

    说明

    As the problem statement

    As the problem statement.

    floyd

    ①不读题②手残

    mdzz。。

    屠龙宝刀点击就送

    #include <cstring>
    #include <ctype.h>
    #include <cstdio>
    #define N 505
    
    int F[N],ans1=0x7fffffff,ans2,p,f,c,Map[N][N];
    inline int min(int a,int b) {return a>b?b:a;}
    inline void read(int &x)
    {
        bool f=0;
        register char ch=getchar();
        for(x=0;!isdigit(ch);ch=getchar()) if(ch=='-') f=1;
        for(;isdigit(ch);ch=getchar()) x=x*10+ch-'0';
        x=f?-x:x;
    }
    int main()
    {
        read(p);read(f);read(c);
        for(int i=1;i<=f;++i) read(F[i]);
        memset(Map,1,sizeof(Map));
        for(int i=1;i<=p;i++) Map[i][i]=0;
        for(int x,y,z;c--;)
        {
            read(x);read(y);read(z);
            Map[x][y]=Map[y][x]=min(Map[x][y],z);
        }
        for(int k=1;k<=p;++k)
         for(int i=1;i<=p;++i)
          for(int j=1;j<=p;++j)
           if(Map[i][j]>Map[i][k]+Map[k][j])
           Map[i][j]=Map[i][k]+Map[k][j];
        for(int i=1;i<=p;++i)
        {
            int ans=0;
            for(int j=1;j<=f;++j) ans+=Map[i][F[j]];
            if(ans<ans1) ans1=ans,ans2=i;
        }
        printf("%d
    ",ans2);
        return 0;
    }
    我们都在命运之湖上荡舟划桨,波浪起伏着而我们无法逃脱孤航。但是假使我们迷失了方向,波浪将指引我们穿越另一天的曙光。
  • 相关阅读:
    68、成员列表初始化?
    67、类成员初始化方式?构造函数的执行顺序 ?为什么用成员初始化列表会快一 些?
    64、malloc申请的存储空间能用delete释放吗?
    63、new和delete的实现原理, delete是如何知道释放内存的大小的额?
    62、delete p、delete [] p、allocator都有什么作用?
    60、C++模板是什么,你知道底层怎么实现的?
    nyoj--814--又见拦截导弹(动态规划+贪心)
    hdoj--1950--Bridging signals(二分查找+LIS)
    nyoj--214--单调递增子序列(二)(二分查找+LIS)
    hdoj--1010--Tempter of the Bone(搜索+奇偶剪枝)
  • 原文地址:https://www.cnblogs.com/ruojisun/p/7396604.html
Copyright © 2020-2023  润新知