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算法.下面是一些基本介绍: 该算法可以计算图上任意两点间的最短路径 时间复杂度: ...
随机推荐
- MySQL如何快速获取binlog的开始时间和结束时间
之前写过一篇文章MySQL如何获取binlog的开始时间和结束时间[1],文章里面介绍了如何获取MySQL数据库二进制日志(binlog)的开始时间与结束时间的一些方法.实际应用当中,我们可能还会遇到 ...
- CentOS7部署Redis(离线单机)
一.检查是否安装 ## 检查是否安装了Redis [root@localhost /]# ps -ef | grep redis ## 存在就删除 [root@localhost /]# sudo y ...
- C++ Virtual Functions
Virtual这个关键字在多态中扮演一个绝对重要的角色,只要member functions声明的前面加上virtual的关键字,他就会成为 Virtual member functions.任何一个 ...
- objectarx 之让用户自定义插件命令
#include <iostream> #include <fstream> virtual AcRx::AppRetCode On_kInitAppMsg (void *pk ...
- CSS——float浮动属性
流动布局 流动模型(Flow),即文档流,浏览器打开HTML网页时,从上往下,从左往右,逐一加载. 在正常情况下,HTML元素都会根据文档流来分布网页内容的. 文档流有2大特征: ① 块状元素会随着浏 ...
- 如何在Windows上一键部署PaddleOCR的WebAPI服务
PaddleOCR旨在打造一套丰富.领先.且实用的OCR工具库,助力开发者训练出更好的模型,并应用落地. 官方开源项目地址:PaddlePaddle/PaddleOCR: Awesome multi ...
- BOM弹窗 滚动条
// window.alert('弹出内容') 警告框 // window.prompt('弹出内容') 输入框 // 以 字符串 形式 存储输入 ...
- v-once指令 v-if和v-show
v-once指令 只渲染元素和组件一次,之后元素和组件将失去响应式功能 v-if和v-show 根据表达式的布尔值(true/false)进行判断是否渲染该元素 注:v-if 有更高的切换开销,而 v ...
- 如果设备不支持vulkan,就用swiftshader,否则就加载系统的vulkan的正确姿势(让程序能够智能的在vulkan-1.dll和libvk_swiftshader.dll之间切换)
一些老的显卡设备没有更新驱动,甚至根本就不支持Vulkan的显卡,遇到静态链接的vulkan-1.lib文件,启动exe就会崩溃. 你以为从别的机器拷贝这个vulkan-1.dll就可以了吗? 太傻太 ...
- kettle从入门到精通 第五十一课 ETL之kettle Avro input
1.我们在学习nifi的过程中有接触到Avro schema,当时我在想kettle应该也支持Avro,果不其然kettle也是支持Avro文件的读和写的.今天我们一起来学习下kettle中如何使用A ...