Cod sursa(job #3284431)

Utilizator mateistefan11matei stefan mateistefan11 Data 11 martie 2025 17:14:29
Problema Algoritmul lui Dijkstra Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.21 kb
#include <bits/stdc++.h>
#define oo 100101010
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
int n,m;
vector< pair<int,int> > G[100005];
priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>> q;
int d[100005], viz[100005];
void Djikstra(int x)
{
    for(int i = 1; i <= n; i++)
        d[i] = oo;
    d[x] = 0;
    q.push({0,x});
    while(!q.empty())
    {
        auto t = q.top();
        q.pop();
        if(d[t.second] == oo) return;
        if(!viz[t.second])
        {
            viz[t.second] = 1;
            for(auto e : G[t.second])
            {
                if(d[e.second] > e.first + d[t.second])
                {
                    d[e.second] = e.first + d[t.second];
                    q.push({d[e.second], e.second});
                }

            }
        }
    }
}
int main()
{
    ios_base::sync_with_stdio(0);
    fin.tie(0);
    fout.tie(0);
    int i,j,c;
    fin >> n >> m;
    while(m--)
    {
        fin >> i >> j >> c;
        G[i].push_back({c,j});
    }
    Djikstra(1);
    for(int i = 2; i <= n; i++)
        if(d[i] == oo) fout << 0 << " ";
        else fout << d[i] << " ";
    return 0;
}