Cod sursa(job #2975704)

Utilizator and_Turcu Andrei and_ Data 7 februarie 2023 09:45:29
Problema Drumuri minime Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.38 kb
#include <bits/stdc++.h>
#define N 1507
#define MOD 104659
#define int long long
using namespace std;

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

int n,m;
vector< pair<int,int> > g[N];
bitset<N> viz;
vector<int> dist(N,-1);


void Citire()
{
    fin >> n >> m;
    for(int i=1;i<=m;i++)
    {
        int x,y,c;
        fin >> x >> y >> c;
        g[x].push_back({c,y});
        g[y].push_back({c,x});
    }
    fin.close();
}

void Rezolvare()
{
    priority_queue<pair<int,int>> pq;
    pq.push({0,1});
    dist[1]=1;
    while( !pq.empty() )
    {
        int cur=pq.top().second;
        int val=-pq.top().first;
        pq.pop();
        if( !viz[cur] )
        {
            for( auto w:g[cur] )
            {
                int nod=w.second;
                int cost=w.first;
cout << val << " ";
                if( dist[nod]==-1 )
                {
                    dist[nod]=cost;
                    pq.push({-cost,nod});
                    continue;
                }
                if( 1LL*val*cost<1LL*dist[nod] )
                {
                    dist[nod]=1LL*val*cost%MOD;
                    pq.push({-dist[nod],nod});
                }
            }
        }
        viz[cur]=1;
    }
    for(int i=2;i<=n;i++)fout << dist[i] << " ";
}


int32_t main()
{
    Citire();
    Rezolvare();
    return 0;
}