Cod sursa(job #2272485)

Utilizator TherevengerkingSurani Adrian Therevengerking Data 30 octombrie 2018 10:47:50
Problema Algoritmul lui Dijkstra Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.16 kb
#include <bits/stdc++.h>

using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
const int Nmax = 50000 + 5;
#define x first
#define y second
#define pb push_back
#define pii pair<int, int>
int n, m;
vector <pii> v[Nmax];
int dp[Nmax];
bool inq[Nmax];
priority_queue<pii, vector<pii>, greater<pii> >Q;
int main()
{
    fin >> n >> m;
    for(int i = 1, a, b, cst; i <= m; ++i)
    {
        fin >> a >> b >> cst;
        v[a].pb({b, cst});
    }
    Q.push({0, 1});
    for(int i = 1; i <= n; ++i)dp[i] = (1 << 30);
    dp[1] = 0; inq[1] = 1;
    while(!Q.empty())
    {
        int nod = Q.top().y;
        int cst = Q.top().x;
        inq[nod] = 0;
        Q.pop();
        for(auto i : v[nod])
        {
            if(dp[i.x] > dp[nod] + i.y)
            {
                dp[i.x] = dp[nod] + i.y;
                if(!inq[i.x])
                {
                    Q.push({dp[i.x], i.x});
                    inq[i.x] = 1;
                }
            }
        }
    }
    for(int i = 2; i <= n; ++i)
        if(dp[i] != (1 << 30))fout << dp[i] << ' ';
        else fout << 0 << ' ';
    return 0;
}