Cod sursa(job #2388076)

Utilizator andreixxiLungu Andrei andreixxi Data 25 martie 2019 17:19:44
Problema Algoritmul lui Dijkstra Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.18 kb
#include <iostream>
#include <fstream>
#include <vector>

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];

int main()
{
    int n, m, a, b, c;
    f>>n>>m;
    for(int i=2; i<=n; i++)
        dist[i] = INF;
    dist[1] = 0;

    for(int i=1; i<=m; i++)
    {
        f>>a>>b>>c;
        graph[a].push_back(b);
        graphC[a].push_back(c);
    }

    int idx = 0;
    for(int t=1; t<=n; t++)
    {
        int mini = INF;
        for(int i=1; i<=n; i++)
            if(!viz[i] && dist[i] < mini)
            {
                mini = dist[i];
                idx = i;
            }
        viz[idx] = 1;

        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;
        }
    }
    for(int i=2; i<=n; i++)
    {
        if(dist[i] == INF)
            g<<0<<" ";
            else
            g<<dist[i]<<" ";
    }
}