Cod sursa(job #2842055)

Utilizator Y.MalmsteenB.P.M. Y.Malmsteen Data 30 ianuarie 2022 23:19:14
Problema Drumuri minime Scor 20
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.66 kb
#include <fstream>
#include <vector>
#include <queue>
#include <cmath>

using namespace std;
const int NMAX = 1501, //MMAX = 2501,
          MOD = 104659;
const double EPS = 1e-9,
             INF = 1e+9;

struct muchie
{
    int y;
    double w;

    bool operator<(const muchie &m) const
    {
        return w > m.w;
    }
};

int N, M;
double D[NMAX]; ///  D[i] = distanta minima de la nodul src la i
int nrD[NMAX];  ///nrD[i] = numarul de drumuri de lungime D[i]
vector<muchie> G[NMAX];
priority_queue<muchie> Q;

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

void citire()
{
    int x, y, c;
    double w;
    f >> N >> M;
    while(M--)//for(int i = 1; i <= M; ++i)
    {
        f >> x >> y >> c;
        w = log(c);
        G[x].push_back({y, w});
        G[y].push_back({x, w});
    }
}

void init(int src)
{
    for(int i = 1; i <= N; i++)
        D[i] = INF;
    D[src] = 0;
}

void Dijkstra(int src)
{
    init(src);
    Q.push({src, 0});
    nrD[src] = 1;
    while(!Q.empty())
    {
        muchie crt = Q.top();
        Q.pop();
        for(muchie& m : G[crt.y])
        {
            double cost = D[crt.y] + m.w;
            if(fabs(cost - D[m.y]) <= EPS)
                nrD[m.y] = (nrD[m.y] + nrD[crt.y]) % MOD;
            else
                if(D[m.y] - cost > EPS)
                {
                    D[m.y] = cost;
                    nrD[m.y] = nrD[crt.y];
                    Q.push({m.y, D[m.y]});
                }
        }
    }
}

int main()
{
    citire();
    Dijkstra(1);
    for(int i = 2; i <= N; i++)
        g << nrD[i] << ' ';
    f.close();
    g.close();
    return 0;
}