题解 | #小红送外卖#

小红送外卖

https://www.nowcoder.com/practice/2850d7c941f6494e82ba74bc899eb512

大水题,跑完dijk后就可以得知 1 的其他点的最短路,然后每次乘2即可,因为是来回

#include <bits/stdc++.h>
using namespace std;
#define int long long
const int INF = 1e18;
const int N = 1e5 + 5;
int __t = 1;
vector<pair<int, int>> a[N];
int dist[N];
void dijkstra(int s) {
    fill(dist, dist + N, INF);
    priority_queue<pair<int, int>, vector<pair<int, int>>,
                   greater<pair<int, int>>>
        pq;
    dist[s] = 0;
    pq.push({0, s});
    while (!pq.empty()) {
        int d = pq.top().first;
        int u = pq.top().second;
        pq.pop();
        if (d > dist[u])
            continue;
        for (auto e : a[u]) {
            int v = e.first;
            int w = e.second;
            if (dist[u] + w < dist[v]) {
                dist[v] = dist[u] + w;
                pq.push({dist[v], v});
            }
        }
    }
}
void solve() {
    int n, m, q;
    cin >> n >> m >> q;
    for (int i = 0; i < m; ++i) {
        int u, v, w;
        cin >> u >> v >> w;
        a[u].emplace_back(v, w);
        a[v].emplace_back(u, w);
    }
    dijkstra(1);
    int ans = 0;
    for (int i = 0; i < q; ++i) {
        int x;
        cin >> x;
        ans += 2 * dist[x];
    }
    cout << ans << "\n";
}
int32_t main() {
#ifdef ONLINE_JUDGE
    ios::sync_with_stdio(false);
    cin.tie(0);
#endif
    // cin >> __t;
    while (__t--)
        solve();
    return 0;
}

全部评论

相关推荐

贪食滴🐶:你说熟悉扣篮的底层原理,有过隔扣职业球员的实战经验吗
点赞 评论 收藏
分享
评论
1
收藏
分享
牛客网
牛客企业服务