题目链接

题目

题目描述

Bessie has two crisp red apples to deliver to two of her friends in the herd. Of course, she travels the C (1 <= C <= 200,000)

cowpaths which are arranged as the usual graph which connects P (1 <= P <= 100,000) pastures conveniently numbered from 1..P: no cowpath leads from a pasture to itself, cowpaths are bidirectional, each cowpath has an associated distance, and, best of all, it is always possible to get from any pasture to any other pasture. Each cowpath connects two differing pastures \(P1_i\) (1 <= \(P1_i\) <= P) and \(P2_i\)​ (1 <= \(P2_i\) <= P) with a distance between them of \(D_i\). The sum of all the distances \(D_i\)​ does not exceed 2,000,000,000.

What is the minimum total distance Bessie must travel to deliver both apples by starting at pasture PB (1 <= PB <= P) and visiting pastures PA1 (1 <= PA1 <= P) and PA2 (1 <= PA2 <= P)

in any order. All three of these pastures are distinct, of course.

Consider this map of bracketed pasture numbers and cowpaths with distances:
3 2 2
[1]-----[2]------[3]-----[4]
\ / \ /
7\ /4 \3 /2
\ / \ /
[5]-----[6]------[7]
1 2
If Bessie starts at pasture [5] and delivers apples to pastures [1] and [4], her best path is:
5 -> 6-> 7 -> 4* -> 3 -> 2 -> 1*
with a total distance of 12.

输入描述

  • Line 1: Line 1 contains five space-separated integers: C, P, PB, PA1, and PA2
  • Lines 2..C+1: Line i+1 describes cowpath i by naming two pastures it connects and the distance between them: \(P1_i, P2_i, D_i\)

输出描述

  • Line 1: The shortest distance Bessie must travel to deliver both apples

示例1

输入

9 7 5 1 4
5 1 7
6 7 2
4 7 2
5 6 1
5 2 4
4 3 2
1 2 3
3 2 2
2 6 3

输出

12

题解

知识点:最短路。

从 \(PB\) 出发必须经过 \(PA1,PA2\) 的最短路,显然我们分别考虑 \(PA1,PA2\) 作为终点即可。

先以三个点为起点跑三次最短路,然后讨论两条路径 PB->PA1->PA2,PB->PA2->PA1 的最小值即可。

时间复杂度 \(O((C+P)\log C)\)

空间复杂度 \(O(C+P)\)

代码

#include <bits/stdc++.h>
#define ll long long using namespace std; const int N = 100007, M = 200007 << 1; template<class T>
struct Graph {
struct edge {
int v, nxt;
T w;
};
int idx;
vector<int> h;
vector<edge> e; Graph(int n, int m) :idx(0), h(n + 1), e(m + 1) {} void clear(int n, int m) {//全局使用时清零,确定范围防止超时
idx = 0;
h.assign(n + 1, 0);
e.assign(m + 1, { 0,0,0 });
} void add(int u, int v, T w) {
e[++idx] = edge{ v,h[u],w };
h[u] = idx;
}
};
Graph<int> g(N, M); void dijkstra(int st, vector<int> &dis) {
dis.assign(dis.size(), 0x3f3f3f3f);
vector<bool> vis(dis.size(), 0);
struct node {
int v, w;
bool operator<(node a) const {
return w > a.w;
}
};
priority_queue<node> pq;
dis[st] = 0;
pq.push({ st,0 });
while (!pq.empty()) {
int u = pq.top().v;
pq.pop();
if (vis[u]) continue;
vis[u] = 1;
for (int i = g.h[u];i;i = g.e[i].nxt) {
int v = g.e[i].v, w = g.e[i].w;
if (dis[v] > dis[u] + w) {
dis[v] = dis[u] + w;
pq.push({ v, dis[v] });
}
}
}
} int main() {
std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int C, P, PB, PA1, PA2;
cin >> C >> P >> PB >> PA1 >> PA2;
for (int i = 1;i <= C;i++) {
int P1, P2, D;
cin >> P1 >> P2 >> D;
g.add(P1, P2, D);
g.add(P2, P1, D);
}
vector<int> disB(P + 1), disA1(P + 1), disA2(P + 1);
dijkstra(PB, disB);
dijkstra(PA1, disA1);
dijkstra(PA2, disA2);
cout << min(disB[PA1] + disA1[PA2], disB[PA2] + disA2[PA1]) << '\n';
return 0;
}

NC24755 [USACO 2010 Dec S]Apple Delivery的更多相关文章

  1. Usaco 2010 Dec Gold Exercise(奶牛健美操)

    /*codevs 3279 二分+dfs贪心检验 堆版本 re一个 爆栈了*/ #include<cstdio> #include<queue> #include<cst ...

  2. USACO Apple Delivery

    洛谷 P3003 [USACO10DEC]苹果交货Apple Delivery 洛谷传送门 JDOJ 2717: USACO 2010 Dec Silver 1.Apple Delivery JDOJ ...

  3. BZOJ 2100: [Usaco2010 Dec]Apple Delivery( 最短路 )

    跑两遍最短路就好了.. 话说这翻译2333 ---------------------------------------------------------------------- #includ ...

  4. USACO翻译:USACO 2013 DEC Silver三题

    USACO 2013 DEC SILVER 一.题目概览 中文题目名称 挤奶调度 农场航线 贝西洗牌 英文题目名称 msched vacation shuffle 可执行文件名 msched vaca ...

  5. USACO翻译:USACO 2014 DEC Silver三题

    USACO 2014 DEC SILVER 一.题目概览 中文题目名称 回程 马拉松 奶牛慢跑 英文题目名称 piggyback marathon cowjog 可执行文件名 piggyback ma ...

  6. [USACO 2017 Dec Gold] Tutorial

    Link: USACO 2017 Dec Gold 传送门 A: 为了保证复杂度明显是从终结点往回退 结果一开始全在想优化建边$dfs$……其实可以不用建边直接$multiset$找可行边跑$bfs$ ...

  7. 洛谷P3003 [USACO10DEC]苹果交货Apple Delivery

    P3003 [USACO10DEC]苹果交货Apple Delivery 题目描述 Bessie has two crisp red apples to deliver to two of her f ...

  8. 洛谷——P3003 [USACO10DEC]苹果交货Apple Delivery

    P3003 [USACO10DEC]苹果交货Apple Delivery 这题没什么可说的,跑两遍单源最短路就好了 $Spfa$过不了,要使用堆优化的$dijkstra$ 细节:1.必须使用优先队列+ ...

  9. 洛谷 P3003 [USACO10DEC]苹果交货Apple Delivery

    洛谷 P3003 [USACO10DEC]苹果交货Apple Delivery 题目描述 Bessie has two crisp red apples to deliver to two of he ...

  10. NC24724 [USACO 2010 Feb S]Chocolate Eating

    NC24724 [USACO 2010 Feb S]Chocolate Eating 题目 题目描述 Bessie has received \(N (1 <= N <= 50,000)\ ...

随机推荐

  1. 修改elasticsearch默认索引返回数

    1. 背景 (1) 客户反映es查询只能返回10000个数据,而需求时返回1.9W个数据,因此需要设置对应索引的默认返回数index.max_result_window (2) 给客户部署的服务以do ...

  2. 海思Hi35xx 通过uboot 读取U盘文件进行固件升级

    前言 基本过程为:uboot 启动后,通过命令将U盘的的文件读取到内存中,再通过uboot 的flash 写入命令将读取到内存中的升级文件写入到flash的固定位置. (一)usb常用命令 uboot ...

  3. [转帖]云数据库是杀猪盘么,去掉中间商赚差价,aws数据库性能提升 10 倍!价格便宜十倍。

    https://tidb.net/blog/021059f1 于是乎dba中的冯大嘴喊出了云数据库就是杀猪盘.让每个公司自建数据库. 那么有没有一种数据库又便宜又好用呢.有 哪就是tidb数据库. 之 ...

  4. [转帖]iptables ip_conntrack_max 调整

    https://www.diewufeiyang.com/post/583.html 一.概念 ==================== -允许的最大跟踪连接条目:CONNTRACK_MAX(默认值是 ...

  5. [转帖]Prometheus监控系统存储容量优化攻略,让你的数据安心保存!

    云原生监控领域不可撼动,Prometheus 是不是就没缺点?显然不是. 一个软件如果什么问题都想解决,就会导致什么问题都解决不好.所以Prometheus 也存在不足,广受诟病的问题就是 单机存储不 ...

  6. 【转帖】Linux 调优篇:虚拟化调优(hugepage 大页内存)* 叁

    一. 大页(HugePages)概念Hugepage的引入二. hugepages相关概念三.Regular Pages 与 HugePagesa.Regular Pagesb.Huge Pages四 ...

  7. [转帖]【杂学第十二篇】oracledb_exporter监听oracle19c数据库出现libclntsh、ORA-12162、ORA-00942异常解决

    http://www.taodudu.cc/news/show-4845374.html docker run -d --name oracledb_exporter --restart=always ...

  8. [转帖]文件系统读写性能fio测试方法及参数详解

    简介 Fio 是一个 I/O 工具,用来对硬件进行压力测试和验证,磁盘IO是检查磁盘性能的重要指标,可以按照负载情况分成照顺序读写,随机读写两大类. Fio支持13种不同的I/O引擎,包括:sync, ...

  9. Ubuntu2204设置固定IP地址

    前言 Ubuntu每次升级都会修改一部分组件. 从1804开始Ubuntu开始使用netplan的方式进行网络设置. 但是不同版本的配置一直在升级与变化. 今天掉进坑里折腾了好久. 所以这边总结一下, ...

  10. Linux应用程序下网络栈参数的简单整理

    somaxconn 该参数应该是决定一个服务能够同时处理多少个网络请求的核心参数. 一个程序能够支持多少个访问参数,是有两部分来决定, 第一部分是somaxconn ,第二部分是应用服务器启动时传递过 ...