Cod sursa(job #2230639)

Utilizator DandeacDan Deac Dandeac Data 10 august 2018 20:26:21
Problema Algoritmul lui Dijkstra Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.16 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <algorithm>
#include <cstring>
using namespace std;

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

#define Gmax 50010
#define INF 0x3f3f3f3f

struct point{
    int nod;
    int cost;
};

bool operator<(point a,point b)
{
    return a.cost > b.cost;
}

vector <pair <int,int> > G[Gmax];
priority_queue <point> pq;
int dist[Gmax];


int main()
{
    int N,M;
    f>>N>>M;

    for(int i=1;i<=M;i++)
    {
        int x,y,z;
        f>>x>>y>>z;
        G[x].push_back(make_pair(y,z));
    }
    memset(dist,0x3f,sizeof(dist));

    pq.push({1,0});

    while(!pq.empty())
    {
        int nod = pq.top().nod;
        int d = pq.top().cost;
        pq.pop();

        if(dist[nod] != INF)
            continue;
        dist[nod] = d;


        for(int i=0; i < G[nod].size(); i++)
        {
            pq.push({G[nod][i].first,d + G[nod][i].second});
        }
    }

    for(int i = 2 ; i<=N ; i++)
    {
        if(dist[i]==INF) g<<0<<' ';
        else
            g<<dist[i] <<' ';
    }

    cout<<endl;

    return 0;
}