题目描述
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
题意翻译
贝西有两个又香又脆的红苹果要送给她的两个朋友。当然她可以走的 C(1≤C≤200000)C(1 \le C \le 200000)C(1≤C≤200000) 条“牛路”都被包含在一种常用的图中。这张图同时包含了P(1≤P≤100000)P(1 \le P \le 100000)P(1≤P≤100000) 个牧场,分别被标为 1⋯P1 \cdots P1⋯P。没有“牛路”会从一个牧场又走回它自己。“牛路”是双向的,每条牛路都会被标上一个距离。最重要的是,每个牧场都可以通向另一个牧场。每条牛路都连接着两个不同的牧场 P1_iP1\_iP1_i和P2_iP2\_iP2_i(1≤P1_i,P2_i≤P1 \le P1\_i,P2\_i \le P1≤P1_i,P2_i≤P),距离为D_iD\_iD_i。所有“牛路”的距离之和不大于 200000000020000000002000000000。
现在,贝西要从牧场 PBPBPB 开始给 PA_1PA\_1PA_1 和 PA_2PA\_2PA_2 牧场各送一个苹果(PA_1PA\_1PA_1 和 PA_2PA\_2PA_2 顺序可以调换),那么最短的距离是多少呢?当然,PBPBPB、PA_1PA\_1PA_1 和 PA_2PA\_2PA_2 各不相同。
输入输出样例
输入 #19 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
输出 #1
12
注意,没有“牛路”会从一个牧场又走回它自己。(刚开始读题省略了这句话导致没A)
所以不能从起点到终点1,然后回到起点再到终点2!!!
必须是起点到终点1,终点1到终点2,或者起点到终点2,终点2到终点1
所以,不是所有题的最短路都是从起点到终点1,返回起点,再到另外一个终点2
要注意读题,可能是起点到终点1,终点1到终点2;也可能是起点到终点2,终点2到终点1
(被坑了)
#include <bits/stdc++.h> using namespace std; const int N=200005; struct node { int v, w; bool operator <(const node &A) const { return w>A.w; } }; int n, m, s, t1, t2, u1, v1, w1, dis[N], vis[N], ans=0; vector<node> a[N]; priority_queue<node> q; void dij(int t) { memset(dis, 63, sizeof dis); memset(vis, 0, sizeof vis); dis[t]=0, q.push({t, 0}); while (!q.empty()) { int u=q.top().v; q.pop(); if (vis[u]) continue; vis[u]=1; for (int i=0; i<a[u].size(); i++) { int v=a[u][i].v, w=a[u][i].w; if (!vis[v] && dis[v]>dis[u]+w) { dis[v]=dis[u]+w; q.push({v, dis[v]}); } } } } int main() { scanf("%d%d%d%d%d", &m, &n, &s, &t1, &t2); for (int i=1; i<=m; i++) { scanf("%d%d%d", &u1, &v1, &w1); a[u1].push_back({v1, w1}); a[v1].push_back({u1, w1}); } dij(s); if (dis[t1]<dis[t2]) { ans=dis[t1]; dij(t1); ans+=dis[t2]; } else { ans=dis[t2]; dij(t2); ans+=dis[t1]; } printf("%d", ans); return 0; }
标签:终点,洛谷,Apple,int,题解,2PA,P1,牛路,dis From: https://www.cnblogs.com/didiao233/p/17992525