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. 8 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. 8 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. 8 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. 7 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. 6 months 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. 5 months 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. 5 months 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. 5 months 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. 5 months 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. 5 months 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. 5 months 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. 5 months 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. 5 months 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. 5 months 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.

  15. 4 months ago
    2026-4-13 6:23:12

    Alright, alright, pt888vip, I see you. Took a spin, and not gonna lie, had some fun. Smooth sailing for a bit, ya know? Check it out for yourself: pt888vip

  16. 4 months ago
    2026-4-13 6:23:27

    Just a heads up, pgbetlogin is a thing. I’ve checked it out, and it’s pretty standard. If you’re looking for something new, maybe give it a look. Here’s the link: pgbetlogin

  17. 4 months ago
    2026-4-13 6:23:43

    Alright, I’ve seen some buzz around funbetquees. Anyone had any luck there? Thinking of checking it out. What do you think? Click here now: funbetquees

  18. 3 months ago
    2026-4-27 1:34:14

    Curious about starting out? The platform sounds user-friendly! For beginners, maybe checking out the okplaygame app download guide first helps. It’s like learning a new subject-step-by-step!

  19. 2 months ago
    2026-6-08 22:39:06

    OMG, this whole setup is wild! ✨ The betting options sound legit, but check out sporting bet download apk for more details. Seriously good stuff! 🤩

  20. 2 months ago
    2026-6-13 11:01:10

    That’s a great point about accessibility in gaming! Platforms like jlph login download are making it easier for Filipino players to enjoy diverse options – from slots to live casinos – right on their phones. Quick deposits are a huge plus too!

  21. 1 month ago
    2026-6-27 13:47:31

    Analyzing these shortest path algorithms highlights how optimization is universal. Whether solving graph theory problems or optimizing user experience, the goal is finding the most efficient route. This principle of optimized interaction is crucial, whether in complex code or enjoying a seamless digital platform like the GTaya app casino. Excellent deep dive!

  22. 3 weeks ago
    2026-7-15 10:58:54

    Solid analysis of bankroll management – crucial for any serious player! Seeing platforms like bee ph casino com cater to local PH players with easy registration is a smart move for accessibility & growth. Good stuff!

  23. 1 week ago
    2026-7-24 23:00:41

    The UX here feels very structured; they nail the logic-driven flow mentioned in the product details. For optimal gameplay insight, check out casinoplus com ph games. Keeps things clean!

  24. 1 week ago
    2026-7-26 23:07:08

    Seeing the focus on instant response time is key. For a smooth flow, check out x88 pro game; it feels genuinely modern. Great read!

Send Comment Edit Comment


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