Cod sursa(job #2860391)

Utilizator Moise_AndreiMoise Andrei Moise_Andrei Data 2 martie 2022 15:07:31
Problema Algoritmul lui Dijkstra Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.97 kb
#include <bits/stdc++.h>
using namespace std;
ifstream in("dijkstra.in");
ofstream out("dijkstra.out");
vector <pair <int, int> > v[50005];
queue <pair <int, int> > q;
int dist[50005];
int main()
{
    int n, m;
    in >> n >> m;
    for(int i = 1; i <= m; i ++)
    {
        int a, b, c;
        in >> a >> b >> c;
        v[a].push_back(make_pair(b, c));
    }
    q.push(make_pair(1, 0));
   /// cout << '\n';
    while(!q.empty())
    {
        pair <int, int> x = q.front();
        q.pop();
        for(int i = 0; i < v[x.first].size(); i ++)
        {
            if(dist[v[x.first][i].first] == 0 && (v[x.first][i].first != x.first))
            {
                dist[v[x.first][i].first] = dist[x.first] + v[x.first][i].second;
                q.push(v[x.first][i]);
                ///cout << x << " " << v[x][i] << '\n';
            }
        }
    }
    for(int i = 2; i <= n; i ++)
    {
        out << dist[i] << " ";
    }
    return 0;
}