Cod sursa(job #3379320)

Utilizator alessandraspioanaSpataroaia Alessandra-Ioana alessandraspioana Data 30 noiembrie 2026 08:49:02
Problema Drumuri minime Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.32 kb
#include <fstream>
#include <cmath>
#include <vector>
#include <set>
using namespace std;

ifstream fin("dmin.in");
ofstream fout("dmin.out");

int main() {
    int n, m;
    fin >> n >> m;
    vector<vector<pair<int, long double>>> g(n + 1);
    for (int i = 0; i < m; i++) {
        int x, y, c;
        fin >> x >> y >> c;
        long double l = log((long double)c);
        g[x].push_back({y, l});
        g[y].push_back({x, l});
    }
    const long double EPS = 1e-12L;
    vector<long double> d(n + 1, 1e18L);
    vector<int> ways(n + 1, 0);
    vector<bool> done(n + 1, false);
    set<pair<long double, int>> q;
    d[1] = 0;
    ways[1] = 1;
    q.insert({0, 1});
    while (!q.empty()) {
        int u = q.begin()->second;
        q.erase(q.begin());
        done[u] = true;
        for (auto &e : g[u]) {
            int v = e.first;
            if (done[v]) continue;
            long double nd = d[u] + e.second;
            if (nd < d[v] - EPS) {
                q.erase({d[v], v});
                d[v] = nd;
                ways[v] = ways[u];
                q.insert({nd, v});
            } else if (abs(nd - d[v]) <= EPS) {
                ways[v] = (ways[v] + ways[u]) % 104659;
            }
        }
    }
    for (int i = 2; i <= n; i++) fout << ways[i] << ' ';
    fout << '\n';
}