Cod sursa(job #3380704)

Utilizator AlexPopescuPopescu Alexandru AlexPopescu Data 2 decembrie 2026 09:27:18
Problema Drumuri minime Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.66 kb
#include <bits/stdc++.h>
using namespace std;


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

const int MOD=104659;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int N,M;
    fin>>N>>M;
    vector<vector<pair<int,int>>>graph(N+1);
    for(int i=1;i<=M;i++)
        { int x,y,cost;
          fin>>x>>y>>cost;
          graph[x].push_back({y,cost});
          graph[y].push_back({x,cost});
        }
    vector<long double>distances(N+1,1e100L);
    vector<long long>ways(N+1,0);
    priority_queue<pair<long double,int>,vector<pair<long double,int>>,greater<pair<long double,int>>>node_list;
    distances[1]=0;
    ways[1]=1;
    node_list.push({0,1});
    while(!node_list.empty())
         { long double current_distance=node_list.top().first;
           int current_node=node_list.top().second;
           node_list.pop();
           if(current_distance>distances[current_node]+1e-12L)
               continue;
           for(auto &neighbour:graph[current_node])
               { long double next_distance=current_distance+logl((long double)neighbour.second);
                 if(next_distance<distances[neighbour.first]-1e-12L)
                    { distances[neighbour.first]=next_distance;
                      ways[neighbour.first]=ways[current_node];
                      node_list.push({next_distance,neighbour.first});
                    }
                 else if(fabsl(next_distance-distances[neighbour.first])<=1e-12L)
                    { ways[neighbour.first]=(ways[neighbour.first]+ways[current_node])%MOD;
                    }
               }
         }
    for(int i=2;i<=N;i++)
        fout<<ways[i]%MOD<<' ';
    fout<<'\n';
    return 0;
}