Cod sursa(job #3380121)

Utilizator ryuuAdrian Rusu ryuu Data 1 decembrie 2026 20:35:12
Problema Drumuri minime Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.8 kb
#include <fstream>
#include <vector>
#include <queue>
#include <cmath>
using namespace std;

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

const int NMAX = 1505;
const int MOD = 104659;
const double EPS = 1e-9;
const double INF = 1e18;

vector<pair<int, double>> graph[NMAX]; // (vecin, log(cost))
double dist[NMAX];                     // distanta (in logaritmi)
int cnt[NMAX];                         // numarul de drumuri minime
bool done[NMAX];                       // nodul a fost "finalizat" in Dijkstra

int main() {
    int n, m;
    fin >> n >> m;

    for (int i = 0; i < m; i++) {
        int x, y, c;
        fin >> x >> y >> c;
        double w = log((double)c);
        graph[x].push_back({y, w});
        graph[y].push_back({x, w});
    }

    for (int i = 1; i <= n; i++)
        dist[i] = INF;

    priority_queue<pair<double, int>, vector<pair<double, int>>,
                   greater<pair<double, int>>> pq;

    dist[1] = 0;
    cnt[1] = 1; // exista un singur drum de la 1 la 1: cel vid
    pq.push({0, 1});

    while (!pq.empty()) {
        int node = pq.top().second;
        pq.pop();

        if (done[node])
            continue;
        done[node] = true;

        for (auto edge : graph[node]) {
            int next = edge.first;
            double newDist = dist[node] + edge.second;

            if (newDist < dist[next] - EPS) {
                // drum strict mai bun
                dist[next] = newDist;
                cnt[next] = cnt[node];
                pq.push({newDist, next});
            } else if (fabs(newDist - dist[next]) <= EPS) {
                // drum la fel de bun: adunam numarul de drumuri
                cnt[next] = (cnt[next] + cnt[node]) % MOD;
            }
        }
    }

    for (int i = 2; i <= n; i++)
        fout << cnt[i] << " ";
    fout << "\n";

    return 0;
}