Cod sursa(job #2978639)

Utilizator CalinHanguCalinHangu CalinHangu Data 13 februarie 2023 22:58:22
Problema Drumuri minime Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.74 kb
#include <fstream>
#include <vector>
#include <queue>
#include <cmath>
#include <algorithm>

#define ll long long
#define pb push_back
#define bg begin
#define en end
#define x first
#define y second

#define int ll

using namespace std;

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

const int NMAX = 1505;
const int MOD = 104659;
const double INF = 1e9 + 5;
const double err = 1e-6;
const char nl = '\n';

int n, m, ans[NMAX], inq[NMAX];

double dist[NMAX];

struct pii{
    int x;
    double y;
    bool operator < (const pii &aux) const
    {
        return y > aux.y;
    }
};

vector<pii> v[NMAX];

priority_queue<pii> pq;

void solve(){
    for(int i = 1; i <= n; ++i)
        dist[i] = INF;
    dist[1] = 0, inq[1] = 1, ans[1] = 1;
    pq.push({1, 0});
    while(!pq.empty()){
        int cur_node = pq.top().first;
        pq.pop();
        inq[cur_node] = 0;
        for(auto neigh: v[cur_node]){
            if(dist[neigh.x] - dist[cur_node] - neigh.y > err){
                dist[neigh.x] = dist[cur_node] + neigh.y;
                ans[neigh.x] = 0;
                if(!inq[neigh.x]){
                    inq[neigh.x] = 1;
                    pq.push({neigh.x, dist[neigh.x]});
                }
            }
            if(abs(dist[neigh.x] - dist[cur_node] - neigh.y) < err)
                ans[neigh.x] = (ans[neigh.x] + ans[cur_node]) % MOD;
        }
    }
    for(int i = 2; i <= n; ++i)
        out << ans[i] << ' ';
    out << nl;
}

signed main()
{
    in >> n >> m;
    for(int i = 2; i <= m; ++i){
        int a, b;
        double c;
        in >> a >> b >> c;
        c = log(c);
        v[a].pb({b, c});
        v[b].pb({a, c});
    }
    solve();
    return 0;
}