NC24755 [USACO 2010 Dec S]Apple Delivery
题目
题目描述
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 (1 <= <= P) and (1 <= <= P) with a distance between them of . The sum of all the distances 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:
输出描述
- 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,PB->PA2->PA1
的最小值即可。
时间复杂度
空间复杂度
代码
#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; }
本文来自博客园,作者:空白菌,转载请注明原文链接:https://www.cnblogs.com/BlankYang/p/17023217.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧