Cod sursa(job #3192716)

Utilizator matei__bBenchea Matei matei__b Data 13 ianuarie 2024 10:46:37
Problema Algoritmul lui Dijkstra Scor 90
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.75 kb
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define ld long double
#define chad char
#define mod 1'000'000'007
#define dim 100005
#define lim 1000000
#define mdim 20'005
#define mult 2e9
#define maxx 200002
#define INF 1'000'000'000
#define simaimult 1e17
#define FOR(i,a,b) for(int i=(a); i<=(b); i++)
#define pli pair<ll,int>
#define pil pair<int,ll>
#define piii pair<int,pair<int,int> > 
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pb push_back
#define mp make_pair
#define nr_biti __builtin_popcount
using namespace std;
 
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");

int n,nod_start=1,m;
int pre[dim],cost[dim];
bool viz[dim]; 
vector<pii> g[dim];

void init()
{
    for(int i=1; i<=n; i++)
        cost[i]=INF;
}

void dijk() 
{
    priority_queue<pii,vector<pii>,greater<pii> > q;

    q.push(mp(0,1));
    cost[1]=0;
    
    while(q.size())
    {
        int nod=q.top().second;
        q.pop();

        if(cost[nod]==INF)
            break;

        for(auto it:g[nod])
        {
            if(cost[it.first]>cost[nod]+it.second)
            {
                cost[it.first]=cost[nod]+it.second;
                q.push(mp(cost[it.first],it.first));
            }
        }
    }
}

void afis()
{
    for(int i=2; i<=n; i++)
        if(cost[i]==INF)
            fout << 0 << " ";
        else    
            fout << cost[i] << " ";
}

int main()
{
    ios_base::sync_with_stdio(false);
    fin.tie(nullptr);
    fout.tie(nullptr); 

    fin >> n >> m;

    int x,y,c;

    for(int i=1; i<=m; i++)
    {
        fin >> x >> y >> c;

        g[x].pb(mp(y,c));
    }

    init();
    dijk();
    afis();

    return 0;
}