Cod sursa(job #2397491)

Utilizator Teodor01Dorde Teodor Teodor01 Data 4 aprilie 2019 14:28:01
Problema Distante Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.34 kb
#include <fstream>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;
int const NM = 101;
struct ura {
    int node , cost;
    bool operator > (ura x) const {
        return cost > x . cost;
    }
};
vector <ura> v [NM];
priority_queue <ura , vector <ura> , greater <ura> > q;
int dp [NM];
bool viz [NM];
ifstream cin ("a.in");
ofstream cout ("a.out");
void dijkstra (){
    q . push ({1 , 0});
    while (q . empty () == 0){
        int nod = q . top () . node;
        viz [nod] = 1;
        while (q . empty () == 0 && viz [q . top () . node] == 1)
            q . pop ();
        for(int i = 0 ; i < v [nod] . size () ; ++ i){
            int nod2 = v [nod][i] . node;
            int c = v [nod][i] . cost;
            if (dp [nod2] > dp [nod] + c){
                dp [nod2] = dp [nod] + c;
                q . push ({nod2 , dp [nod2]});
            }
        }
    }
}
int main()
{
    int n , m;
    cin >> n >> m;
    for(int i = 1 ; i <= m ; ++ i){
        int a , b , c;
        cin >> a >> b >> c;
        v [a] . push_back ({b , c});
    }
    fill (dp + 1 , dp + n + 1 , (1 << 30));
    dp [1] = 0;
    dijkstra ();
    for(int i = 2 ; i <= n ; ++ i)
        if (dp [i] == (1 << 30))
            cout << 0 << ' ';
        else
            cout << dp [i] << ' ';
    return 0;
}