最短路-迪傑斯特拉(dijkstra)

LANSGANBS發表於2024-05-23

迪傑斯特拉(dijkstra)

//
// Created by LANSGANBS on 2024/3/8.
//
/*
 * code template: https://github.com/LANSGANBS/code-template
 * URL:
 * Status:
 * 寫完這道就去原
*/
#include <bits/stdc++.h>

using namespace std;
using i64 = long long;
#define endl '\n'
#define buff ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
#define debug cout << "********************************************" << endl;
#define YES cout << "YES" << endl;
#define NO cout << "NO" << endl;
#define Yes cout << "Yes" << endl;
#define No cout << "No" << endl;
#define yes cout << "yes" << endl;
#define no cout << "no" << endl;
#define REP(i, a, b) for (int i = a; i <= b; i++)
#define _REP(i, b, a) for (int i = b; i >= a; i--)
#define rep(i, a, b) for (int i = a; i < b; i++)
#define _rep(i, b, a) for (int i = b; i > a; i--)
#define all(x) begin(x), end(x)
#define mem(a, x) memset(a, x, sizeof(a))
#define gcd(a, b) __gcd(a, b)
#define lcm(a, b) (a / gcd(a, b) * b)
#define sz(x) (int)x.size()
#define lowbit(x) (x & -x)
#define pb push_back
#define ll long long
#define int ll
#define ld long double
#define vi vector<int>

i64 ceilDiv(i64 n, i64 m) // up
{
    if (n >= 0)
    {
        return (n + m - 1) / m;
    }
    else
    {
        return n / m;
    }
}

i64 floorDiv(i64 n, i64 m) // low
{
    if (n >= 0)
    {
        return n / m;
    }
    else
    {
        return (n - m + 1) / m;
    }
}

template<typename T1, typename T2>
istream &operator>>(istream &cin, pair<T1, T2> &a)
{
    return cin >> a.first >> a.second;
}

template<typename T1>
istream &operator>>(istream &cin, vector<T1> &a)
{
    for (auto &x: a)
    {
        cin >> x;
    }
    return cin;
}

template<typename T1, typename T2>
ostream &operator<<(ostream &cout, const pair<T1, T2> &a)
{
    return cout << a.first << ' ' << a.second;
}

template<typename T1, typename T2>
ostream &operator<<(ostream &cout, const vector<pair<T1, T2>> &a)
{
    for (auto &x: a)
    {
        cout << x << endl;
    }
    return cout;
}

template<typename T1>
ostream &operator<<(ostream &cout, const vector<T1> &a)
{
    int n = a.size();
    if (!n)
    {
        return cout;
    }
    cout << a[0];
    for (int i = 1; i < n; i++)
    {
        cout << ' ' << a[i];
    }
    return cout;
}

template<typename... Args>
void see(Args &...args)
{
    ((cin >> args), ...);
}

template<typename... Args>
void out(Args... args)
{
    ((cout << args << " "), ...);
}

template<typename... Args>
void outl(Args... args)
{
    ((cout << args << endl), ...);
}

const int mod = 1e9 + 7;
#ifdef ONLINE_JUDGE
constexpr int N = 2e5 + 7;
#else
constexpr int N = 1e3 + 7;
#endif

using PII = pair<int, int>;
using i64 = long long;

const int inf = 1e9;

vector<PII> g[N]; //存圖
vector<int> dist(N, inf); //存距離 初始化為無窮 inf

void dijkstra(int s)
{
    priority_queue<PII, vector<PII>, greater<PII>> heap;
    heap.emplace(0, s);
    dist[s] = 0;// s 為起點

    while (heap.size())
    {
        auto [dis, u] = heap.top();
        heap.pop();
        if (dist[u] < dis)
        {
            continue;
        }
        for (auto &[v, w]: g[u])
        {
            if (dist[v] > dis + w)
            {
                dist[v] = dis + w;
                heap.emplace(dist[v], v);
            }
        }
    }
}

void solve()
{
    int n, m, s, a, b, c;
    see(n, m, s);
    rep (i, 0, m)
    {
        see(a, b, c);
        g[a].pb({b, c});
    }
    dijkstra(s);
    cout << dist[5] << endl; //查詢s-5的最短路
}

signed main()
{
    buff;
    int tt = 1;
    // cin >> tt;
    while (tt--)
    {
        solve();
    }
    return 0;
}

相關文章