[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的更多相关文章

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

    P2935 [USACO09JAN]最好的地方Best Spot 题目描述 Bessie, always wishing to optimize her life, has realized that ...

  2. $P2935 [USACO09JAN]最好的地方Best Spot$

    P2935 [USACO09JAN]最好的地方Best Spot Floyd的水题(黄题) 海星. 这可能是我第一道发的Floyd的博客 inline void Floyd(){ ;k<=n;k ...

  3. Luogu P2935 最好的地方Best Spot

    Luogu P2935 最好的地方Best Spot 这道题就是一道近乎裸的Floyd,因为数据很小,所以用领接表存图就可以了. #include<bits/stdc++.h> #defi ...

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

    题目描述 Bessie, always wishing to optimize her life, has realized that she really enjoys visiting F (1 ...

  5. 计算机系统结构总结_Memory Review

    这次就边学边总结吧,不等到最后啦 Textbook: <计算机组成与设计——硬件/软件接口>  HI <计算机体系结构——量化研究方法>       QR Ch3. Memor ...

  6. 算法之Floyd-Warshall算法【c++】【图论】【最短路】

    我们作为刚学图论的小蒟蒻,先接触到的算法一定是图上最短路径算法.而最短路算法中最简单的当属Floyd-Warshall算法.下面是一些基本介绍: ​该算法可以计算图上任意两点间的最短路径 时间复杂度: ...

随机推荐

  1. ctf_web

    ctfshow web13 访问题目链接 一看是一道文件上传题,上传文件进行测试 上传php会显示 error suffix 因此推测会检测格式 当文件字数超出一定字数时,显示 error file ...

  2. 前后端分离项目(vue+springboot)集成pageoffice实现在线编辑office文件

    前后端分离项目下使用PageOffice原理图 集成步骤 前端 vue 项目 在您Vue项目的根目录下index.html中引用后端项目根目录下pageoffice.js文件.例如: <scri ...

  3. 稳定、省钱的 ClickHouse 读写分离方案:基于 JuiceFS 的主从架构实践

    Jerry 是一家位于北美的科技公司,利用人工智能和机器学习技术,简化汽车保险和贷款的比价和购买流程.在美国,Jerry 的应用在其所属领域排名第一. 随着数据规模的增长,Jerry 在使用 AWS ...

  4. es命令大全,elasticsearch命令详解

    参考链接 Relational DB Elasticsearch 数据库(database) 索引(indices) 表(tables) types 行(rows) documents 字段(colu ...

  5. Python:conda install 和pip install的区别

    pip是个安装包的软件,conda是个环境管理的工具.conda能够安装多个python解释器,pip不行.因此conda在实际开发中是主要用来隔离不同的python版本和Tensorflow& ...

  6. linux下srpm源码包的使用和安装

    目录 一.关于srpm包 二.srpm包和rpm包的区别 三.不对srpm包做修改,直接安装srpm包 四.对srpm包的源码进行修改,然后安装srpm包 一.关于srpm包 ​ SRPM包是Sour ...

  7. 开源项目分享:ChatGPT 控制台聊天应用

    开源项目分享:ChatGPT 控制台聊天应用 分享一个我最近完成的一个小应用,一个ChatGPT 的控制台聊天应用,大家都在搞AI,我也来玩一玩,顺便分享到社区,有兴趣的小伙伴可以去我的github主 ...

  8. 对比使用IConfigurationSectionHandler和ConfigurationSection自定义节点和自定义处理程序

    使用自定义处理程序处理节点的好处我认为是,可以在业务代码中,不必再写一些读取配置文件的非业务代码了,只管取值.如果多处使用该配置节点,其优势更能体现出来.然后突然要增删配置,也只管改节点处理程序的代码 ...

  9. 未能加载文件或程序集“UFIDA.U8.UAP.GcRegister, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null”或它的某一个依赖项

    这是做采购入库单新增的时候遇到的问题,我是采用模仿写用友u8的zpurrkdhead 主表视图和子表视图zpurrkdtail 去构建 xml文档这样做的,但是再做的时候 [cgcroutecode] ...

  10. 深入解析LinkedHashMap

    LinkedHashMap是HashMap的一个子类,保存了记录的插入顺序,在用Iterator遍历LinkedHashMap时,先得到的记录肯定是先插入的,也可以在构造时带参数,按照访问次序排序. ...