Cod sursa(job #3360142)

Utilizator Andrei_GAndreiG Andrei_G Data 9 iulie 2026 12:57:07
Problema Drumuri minime Scor 25
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.99 kb
#include <fstream>
#pragma GCC optimize("O3,unroll-loops")
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <climits>
#include <iomanip>
#include <numeric>
#include <bitset>
#include <string>
#include <vector>
#include <cmath>
#include <queue>
#include <deque>
#include <stack>
#include <list>
#include <map>
#include <set>
#define int long long
//#define int short
using namespace std;

ifstream cin("dmin.in");
ofstream cout("dmin.out");

const int nmax = 1500;
const int inf = 2e9;
const int mod = 104659;
const double eps = 1e-9;

int n, m, cnt[nmax + 5];
double dis[nmax + 5];
vector<pair<int, double>> adj[nmax + 5];

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

void dijkstra(int stnode){
    priority_queue<pair<int, double>, vector<pair<int, double>>, cmp> pq;
    dis[stnode] = 0;
    cnt[stnode] = 1;
    pq.push({stnode, 0.0});
    while (!pq.empty()){
        int curnode = pq.top().first;
        double curweight = pq.top().second;
        pq.pop();
        if (curweight - dis[curnode] > eps){
            continue;
        }
        for (auto& [node, weight] : adj[curnode]){
            double newweight = weight + curweight;
            if (dis[node] > newweight){
                dis[node] = newweight;
                cnt[node] = cnt[curnode];
                pq.push({node, newweight});
            }
            else if (abs(newweight - dis[node]) <= eps){
                cnt[node] += cnt[curnode];
                cnt[node] %= mod;
            }
        }
    }
}

signed main(){
    cin>>n>>m;
    while (m--){
        int u, v;
        double w;
        cin>>u>>v>>w;

        w = log2(w);
        adj[u].push_back({v, w});
        adj[v].push_back({u, w});
    }
    for (int i = 1; i <= n; i++){
        dis[i] = inf;
    }
    dijkstra(1);
    for (int i = 2; i <= n; i++){
        cout<<cnt[i]<<" ";
    }
}