Cod sursa(job #2369651)

Utilizator andrei32576Andrei Florea andrei32576 Data 6 martie 2019 08:41:13
Problema Algoritmul lui Dijkstra Scor 90
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.1 kb
#include <bits/stdc++.h>
using namespace std;

int n,m,i,x,y,c;
int d[50005];
int inf=INT_MAX;

struct muchie
{
    int x,c;
};
vector <muchie> G[50005];

struct comp
{
    bool operator() (int x,int y)
    {
        return d[x]>d[y];
    }
};

priority_queue <int, vector<int>, comp> pq;

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

void dijkstra(int S)
{
    int i;
    for(i=1;i<=n;i++)
        d[i]=inf;
    d[S]=0;
    pq.push(S);

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

        for(auto &it : G[nod])
        {
            int nc=it.x;
            int c=it.c;
            if(d[nod]+c<d[nc])
            {
                d[nc]=d[nod]+c;
                pq.push(nc);
            }
        }
    }
}

int main()
{
    f>>n>>m;

    for(i=1;i<=m;i++)
    {
        f>>x>>y>>c;
        G[x].push_back({y,c});
    }

    dijkstra(1);

    for(i=2;i<=n;i++)
    {
        if(d[i]==inf)
            g<<0<<" ";
        else
            g<<d[i]<<" ";
    }

    f.close();
    g.close();
    return 0;
}