Cod sursa(job #2539844)

Utilizator luci.tosaTosa Lucian luci.tosa Data 6 februarie 2020 13:23:42
Problema Algoritmul lui Dijkstra Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.96 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
int n, m, viz[50005], cost[50005];
struct nod {
    int x, c;
}aux1;
vector <nod> g[50005];
queue <int> q;
void bfs(int nod) {
    viz[nod] = 1;
    q.push(nod);
    while(!q.empty()) {
        int aux = q.front();
        q.pop();
        for(int i = 0; i < g[aux].size(); i++) {
            aux1 = g[aux][i];
            if(!viz[aux1.x]) {
                viz[aux1.x]=1;
                q.push(aux1.x);
                cost[aux1.x] = cost[aux] + aux1.c;
            }
        }
    }
}
int main() {
    fin >> n >> m;
    int a, b, c;
    for(int i = 1; i <= m; i++) {
        fin >> a >> b >> c;
        nod aux;
        aux.x = b;
        aux.c = c;
        g[a].push_back(aux);
    }
    bfs(1);
    for(int i = 2; i <= n; i++)
        fout << cost[i] << " ";
    return 0;
}