Cod sursa(job #3380088)

Utilizator laur7766laur m laur7766 Data 1 decembrie 2026 20:08:37
Problema Drumuri minime Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.84 kb
#include <bits/stdc++.h>

using namespace std;

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

struct cmp {
    bool operator() (const pair<int, double>& a, const pair<int, double>& b) {
        return a.second > b.second;
    }
};

int N, M;
double distanta[1505];
int nr_drumuri[1505];
const double INF = 1e16;
const int MOD = 104659;
vector<pair<int, double>> G[1505];
priority_queue<pair<int, double>, vector<pair<int, double>>, cmp> pq;

void Dijkstra(int NodStart) {
    for (int i = 1; i <= N; ++i) {
        distanta[i] = INF;
    }
    
    distanta[NodStart] = 0.0;
    nr_drumuri[NodStart] = 1;
    pq.push({NodStart, 0.0});
    
    while (!pq.empty()) {
        int Nod = pq.top().first;
        double Cost = pq.top().second;
        pq.pop();
        
        if (distanta[Nod] + 1e-9 < Cost) {
            continue;
        }
        
        for (const auto& adiac : G[Nod]) {
            int vecin = adiac.first;
            double cost_vecin = adiac.second;
            
            if (distanta[Nod] + cost_vecin + 1e-9 < distanta[vecin]) {
                distanta[vecin] = distanta[Nod] + cost_vecin;
                nr_drumuri[vecin] = nr_drumuri[Nod];
                pq.push({vecin, distanta[vecin]});
            }
            else if (abs(distanta[Nod] + cost_vecin - distanta[vecin]) <= 1e-9) {
                nr_drumuri[vecin] = (nr_drumuri[vecin] + nr_drumuri[Nod]) % MOD;
            }
        }
    }
}

int main() {
    int x, y;
    double cost;
    fin >> N >> M;
    for (int i = 0; i < M; ++i) {
        fin >> x >> y >> cost;
        double c_log = log(cost);
        G[x].push_back({y, c_log});
        G[y].push_back({x, c_log});
    }
    
    Dijkstra(1);
    
    for (int i = 2; i <= N; ++i) {
        fout << nr_drumuri[i] << ' ';
    }
    
    return 0;
}