Cod sursa(job #3201694)

Utilizator stefan2806Radulescu Stefan stefan2806 Data 9 februarie 2024 16:33:15
Problema Algoritmul lui Dijkstra Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.23 kb
#include <fstream>
#define max_value 2000000000
#include <vector>
#include <queue>

using namespace std;

ifstream cin("dijkstra.in");
ofstream cout("dijkstra.out");

int d[50010],n,m,i,x,y,c;

struct NodeCost{

    int node, cost;

    bool operator < (const NodeCost other) const {

        return cost > other.cost;

    }
};

vector<NodeCost>v[50010];
priority_queue<NodeCost>q;

void dijkstra(int nod)
{
    int i,curentDist,curentNod,nr;
    for(i=1; i<=n; i++)
        d[i]=max_value;
    q.push({nod,0});
    while(!q.empty())
    {
        curentNod=q.top().node;
        curentDist=q.top().cost;
        q.pop();

        if(d[curentNod]!=max_value)
            continue;

        d[curentNod]=curentDist;

        for(auto it: v[curentNod])
        {
            if(d[it.node]==max_value)
            {
                    q.push({it.node,curentDist+it.cost});
            }
        }
    }
}

int main()
{
    cin>>n>>m;
    for(i=1; i<=m; i++)
    {
        cin>>x>>y>>c;
        v[x].push_back({y,c});
    }
    dijkstra(1);
    for(i=2; i<=n; i++)
    {
        if(d[i]==max_value)
            cout<<0<<" ";
        else
            cout<<d[i]<<" ";
    }
    return 0;
}