Cod sursa(job #2167000)

Utilizator andreimuthMuth Andrei andreimuth Data 13 martie 2018 19:45:38
Problema Algoritmul lui Dijkstra Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.08 kb
#include <fstream>
#include <vector>
#include <queue>
 #define nmax 50002
#define cost first
#define nod second
#define INF 0x3f3f3f3f
using namespace std;
ifstream fin ("dijkstra.in");
ofstream fout ("dijkstra.out");
priority_queue <pair <int, int>, vector <pair <int, int> >, greater <pair <int, int> > > pq;
vector < pair < int, int > > g[nmax];
int viz[nmax], i, j, c, n, m;
pair < int, int > aux;

int main()
{
    fin >> n >> m;
    while (fin >> i >> j >> c)
       g[i].push_back (make_pair (c, j));

    pq.push (make_pair (0, 1));

    while (!pq.empty())
    {
        aux = pq.top ();
        pq.pop();
        if (!viz[aux.nod])
        {
            viz[aux.nod] = aux.cost;
            for (i = 0; i < g[aux.nod].size (); i++)
            {
                if (!viz[g[aux.nod][i].nod])
                    pq.push (make_pair (aux.cost + g[aux.nod][i].cost, g[aux.nod][i].nod));
            }
        }
    }

     for (i = 2; i <= n; i ++)
    {
        fout << viz[i] << " ";
    }


    fin.close ();
    fout.close ();
    return 0;
}