打卡信奥刷题(1294)用C++实现信奥 P2935 [USACO09JAN] Best Spot S

P2935 [USACO09JAN] Best Spot S

题目描述

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)P(1 \leq P \leq 500)P(1P500) 个牧场,贝茜特别喜欢其中的 FFF 个。所有的牧场由 C(1<C≤8000)C(1 < C \leq 8000)C(1<C8000)条双向路连接,第 iii 条路连接着 ai,bia_i,b_iai,bi,需要 Ti(1≤Ti<892)T_i(1 \leq T_i < 892)Ti(1Ti<892) 个单位时间来通过。

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

由样例解释可见,在样例环境下,牧场 101010 到所有贝茜喜欢的牧场的平均距离最小,为最佳牧场。

输入格式

* 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

输入 #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.

C++实现

#include<bits/stdc++.h>
const int inf=0x3fffffff;
using namespace std;
int n,m,p,like[501];
int ans,mi=inf,f[501][501];
int main()
{
	int i,j,k,u,v,c,sum;
	cin>>n>>p>>m;
	for(i=1;i<=n;i++)
	{
	  for(j=1;j<=n;j++) f[i][j]=inf;
	  f[i][i]=0;
	}
	for(i=1;i<=p;i++) cin>>like[i];
	for(i=1;i<=m;i++)
	{
	  cin>>u>>v>>c;
	  f[u][v]=c;
	  f[v][u]=c;
	}
	for(k=1;k<=n;k++)
	  for(i=1;i<=n;i++)
	    for(j=1;j<=n;j++)
	      f[i][j]=min(f[i][j],f[i][k]+f[k][j]);//DP动态转移方程
	for(i=1;i<=n;i++)
	{
	  sum=0;
	  for(j=1;j<=p;j++) sum+=f[i][like[j]];
	  if(sum<mi){mi=sum; ans=i;}
	}
	cout<<ans<<endl;
	return 0;
}

在这里插入图片描述

后续

接下来我会不断用C++来实现信奥比赛中的算法题、GESP考级编程题实现、白名单赛事考题实现,记录日常的编程生活、比赛心得,感兴趣的请关注,我后续将继续分享相关内容

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值