Cod sursa(job #2843495)

Utilizator octavian2411Cretu Octavian octavian2411 Data 2 februarie 2022 16:11:25
Problema Algoritmul lui Dijkstra Scor 20
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.07 kb
#include <fstream>
#include <queue>
#include <vector>
#define N 50001
#define M 250001
#define INF 1000000001

using namespace std;

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

priority_queue<pair<int,int>>h;
vector<pair<int,int>>succesori[N];
int d[N],n,m;
bool selectat[N];

void dijkstra(int x0)
{
    for (int i = 1; i <= n; i++)
    {
        d[i] = INF;
    }
    d[x0] = 0;
    h.push({0,x0});
    while (!h.empty())
    {
        int x = h.top().second;
        h.pop();
        if (selectat[x])
            continue;
        selectat[x]=1;
        for (auto p:succesori[x])
        {
            int y=p.first;
            int c=p.second;
            if (d[x]+c<d[y])
            {
                d[y]=d[x]+c;
                h.push({-d[y],y});
            }
        }
    }
}

int main()
{
    in>>n>>m;
    for (int i=1;i<=m;i++)
	{
	    int x, y, dist;
	    in>>x>>y>>dist;
	    succesori[x].push_back({y,dist});
	}
	dijkstra(1);
	for (int i=2;i<=n;i++)
    {
        out<<d[i]<<" ";
    }
    return 0;
}