Cod sursa(job #2160340)

Utilizator calinlixandruLixandru Calin-Mihai calinlixandru Data 11 martie 2018 12:34:25
Problema Algoritmul lui Dijkstra Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.06 kb
#include <iostream>
#include <fstream>
#include <queue>
#include <vector>

#define maxx 50002
#define cost first
#define nod second
#define inf 0x3f3f3f3f

using namespace std;

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

int n,m,j,c,i,viz[maxx];
vector < pair <int,int> > G[maxx];
vector < pair <int,int> > :: iterator it;
priority_queue < pair < int, int >, vector < pair < int, int > >, greater < pair < int, int > > > pq;
pair <int, int> aux;
int main()
{
    f>>n>>m;
    while(f>>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(it=G[aux.nod].begin();it!=G[aux.nod].end();it++)
            {
                if(!viz[(*it).nod])
                    pq.push(make_pair(aux.cost+(*it).cost,(*it).nod));
            }
        }
    }
    for(i=2;i<=n;i++)
    {
        g<<viz[i]<<" ";
    }
    f.close();
    g.close();
    return 0;
}