Cod sursa(job #3380085)

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

using namespace std;

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

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

int N, M;
long double distanta[1505];
int nr_drumuri[1505];
const long double INF = 1e18;
const int MOD = 104659;
vector<pair<int, long double>> G[1505];
priority_queue<pair<int, long double>, vector<pair<int, long 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;
        long double Cost = pq.top().second;
        pq.pop();
        
        if (distanta[Nod] < Cost) {
            continue;
        }
        
        for (const auto& adiac : G[Nod]) {
            int vecin = adiac.first;
            long double cost_vecin = adiac.second;
            
            if (distanta[Nod] + cost_vecin < distanta[vecin]) {
                distanta[vecin] = distanta[Nod] + cost_vecin;
                nr_drumuri[vecin] = nr_drumuri[Nod];
                pq.push({vecin, distanta[vecin]});
            }
            else if (distanta[Nod] + cost_vecin == distanta[vecin]) {
                nr_drumuri[vecin] = (nr_drumuri[vecin] + nr_drumuri[Nod]) % MOD;
            }
        }
    }
}

int main() {
    int x, y;
    long double cost;
    fin >> N >> M;
    for (int i = 0; i < M; ++i) {
        fin >> x >> y >> cost;
        long double c_log = logl(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;
}