Cod sursa(job #2950954)

Utilizator pifaDumitru Andrei Denis pifa Data 4 decembrie 2022 21:49:19
Problema Drumuri minime Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.62 kb
#include <fstream>
#include <vector>
#include <queue>
#include <cmath>

using namespace std;

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

const double eps = 1e-6;
const int maxN = 1505, inf = 0x3f3f3f3f, mod = 104659;
int n, m, ans[maxN];
double dist[maxN];
bool used[maxN];
struct heapNode {
    int nod;
    double cost;
    bool operator < (const heapNode &other) const
    {
        return cost > other.cost;
    }
};
vector <heapNode> G[maxN];
priority_queue <heapNode> heap;

double modul(double x)
{
    if(x < 0)
        x = -x;
    return x;
}

void dijkstra()
{
    for(int i = 1; i <= n; i++)
        dist[i] = inf;
    dist[1] = 0;
    ans[1] = 1;
    heap.push({1, 0});
    while(!heap.empty())
    {
        heapNode curr = heap.top();
        heap.pop();
        if(used[curr.nod])
            continue;
        used[curr.nod] = 1;
        for(heapNode nxt : G[curr.nod])
        {
            if(dist[curr.nod] + nxt.cost + eps < dist[nxt.nod])
            {
                dist[nxt.nod] = dist[curr.nod] + nxt.cost;
                ans[nxt.nod] = 0;
                heap.push({nxt.nod, dist[nxt.nod]});
            }
            if(modul(dist[curr.nod] + nxt.cost - dist[nxt.nod]) < eps)
                ans[nxt.nod] = (ans[nxt.nod] + ans[curr.nod]) % mod;
        }
    }
}

int main()
{
    fin >> n >> m;
    for(int i = 1; i <= m; i++)
    {
        int x, y, c;
        fin >> x >> y >> c;
        double dc = log2(c);
        G[x].push_back({y, dc});
    }
    dijkstra();
    for(int i = 2; i <= n; i++)
        fout << ans[i] << ' ';
    return 0;
}