P2935
[USACO09JAN]Best Spot S
题目
约翰拥有P(1<=P<=500)个牧场.贝茜特别喜欢其中的F个.所有的牧场 由C(1 < C<=8000)条双向路连接,第i路连接着ai,bi,需要Ti(1<=Ti< 892)单 位时间来通过.
作为一只总想优化自己生活方式的奶牛,贝茜喜欢自己某一天醒来,到达所有那F个她喜欢的 牧场的平均需时最小.那她前一天应该睡在哪个牧场呢?请帮助贝茜找到这个最佳牧场.
此可见,牧场10到所有贝茜喜欢的牧场的平均距离最小,为最佳牧场.
输入
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
输出
10
点拨
根据题意,我们只需要求出这几个最喜爱的农场相对于每个点的距离,相加求最小值即可
对每个点要求最短路,时间复杂度为O(n^2*logn)
代码
#include <iostream>
#include <utility>
#include <queue>
using namespace std;
typedef long long ll;
#define fi(i, a, b) for (int i = a; i <= b; ++i)
#define fr(i, a, b) for (int i = a; i >= b; --i)
#define x first
#define y second
#define sz(x) ((int)(x).size())
#define pb push_back
using pii = pair<int, int>;
//#define DEBUG
int cnt = 1;
int head[505];
int dis[505][505];
int s[505];
int ans[505];
struct edge
{
int e, w, next;
} edge[16005];
void add(int a, int b, int c)
{
edge[cnt].e = b;
edge[cnt].w = c;
edge[cnt].next = head[a];
head[a] = cnt++;
}
struct node
{
int e, w;
bool operator<(const node p) const
{
return w > p.w;
}
};
int p, f, c;
void djstra(int s)
{
priority_queue<node> pri;
bool vis[505];
fi(i, 0, p) vis[i] = false;
pri.push({s, 0});
while (!pri.empty())
{
node temp = pri.top();
pri.pop();
int x = temp.e, y = temp.w;
vis[x] = true;
// cout << x << endl;
for (int j = head[x]; j != 0; j = edge[j].next)
{
int e = edge[j].e;
int w = edge[j].w;
// cout << e << " " << w << endl;
// cout << dis[s][e] << endl;
if (dis[s][e] >= dis[s][x] + w)
{
dis[s][e] = dis[e][s] = dis[s][x] + w;
// cout << e << " " << dis[s][e] << " ";
if(!vis[e])
pri.push({e, dis[s][e]});
}
}
}
fi(i, 1, p) ans[i] += dis[s][i];
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
#ifdef DEBUG
// freopen(D:\in.txt,r,stdin);
#endif
cin >> p >> f >> c;
fi(i, 1, f) cin >> s[i];
fi(i, 1, p) fi(j, 1, p) dis[i][j] = 0x3f3f3f3f;
fi(i, 1, p) dis[i][i] = 0;
while (c--)
{
int a, b, c1;
cin >> a >> b >> c1;
add(a, b, c1);
add(b, a, c1);
dis[a][b] = dis[b][a] = c1;
}
// djstra(1);
fi(i, 1, f)
{
djstra(s[i]);
}
double res[505];
fi(i,1,p) res[i] = (double)ans[i]/f;
// fi(i, 1, p) cout << res[i] << " ";
// cout << endl;
double minn = 0x3f3f3f;
int minn1;
fi(i,1,p){
if(res[i] < minn){
minn = res[i];
minn1 = i;
}
}
cout << minn1 << endl;
return 0;
}
P2935的更多相关文章
- 洛谷——P2935 [USACO09JAN]最好的地方Best Spot
P2935 [USACO09JAN]最好的地方Best Spot 题目描述 Bessie, always wishing to optimize her life, has realized that ...
- $P2935 [USACO09JAN]最好的地方Best Spot$
P2935 [USACO09JAN]最好的地方Best Spot Floyd的水题(黄题) 海星. 这可能是我第一道发的Floyd的博客 inline void Floyd(){ ;k<=n;k ...
- Luogu P2935 最好的地方Best Spot
Luogu P2935 最好的地方Best Spot 这道题就是一道近乎裸的Floyd,因为数据很小,所以用领接表存图就可以了. #include<bits/stdc++.h> #defi ...
- 洛谷 P2935 [USACO09JAN]最好的地方Best Spot
题目描述 Bessie, always wishing to optimize her life, has realized that she really enjoys visiting F (1 ...
- 计算机系统结构总结_Memory Review
这次就边学边总结吧,不等到最后啦 Textbook: <计算机组成与设计——硬件/软件接口> HI <计算机体系结构——量化研究方法> QR Ch3. Memor ...
- 算法之Floyd-Warshall算法【c++】【图论】【最短路】
我们作为刚学图论的小蒟蒻,先接触到的算法一定是图上最短路径算法.而最短路算法中最简单的当属Floyd-Warshall算法.下面是一些基本介绍: 该算法可以计算图上任意两点间的最短路径 时间复杂度: ...
随机推荐
- pageoffice在线编辑word文件并禁止选中
一.整篇文档禁止选中 wordDoc.setDisableWindowSelection(true); //禁止word的选择文字功能 二.根据条件判断是否禁止选中 比如:选中内容超过一定字数,取消选 ...
- PageOffice实现文件在线安全预览——禁止编辑、下载、复制等
实际应用中,很多时候不仅需要在线编辑文件,还需要在线安全预览文件,要求不能复制.下载.另存到本地等情况.pageoffice可以实现文件在线安全预览,禁止:编辑.复制.粘贴.右键菜单.选择.下载.另存 ...
- mysql命令最新
查看授权 mysql> select user,host from mysql.user; +--------+------------+ | user | host | +--------+- ...
- 关于.net Core在华为云的鲲鹏服务器上部署的细节纪要
由于鲲鹏使用的是ARM的cpu,,非x86的,我们公司买的是Centos,,由于需要在上面部署.net core 3.0/3.1的应用,,在按照官方的文章进行部署之后,会提示 FailFast: Co ...
- AppFlow上新——智谱ChatGLM轻松接入聊天
智谱AI 开放平台提供一系列具有不同功能和定价的大模型,包括通用大模型.超拟人大模型.图像大模型.向量大模型等,并且支持使用您的私有数据对模型进行微调.其中ChatGLM系列模型在国内也享有盛名,现在 ...
- NOIP模拟90(多校23)
T1 回文 解题思路 原来 \(n^3\) 可以过 500 ... 先枚举一下路径长度,对于同一路径长度点数最多是 \(n\) 个,我们可以接着枚举从 \((n,m)\) 出发的路径长度相同的点. 然 ...
- C# 指针简单使用
1. 使用unsafe C# 支持 unsafe 上下文,你可在其中编写不可验证的代码. 在 unsafe 上下文中,代码可使用指针.分配和释放内存块,以及使用函数指针调用方法. C# 中的不安全代码 ...
- Vue第三方库与插件实战手册
title: Vue第三方库与插件实战手册 date: 2024/6/8 updated: 2024/6/8 excerpt: 这篇文章介绍了如何在Vue框架中实现数据的高效验证与处理,以及如何集成E ...
- C#.NET 使用HttpWebRequest发送JSON
方法: public static string HttpPostJson(string url, string postStr, int timeOut, string charset) { Htt ...
- C#.NET 简单使用log4net
1.nuget 中安装log4net 2.添加一个配置文件"log4net2.config",内容如下: <?xml version="1.0" enco ...