Cod sursa(job #2243075)

Utilizator mihai.alphamihai craciun mihai.alpha Data 19 septembrie 2018 21:17:46
Problema Algoritmul lui Dijkstra Scor 90
Compilator cpp Status done
Runda Arhiva educationala Marime 1.13 kb
#include <fstream>
#include <iostream>
#include <vector>
#include <queue>
#include <cstring>

using namespace std;

const int maxn = 50004;

struct at  {
    int fi, se;
    const bool operator < (const at &a) const {
        return fi > a.fi;
    }
};

int n, m;
vector <at > v[maxn];
int d[maxn];

priority_queue <at> pq;

const int maxnr = 0x3f3f3f3f;

int main()  {
	freopen("dijkstra.in", "r", stdin);
	freopen("dijkstra.out", "w", stdout);
	cin >> n >> m;
	for(int i = 1;i <= m;i++)  {
        int a, b, c;
        cin >> a >> b >> c;
        v[a].push_back({b, c});
	}
	memset(d, 0x3f3f3f3f, sizeof d);
	pq.push({0, 1});
	while(!pq.empty())  {
        at nod = pq.top();
        pq.pop();
        int cst = nod.fi, nd = nod.se;
        if(d[nd] == maxnr)  {
            d[nd] = cst;
            for(auto &x : v[nd])  {
                if(d[x.fi] == maxnr)  {
                    pq.push({cst + x.se, x.fi});
                }
            }
        }
	}
	for(int i = 2;i <= n;i++)
        if(d[i] != maxnr)
            cout << d[i] << " ";
        else
            cout << "0 ";
	return 0;
}