Cod sursa(job #3378960)

Utilizator AndreiO86902462Andrei O AndreiO86902462 Data 28 noiembrie 2026 21:57:00
Problema Drumuri minime Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.42 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <cmath>
using namespace std;

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

const unsigned long long mod = 104659;
const long double epsilon = 1e-9L;
const long double infinity = 1e18L;

int main()
{
    int n, m; fin >> n >> m;
    cout << "Read n: " << n << ", m: " << m << "\n";
    
    vector<vector<pair<int, int>>> adj(n);
    for(int i = 0; i < m; ++i) {
        int u, v, w; fin >> u >> v >> w;
        --u; --v;
        adj[u].push_back({w,v});
        adj[v].push_back({w,u});
        cout << "\tRead u: " << u << ", v: " << v << ", w: " << w << "\n";
    }
    
    vector<long double> dist(n, infinity);
    dist[0] = 0.0;
    
    priority_queue<pair<long double,int>, vector<pair<long double,int>>, greater<pair<long double,int>>> pq = {}; //weight, node
    pq.push({dist[0], 0.0});
 
    while (!pq.empty()) {
        pair<long double,int> p1 = pq.top(); pq.pop();
        int u = p1.second;
        
        cout << "Popped node u: " << u << endl;
    
        for(pair<long double,int> p2 : adj[u]) {
            int v = p2.second;
            long double weight = dist[u] + log(p2.first);
            //log(a*b) == log a + log b
            
            cout << "\tAdjacent node v: " << v << " of u, dist[v] " << dist[v] << " > dist[u] " << dist[u] << " + weight " << weight << endl;
            
            if(weight < dist[v] - epsilon) {
                dist[v] = weight;
                pq.push({weight, v});
                cout << "\tInserted {dist[v] " << weight << ", v " << v << " }" << endl;
            }
        }
    }
    
    for(int i = 0; i < n; ++i)
        pq.push({dist[i], i});
    
    vector<unsigned long long> nr_drumuri(n,0);
    nr_drumuri[0] = 1;
    
    while (!pq.empty()) {
        pair<long double,int> p1 = pq.top(); pq.pop();
        int node = p1.second;

        for(pair<long double,int> p2 : adj[node]) {
            int neighbor_node = p2.second;
            long double neighbor_cost = p2.first;

            if(fabsl(dist[neighbor_node] + log(neighbor_cost) - dist[node]) < epsilon) {
                nr_drumuri[node] += nr_drumuri[neighbor_node] % mod;
                nr_drumuri[node] %= mod;
            }
        }
    }
    
    cout << "nr_drumuri: ";
    for(int i = 1; i < n; ++i)
        fout << nr_drumuri[i] % mod << ' ';
    return 0;
}