CSES Shortest Routes II
// compile: make data
// run: ./data < data.in
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#ifdef LOCAL
#include <debug/codeforces.h>
#define debug(x...) _debug_print(#x, x);
#else
#define debug(x...) {};
#endif
template<typename...Args> void print_(Args...args){((cout<<args<<" "),...)<<endl;}
#define rep(i,a,b) for(int i=(a);i<(int)(b);++i)
#define sz(v) ((int)(v).size())
#define print(...) print_(__VA_ARGS__);
#define INTMAX (int)(9223372036854775807)
#define INF (int)(1152921504606846976)
#define double long double
#define int long long
#define MAXN 200010

struct graph {
    struct node {
        int u, v, w;
    };
    vector<vector<node>> e;
    int n, m;
    bool directed;
    bool weighted = 0;
    graph(int V, bool D = 0) {
        n = V;
        e.resize(n);
        directed = D;
    }
    void add_edge(int u, int v, int w = 1) {
        e[u].push_back(node{u, v, w});
        ++m;
    }
    bool spfa(int s, vector<int> &dis) {
        dis.resize(n, INF);
        vector<bool> inq(n, 0);
        vector<int> cnt(n, 0);
        queue<int> q;
        dis[s] = 0, q.push(s), inq[s] = 1;
        while (!q.empty()) {
            int u = q.front(); q.pop();
            inq[u] = 0;
            for (auto edge: e[u]) {
                int v = edge.v, w = edge.w;
                if (dis[v] > dis[u] + w) {
                    dis[v] = dis[u] + w;
                    cnt[v] = cnt[u] + 1;
                    if (cnt[v] > n) return 1;
                    if (!inq[v]) q.push(v), inq[v] = 1;
                }
            }
        }
    }
    void dijkstra(int s, vector<int> &dis) {
        dis.resize(n, INF);
        vector<bool> vis(n, 0);
        priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
        dis[s] = 0, pq.push({0, s});
        while (!pq.empty()) {
            int u = pq.top().second; pq.pop();
            if (vis[u]) continue;
            vis[u] = 1;
            for (auto edge: e[u]) {
                int v = edge.v, w = edge.w;
                if (dis[v] > dis[u] + w) {
                    dis[v] = dis[u] + w;
                    pq.push({dis[v], v});
                }
            }
        }
    }
    void floyd(vector<vector<int>> &dis) {
        dis.resize(n, vector<int>(n, INF));
        rep(i, 0, n) dis[i][i] = 0;
        rep(u, 0, n) for (auto edge: e[u]) {
            int v = edge.v, w = edge.w;
            dis[u][v] = min(dis[u][v], w);
        }
        rep(k, 0, n) rep(i, 0, n) rep(j, 0, n) {
            if (dis[i][k] == INF || dis[k][j] == INF) continue;
            dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);
        }
    }
    void graphviz_dump(string filename = "graph.dot") {
        ofstream gf;
        gf.open(filename);
        gf << (directed ? "digraph" : "graph") << " {\n";
        gf << "    "; rep(i, 0, n) gf << i << " ;"[i==n-1]; gf << endl;
        string notation = directed ? " -> " : " -- ";
        for (auto es: e) for (auto edge: es) if (edge.w != 1) weighted = 1;
        for (auto es: e) {
            for (auto edge: es) {
                if (!directed && edge.u > edge.v) continue;
                gf << "    " << edge.u << notation << edge.v << (weighted ? " ;\n" : ";\n");
            }
        }
        gf << "}\n";
    }
};

int32_t main() {
    ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);

    int n, m, _; cin >> n >> m >> _;
    graph g(n);
    rep(i, 0, m) {
        int u, v, w; cin >> u >> v >> w;
        g.add_edge(u-1, v-1, w);
        g.add_edge(v-1, u-1, w);
    }
    vector<vector<int>> dis;
    g.floyd(dis);
    while (_--) {
        int u, v; cin >> u >> v;
        cout << (dis[u-1][v-1]==INF ? -1 : dis[u-1][v-1]) << endl;
    }

    return 0;
}

Comments

  1. 4 months ago
    2025-12-06 2:24:13

    Just discovered gk999 and I’m kinda digging it. They’ve got a nice variety of options and it feels pretty legit. I’d recommend checking it out if you’re curious! Give gk999 a shot!

  2. 3 months ago
    2025-12-09 10:00:06

    Interesting take on responsible gaming! It’s cool to see platforms like windream ph com focusing on localized payment options like GCash – makes things so much easier for PH players. Definitely a step up!

  3. 3 months ago
    2025-12-19 13:39:10

    Trying the fruit party demo on fruitparty.net is a smart idea. I was hooked instantly after playing it. You gotta test drive the demo first, trust me! See for yourself right here fruit party demo

  4. 3 months ago
    2025-12-29 6:07:19

    Gave vn888com a spin. The registration process was a bit clunky, but once I got in, the site was alright. Their bonuses were a little confusing at first, make sure to read the terms and conditions. Decent enough selection of games, though. Give them a look, but read the fine print: vn888com.

  5. 1 month ago
    2026-2-10 15:42:22

    This implementation elegantly demonstrates graph algorithms fundamentals. The SPFA approach shows creative optimization over Bellman-Ford, though Dijkstra’s method remains preferable for non-negative weights. Such algorithmic thinking mirrors the precision required in og777 app casino platform architecture. Excellent educational resource!

  6. 4 weeks ago
    2026-2-19 20:35:42

    Heard of dua66? It’s… alright. A bit of fun, a bit of a punt. Could be worse, could be better. See for yourself here: dua66

  7. 4 weeks ago
    2026-2-19 20:35:58

    Fancy a spin? BBJLslots might be your cup of tea. It’s got slots, alright. See if you can hit the jackpot! Find it here: bbjlslots

  8. 4 weeks ago
    2026-2-19 20:36:13

    Right, so phwin88login. Worth a punt if you’re looking for something new. Give it a try and tell me what you think over here: phwin88login

  9. 3 weeks ago
    2026-3-03 21:41:15

    Jilipark22, huh? Heard some whispers about this one. Gave it a go and honestly, it’s pretty smooth. Withdrawals were surprisingly quick. Definitely gonna put some more time in here. Give them a try at jilipark22

  10. 3 weeks ago
    2026-3-03 21:41:31

    Khelostarcasino… sounds fancy, right? Turns out, it’s got the goods to back it up. Good selection, responsive support. Had a blast! See for yourself at khelostarcasino

  11. 3 weeks ago
    2026-3-03 21:41:46

    Betgorila! I like the name. Jumped in not expecting much, but was pleasantly surprised. Easy to navigate, good odds in some spots, and cashed out without a hitch. I’m in! More details on betgorila

  12. 3 days ago
    2026-3-18 11:26:46

    Trying to log in to 80jili? Easy peasy! Just head over to 80jili com login and you’ll be winning in no time! Fingers crossed for big payout!

  13. 3 days ago
    2026-3-18 11:27:02

    Xsmb 68, are there some hints and predictions here? Lets check the numbers and cross ourfingers that it’s a winner! Start winning here: xsmb 68

  14. 3 days ago
    2026-3-18 11:27:17

    Looking for the c9taya apk? Downloaded it, installed it, and I’m currently winning. So, yeah, I’d recommend grabbing it if you’re on Android. Find it right here c9taya apk.

Send Comment Edit Comment


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
Previous
Next