Cod sursa(job #3251512)

Utilizator IleaIlea Bogdan Ilea Data 26 octombrie 2024 10:23:02
Problema Algoritmul lui Dijkstra Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.74 kb
#include <bits/stdc++.h>

using namespace std;

string file="dijkstra";
ifstream fin(file+".in");
ofstream fout(file+".out");

map<int, map<int, int> > g;
int b[50001];
void dijkstra(int i)
{
    b[i]=1;
    queue<int> q;
    q.push(i);
    while (!q.empty()){
        i=q.front();
        q.pop();
        for (auto it:g[i]){
            if (!b[it.first] || b[it.first]>b[i]+it.second){
                b[it.first]=b[i]+it.second;
                q.push(it.first);
            }
        }
    }
}
int main()
{
    int n, m;
    fin>>n>>m;
    while (m){
        m--;
        int x, y, z;
        fin>>x>>y>>z;
        g[x][y]=z;
    }
    dijkstra(1);
    for (int i=2; i<=n; i++)fout<<b[i]-1<<" ";
    return 0;
}