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

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.

思路:先跑一遍floyed,然后暴力枚举即可。

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define MAXN 510
using namespace std;
int P,F,C,ans;
int maxn=0x7f7f7f7f;
int f[MAXN],map[MAXN][MAXN];
int main(){
    scanf("%d%d%d",&P,&F,&C);
    for(int i=1;i<=F;i++)
        scanf("%d",&f[i]);
    for(int i=1;i<=P;i++)
        for(int j=1;j<=P;j++){
            map[i][j]=999999;
            if(i==j)    map[i][j]=0;
        }
    for(int i=1;i<=C;i++){
        int x,y,z;
        scanf("%d%d%d",&x,&y,&z);
        map[x][y]=map[y][x]=z;
    }
    for(int k=1;k<=P;k++)
        for(int i=1;i<=P;i++)
            for(int j=1;j<=P;j++)
                if(i!=k&&k!=j&&i!=j&&map[i][k]+map[k][j]<map[i][j])
                    map[i][j]=map[i][k]+map[k][j];
    for(int i=1;i<=P;i++){
        long long sum=0;
        for(int j=1;j<=F;j++)
            sum+=map[i][f[j]];
        if(sum<maxn){
            maxn=sum;
            ans=i;
        }
    }
    cout<<ans;
}

原文地址:https://www.cnblogs.com/cangT-Tlan/p/8149910.html

时间: 2024-10-05 05:16:47

洛谷 P2935 [USACO09JAN]最好的地方Best Spot的相关文章

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

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 nu

[USACO09JAN]最好的地方Best Spot

OJ题号:洛谷2935 思路:Floyd 1 #pragma GCC optimize ("O3") 2 #include<cstdio> 3 #include<cctype> 4 #define inf 0x7fffffff>>1 5 inline int min(const int a,const int b) { 6 return a<b?a:b; 7 } 8 inline int getint() { 9 int ch; 10 whil

洛谷 P2936 [USACO09JAN]全流Total Flow

题目描述 Farmer John always wants his cows to have enough water and thus has made a map of the N (1 <= N <= 700) water pipes on the farm that connect the well to the barn. He was surprised to find a wild mess of different size pipes connected in an appa

洛谷——P2936 [USACO09JAN]全流Total Flow

题目描述 Farmer John always wants his cows to have enough water and thus has made a map of the N (1 <= N <= 700) water pipes on the farm that connect the well to the barn. He was surprised to find a wild mess of different size pipes connected in an appa

Luogu P2935 最好的地方Best Spot

Luogu P2935 最好的地方Best Spot 这道题就是一道近乎裸的Floyd,因为数据很小,所以用领接表存图就可以了. #include<bits/stdc++.h> #define INF 0x3fffffff #define N 510 using namespace std; int p,f,c,mmin=INF,sum,ans; int like[N],e[N][N]; void Read() { scanf("%d%d%d",&p,&f,

洛谷 P2709 BZOJ 3781 小B的询问

题目描述 小B有一个序列,包含N个1~K之间的整数.他一共有M个询问,每个询问给定一个区间[L..R],求Sigma(c(i)^2)的值,其中i的值从1到K,其中c(i)表示数字i在[L..R]中的重复次数.小B请你帮助他回答询问. 输入输出格式 输入格式: 第一行,三个整数N.M.K. 第二行,N个整数,表示小B的序列. 接下来的M行,每行两个整数L.R. 输出格式: M行,每行一个整数,其中第i行的整数表示第i个询问的答案. 输入输出样例 输入样例#1: 6 4 3 1 3 2 1 1 3

洛谷1231 教辅的组成

洛谷1231 教辅的组成 https://www.luogu.org/problem/show?pid=1231 题目背景 滚粗了的HansBug在收拾旧语文书,然而他发现了什么奇妙的东西. 题目描述 蒟蒻HansBug在一本语文书里面发现了一本答案,然而他却明明记得这书应该还包含一份练习题.然而出现在他眼前的书多得数不胜数,其中有书,有答案,有练习册.已知一个完整的书册均应该包含且仅包含一本书.一本练习册和一份答案,然而现在全都乱做了一团.许多书上面的字迹都已经模糊了,然而HansBug还是可

洛谷教主花园dp

洛谷-教主的花园-动态规划 题目描述 教主有着一个环形的花园,他想在花园周围均匀地种上n棵树,但是教主花园的土壤很特别,每个位置适合种的树都不一样,一些树可能会因为不适合这个位置的土壤而损失观赏价值. 教主最喜欢3种树,这3种树的高度分别为10,20,30.教主希望这一圈树种得有层次感,所以任何一个位置的树要比它相邻的两棵树的高度都高或者都低,并且在此条件下,教主想要你设计出一套方案,使得观赏价值之和最高. 输入输出格式 输入格式: 输入文件garden.in的第1行为一个正整数n,表示需要种的

洛谷 P2801 教主的魔法 题解

此文为博主原创题解,转载时请通知博主,并把原文链接放在正文醒目位置. 题目链接:https://www.luogu.org/problem/show?pid=2801 题目描述 教主最近学会了一种神奇的魔法,能够使人长高.于是他准备演示给XMYZ信息组每个英雄看.于是N个英雄们又一次聚集在了一起,这次他们排成了一列,被编号为1.2.…….N. 每个人的身高一开始都是不超过1000的正整数.教主的魔法每次可以把闭区间[L, R](1≤L≤R≤N)内的英雄的身高全部加上一个整数W.(虽然L=R时并不