Cod sursa(job #2673419)

Utilizator Iustin01Isciuc Iustin - Constantin Iustin01 Data 16 noiembrie 2020 19:20:27
Problema Algoritmul lui Dijkstra Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.17 kb

#include <bits/stdc++.h>
#define MAX 100005
#define oo 999999999
using namespace std;

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

vector < pair < int , int > > g[MAX];
int n, m, x, y, cost, d[MAX];
bool c[MAX];

struct cmp{
    bool operator()(int a, int b){
        return d[a] > d[b];
    }
};
priority_queue < int, vector < int >, cmp> q;
void Dijkstra(int k){
    c[k] = true;
    for(int i = 1; i <= n; i++)
        d[i] = oo;

    d[k] = 0;
    q.push(k);

    while(!q.empty()){
        k = q.top();
        q.pop();
        c[k] = 0;
        for(int i = 0; i < g[k].size(); i++){
            int vec = g[k][i].first;
            cost = g[k][i].second;
            if(d[vec] > d[k] + cost){
                d[vec] = d[k] + cost;
                if(!c[vec]){
                    c[vec] = 1;
                    q.push(vec);

                }
            }
        }
    }
}

int main(){
    in>>n>>m;
    while(in>>x>>y>>cost){
        g[x].push_back({ y, cost });
    }

    Dijkstra(1);

    for(int i = 2; i <= n; i++)
        if(d[i] != oo )
            out<<d[i]<<" ";
        else
            out<<0<<" ";
}