Cod sursa(job #2401075)

Utilizator andreixxiLungu Andrei andreixxi Data 9 aprilie 2019 13:32:49
Problema Algoritmul lui Dijkstra Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.37 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>

using namespace std;
ifstream f("dijkstra.in");
ofstream g("dijkstra.out");
int viz[50005], dist[50005];

#define INF 1000000007

vector <int> graph[50005];
vector <int> graphC[50005];
priority_queue < pair <int, int> >myQueue;
pair <int, int> best;
void Dijkstra (int n)
{
    for(int i=2; i<=n; i++)
    {
        dist[i] = INF;
        myQueue.push(make_pair(-dist[i], i));
    }

    while(!myQueue.empty())
    {
        best = myQueue.top();
        myQueue.pop();
        int idx = best.second; //nodul

        int dim = graph[idx].size();
        for(int i=0; i<dim; i++)
        {
            int vecin = graph[idx][i];
            int cost = graphC[idx][i];
            if(dist[idx] + cost < dist[vecin])
                dist[vecin] = dist[idx] + cost;
        }
    }
}
int main()
{
    int n, m, a, b, c;
    f>>n>>m;
    dist[1] = 0;
    //(-dist[idx], idx);
    myQueue.push(make_pair(-0, 1));
    best = myQueue.top();
    //best.first = distanta,   best.second =idx

    for(int i=1; i<=m; i++)
    {
        f>>a>>b>>c;
        graph[a].push_back(b);
        graphC[a].push_back(c);
    }
    Dijkstra(n);
    for(int i=2; i<=n; i++)
    {
        if(dist[i] == INF)
            g<<0<<" ";
        else
            g<<dist[i]<<" ";
    }
}