[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. centos 文件系统权限

    模板:drwxrwxrwx r表是读 (Read) .w表示写 (Write) .x表示执行 (eXecute) 读.写.运行三项权限可以用数字表示,就是r=4,w=2,x=1, 777就是rwxrw ...

  2. JDK源码阅读-------自学笔记(十七)(java.io.File类)

    File类简介 java.io.File类:抽象代表文件和目录. 使用此类,相当于获取了系统的文件,可以对其进行操作. 在开发中,读取文件.生成文件.删除文件.修改文件的属性时经常会用到本类 File ...

  3. uniapp 微信支付,小程序支付,支付宝支付问题汇总

    背景介绍 uni-app 可以转微信小程序或直接打包 APP ,支付模块使用统一方法 uni.requestPayment 但是不同平台参数不同容易出现混淆错乱. 相关网站 uni-app 统一支付: ...

  4. requests + tkinter 获取网页数据

    代码: from tkinter import * import requests window = Tk() window.geometry('500x350+500+100') window.ti ...

  5. 使用 Microsoft Edge WebDriver 自动执行和测试 WebView2 应用 Selenium

    https://learn.microsoft.com/zh-cn/microsoft-edge/webview2/how-to/webdriver

  6. winfrom 程序自己删除自己

    [DllImport("kernel32.dll")] public static extern uint WinExec(string lpCmdLine, uint uCmdS ...

  7. wrk压测工具安装和使用

    wrk压测工具安装: mkdir wrk git clone https://github.com/wg/wrk.git cd wrk/ cp wrk /usr/sbin/ wrk压测工具使用 使用方 ...

  8. ConfigurationManager 读取的不是最新值

    用 ConfigurationManager 修改配置后,ConfigurationManager 读取的不是最新值. 解决方法: 第一种: ConfigurationManager.RefreshS ...

  9. PMP 变更专题

    在浏览器Console中输入下列对应命令 document.getElementsByTagName('video')[0].requestPictureInPicture()--进入画中画 docu ...

  10. 燕千云 YQCloud 数智化业务服务平台 发布1.12版本

    2022年4月29日,燕千云 YQCloud 数智化业务服务平台发布1.12版本,优化客户服务场景.深化智能预测服务的应用,加强系统在多渠道方面的集成,全面提升企业数智化服务的能力! 作为企业数字化服 ...