Cod sursa(job #3341027)

Utilizator mariusharabariMarius Harabari mariusharabari Data 17 februarie 2026 17:32:42
Problema Drumuri minime Scor 15
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.49 kb
#include <bits/stdc++.h>
using namespace std;

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

const int NMAX=1501;
const double epsilon=1e-6;
int n, m;
int nr[NMAX];
double d[NMAX];
vector <pair <int, double>> g[NMAX];
priority_queue <pair <double, int>, vector <pair <double, int>>, greater <pair <double, int>>> pq;
bitset <NMAX> viz;

bool egal(double a, double b){
    return fabs(a-b)<epsilon;
}

int main(){
    fin>>n>>m;
    int x, y;
    double w;
    for(int i=1;i<=m;i++){
        fin>>x>>y>>w;
        w=log2(w);
        //cout<<w<<' ';
        g[x].push_back({y, w});
        g[y].push_back({x, w});
    }
    //cout<<endl;

    d[1]=0;
    nr[1]=1;
    pq.push({0, 1});
    for(int i=2;i<=n;i++)
        d[i]=1e9;

    while(!pq.empty()){
        int nod=pq.top().second;
        double dist=pq.top().first;
        pq.pop();
        //cout<<nod<<' '<<dist<<endl;

        if(viz[nod])
            continue;

        viz[nod]=1;

        for(auto vec : g[nod]){
            //cout<<vec.first<<' '<<d[nod]+vec.second<<' '<<d[vec.first]<<endl;
            if(egal(d[vec.first],d[nod]+vec.second)){
                nr[vec.first]+=nr[nod];
            }
            if(d[vec.first]>d[nod]+vec.second){
                d[vec.first]=d[nod]+vec.second;
                nr[vec.first]=nr[nod];
                pq.push({d[vec.first], vec.first});
            }
        }
    }
    for(int i=2;i<=n;i++){
        fout<<nr[i]<<' ';
    }

    return 0;
}