CSES Investigation
// 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);
#define Debug(x...) _debug_print_format(#x, x);
#else
#define debug(x...)
#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 FIND(a, x) ((find(a.begin(),a.end(),(x))!=a.end())?1:0)
#define cmin(x,...) x=min({(x), __VA_ARGS__})
#define cmax(x,...) x=max({(x), __VA_ARGS__})
#define INTMAX (int)(9223372036854775807)
#define INF (int)(1152921504606846976)
#define double long double
#define int long long
#define MAXN 200010
#define P 1000000007

struct graph {
    struct node {
        int v, w;
        bool operator<(const node &other) const {
            return w < other.w;
        }
        bool operator==(const node &other) const {
            return v == other.v && w == other.w;
        }
    };
    vector<vector<node>> e;
    int n;
    bool directed;
    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{v, w});
    }
    void dijkstra(int s, vector<int> &dis, vector<int> &nmin, vector<int> &nmax, vector<int> &nsum) {
        dis.resize(n, INF); nmin.resize(n, INF); nmax.resize(n, -INF); nsum.resize(n, 0);
        vector<bool> vis(n, 0);
        struct temp {
            int d, u, num;
            bool operator<(const temp &other) const {
                return d < other.d;
            }
            bool operator>(const temp &other) const {
                return d > other.d;
            }
        };
        priority_queue<temp, vector<temp>, greater<temp>> pq;
        dis[s] = 0, nsum[s] = 1, nmin[s] = nmax[s] = 0, pq.push({0, s, 0});
        while (!pq.empty()) {
            auto [d, u, num] = pq.top(); pq.pop();
            if (vis[u]) continue;
            vis[u] = 1;
            for (auto [v, w]: e[u]) {
                if (dis[v] > dis[u] + w) {
                    dis[v] = dis[u] + w;
                    nmin[v] = nmin[u] + 1;
                    nmax[v] = nmax[u] + 1;
                    nsum[v] = nsum[u];
                    pq.push({dis[v], v, num + 1});
                }
                else if (dis[v] == dis[u] + w) {
                    cmin(nmin[v], nmin[u] + 1);
                    cmax(nmax[v], nmax[u] + 1);
                    nsum[v] = (nsum[v] + nsum[u]) % P;
                    pq.push({dis[v], v, num + 1});
                }
            }
        }
    }
    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 ? " -> " : " -- ";
        bool weighted = 0;
        for (auto es: e) for (auto edge: es) if (edge.w != 1) weighted = 1;
        rep(u, 0, n) {
            for (auto [v, w]: e[u]) {
                if (!directed && u > v) continue;
                gf << "    " << u << notation << 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, 1);
    rep(i, 0, m) {
        int u, v, w; cin >> u >> v >> w;
        g.add_edge(u-1, v-1, w);
    }
    g.graphviz_dump();
    vector<int> dis, nmin, nmax, nsum;
    g.dijkstra(0, dis, nmin, nmax, nsum);
    cout << dis[n-1] << " " << nsum[n-1] << " " << nmin[n-1] << " " << nmax[n-1] << endl;

    return 0;
}
No Comments

Send Comment Edit Comment


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