• 牛客小白月赛2 H 武 【Dijkstra】


    链接:https://www.nowcoder.com/acm/contest/86/H
    来源:牛客网

    题目描述

    其次,Sεlιнα(Selina) 要进行体力比武竞赛。
    在 Sεlιнα 所在的城市,有 个街区,编号为 ,总共有 条的街道连接这些街区, 使得每两个街区之间都直接或间接地有街道将它们相连。Sεlιнα 把通过了文化知识竞赛的参赛男友们召集到她家所在的街区 ,并以这个街区为起点,让所有参赛男友们向其他街区跑去。这些参赛者们被命令不准重复跑某条街道,而且在规定时间内要尽可能地跑远。比赛结束后,所有参赛者将停留在他们此时所在的街区。之后 Sεlιнα 开始视察结果。现在她知道每个街区都有一些她的参赛男友停留着,她现在想先去看看离她家第 近的街区。所以作为一位好帮手,你的任务是要告诉她所有街区中,离 Sεlιнα 家第  近的街区与 Sεlιнα 家之间的距离。 

    输入描述:

    第一行三个整数,,含义同题面描述。
    接下去  行,每行三个整数,,表示从第 个街区到第 个街区有一条权值为 的街道相连。街区从 开始标号。

    输出描述:

    输出共一行,一个整数,表示所有街区与 Sεlιнα 家所在街区之间最近距离的第 
     小值。 
    示例1

    输入

    复制
    3 3 2
    1 2 4
    2 3 5

    输出

    复制
    9
    示例2

    输入

    复制
    6 4 3
    1 2 7
    2 3 2
    2 4 2
    2 5 10
    3 6 3

    输出

    复制
    7

    备注:

    
    
     
    思路:
    求单源最短路,稀疏图一般使用  Dijkstra , 稠密图使用 floyd

    AC码:

    #include <algorithm>
    #include <cstdio>
    #include <cstring>
    #include <string>
    #include <cmath>
    #include <iostream>
    #include <map>
    #include <queue>
    #include <set>
    #include <vector>
     
    using namespace std;
     
     
    #define max3(x, y, z) max(max((x), (y)), (z))
    #define min3(x, y, z) min(mix((x), (y)), (z))
    #define pb push_back
    #define ppb pop_back
    #define mk make_pair
    #define pii pair<int, int>
    #define pll pair<long long, long long>
     
     
     
    #define debug_l(a) cout << #a << " " << (a) << endl
    #define debug_b(a) cout << #a << " " << (a) << " "
    #define testin(filename) freopen((filename) ,"r",stdin)
    #define testout(filename) freopen((filename) ,"w",stdout)
     
    typedef long long ll;
    typedef unsigned long long ull;
     
     
    const double PI  = 3.14159265358979323846264338327;
    const double E   = exp(1);
    const double eps = 1e-6;
     
    const int INF    = 0x3f3f3f3f;
    const int NINF   = 0xc0c0c0c0;
    using namespace std;
     
    const int MAXN = 100005;
     
    struct edge
    {
        int from, to, cost;
        edge(int f, int t, int c) : from(f), to(t), cost(c) {}
    };
    int d[MAXN];
    bool vis[MAXN];
    vector<edge>edges;
    vector<int>G[MAXN];
    typedef pair<int, int> P;
     
    void Dijkstra(int s) {
        priority_queue<P, vector<P>, greater<P> > que;
        fill(d, d + MAXN, INF);
        fill(vis, vis + MAXN, 0);
        d[s] = 0;
        que.push(make_pair(0, s));
        while (!que.empty()) {
            P p = que.top(); que.pop();
            int v = p.second;
            if (d[v] < p.first) continue;        //d[v] < p.first 说明,v 点已经通过其他路径变得松弛,距离更短。而 p.first 只是之前入队的旧元素
            for (int i = 0; i < G[v].size(); i++) {
                edge e = edges[G[v][i]];
                if (d[e.to] > d[v] + e.cost) {
                    d[e.to] = d[v] + e.cost;
                    que.push(P(d[e.to], e.to));
                }
            }
        }
    }
     
     
    int main() {
        //testin("data.in");
        int n, p, k;
        scanf("%d%d%d", &n, &p, &k);
     
        for (int i = 0; i < n - 1; i++) {
            int f, t, c;
            scanf("%d%d%d", &f, &t, &c);
            edges.push_back(edge(f, t, c));
            G[f].push_back(edges.size() - 1);
     
            edges.push_back(edge(t, f, c));
            G[t].push_back(edges.size() - 1);
        }
        Dijkstra(p);
        sort(d, d + n + 2);
        printf("%d
    ", d[k]);
        return 0;
    }
  • 相关阅读:
    Codeforces 1163E 高斯消元 + dfs
    Codeforces 1159E 拓扑排序
    Codeforces 631E 斜率优化
    Codeforces 1167F 计算贡献
    Codeforces 1167E 尺取法
    Gym 102007I 二分 网络流
    Codeforces 319C DP 斜率优化
    Codeforces 1163D DP + KMP
    Comet OJ
    Vue 的响应式原理中 Object.defineProperty 有什么缺陷?为什么在 Vue3.0 采用了 Proxy,抛弃了 Object.defineProperty?
  • 原文地址:https://www.cnblogs.com/TianyuSu/p/9398504.html
Copyright © 2020-2023  润新知