Cod sursa(job #3238501)

Utilizator GabrielPopescu21Silitra Gabriel - Ilie GabrielPopescu21 Data 25 iulie 2024 21:17:18
Problema Drumuri minime Scor 5
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.63 kb
#include <bits/stdc++.h>
using namespace std;

const int MOD = 104659;
const int MAX = 1505;
vector<pair<int,double>> graph[MAX];
vector<double> dist;
vector<int> ans;
bitset<MAX> visited;

void dijkstra(int start)
{
    priority_queue<pair<double,int>, vector<pair<double,int>>, greater<pair<double,int>>> q;
    q.push({0.0, start});
    dist[start] = 0.0;
    ans[start] = 1;

    while (!q.empty())
    {
        pair<double,int> node = q.top();
        q.pop();
        if (!visited[node.second])
        {
            visited[node.second] = 1;
            for (pair<int,double> next : graph[node.second])
            {
                if (dist[next.first] > dist[node.second] + next.second)
                {
                    dist[next.first] = dist[node.second] + next.second;
                    ans[next.first] = ans[node.second];
                    q.push({dist[next.first], next.first});
                }
                else if (dist[next.first] == dist[node.second] + next.second)
                    ans[next.first] = (ans[next.first] + ans[node.second]) % MOD;
            }
        }
    }
}

int main()
{
    ifstream cin("dmin.in");
    ofstream cout("dmin.out");
    int n, m;
    cin >> n >> m;
    ans.assign(n+1, 0);
    dist.assign(n+1, DBL_MAX);

    for (int i = 1; i <= m; ++i)
    {
        int x, y, cost;
        cin >> x >> y >> cost;
        double cost2 = log((double)cost);
        graph[x].push_back({y, cost2});
        graph[y].push_back({x, cost2});
    }

    dijkstra(1);

    for (int i = 2; i <= n; ++i)
        cout << (ans[i] % MOD) << " ";

    return 0;
}