Cod sursa(job #3225852)

Utilizator CondoracheTudorCondorache Tudor CondoracheTudor Data 19 aprilie 2024 10:15:41
Problema Algoritmul lui Dijkstra Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.18 kb
#include <fstream>
#include <queue>
#include <vector>

using namespace std;
#define Inf 0x3f3f3f3f

ifstream cin("dijkstra.in");
ofstream cout("dijkstra.out");

using PI = pair<int , int>;
int n , m , x , y , p , D[100001] , w;
vector <PI> G[100001];

void dijkstra(int nod)
{
    queue < PI> Q;
    D[nod] = 0;
    Q.push({0 , nod});
    while(!Q.empty())
    {
       x = Q.front().first;
       y = Q.front().second;
       Q.pop();
       if(x > D[y])
        continue;
       for(size_t q = 0;q<G[y].size();++q)
       {
           int nodnou = G[y][q].first;
           int costnou = G[y][q].second;
           if(D[nodnou] > D[y] + costnou)
           {
               D[nodnou] = D[y] + costnou;
               Q.push({D[nodnou] , nodnou});
           }
       }
    }
}

int main()
{
    cin >> n >> m;
    for(int i = 1 ; i <= m ; ++i)
    {
        cin >> x >> y >> w;
        G[x].push_back({y , w});
    }
    for(int i = 1 ; i <= n ; i++)
        D[i] = Inf;
    p=0;
    dijkstra(p);

    for(int i = 2 ; i <= n ; i++)
        if(D[i] == Inf)
            cout << "0 ";
        else
            cout << D[i] << " ";

        return 0;
}