Cod sursa(job #2543310)

Utilizator trifangrobertRobert Trifan trifangrobert Data 11 februarie 2020 01:15:41
Problema Drumuri minime Scor 95
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.88 kb
#include <bits/stdc++.h>

using namespace std;

const int NMAX = 1505;
const int MOD = 104659;
const long double EPS = 1e-9L;
const int INF = 2000000000;
int N, M;
vector < pair <int, int> > graph[NMAX];
long double dist[NMAX];
int cnt[NMAX];
bitset <NMAX> seen;

struct Heap
{
    int node;
    long double cost;
    Heap()
    {
        this->node = this->cost = 0;
    }
    Heap(int node, int cost)
    {
        this->node = node;
        this->cost = cost;
    }
    inline bool operator<(const Heap& other) const
    {
        return this->cost > other.cost;
    }
};

bool eps(long double x)
{
    return (x < -EPS || EPS < x);
}

priority_queue <Heap> pq;

int main()
{
    ifstream fin("dmin.in");
    ofstream fout("dmin.out");
    fin >> N >> M;
    for (int i = 1;i <= M;++i)
    {
        int u, v, c;
        fin >> u >> v >> c;
        graph[u].push_back(make_pair(v, c));
        graph[v].push_back(make_pair(u, c));
    }
    for (int i = 2;i <= N;++i)
        dist[i] = INF;
    cnt[1] = 1;
    pq.push(Heap(1, 0));
    while (!pq.empty())
    {
        int node = pq.top().node;
        long double cost = pq.top().cost;
        pq.pop();
        if (seen[node] == 0)
        {
            seen[node] = 1;
            for (auto& i : graph[node])
            {
                if (!eps(dist[i.first] - (dist[node] + log(i.second))))
                    cnt[i.first] = (cnt[i.first] + cnt[node]) % MOD;
                else if (dist[i.first] > dist[node] + log(i.second))
                {
                    cnt[i.first] = cnt[node];
                    dist[i.first] = dist[node] + log(i.second);
                    pq.push(Heap(i.first, dist[i.first]));
                }
            }
        }
    }
    for (int i = 2;i <= N;++i)
        fout << cnt[i] << " ";
    fin.close();
    fout.close();
    return 0;
}