Cod sursa(job #2109961)

Utilizator cristii2000cristiiPanaite Cristian cristii2000cristii Data 20 ianuarie 2018 11:46:43
Problema Algoritmul lui Dijkstra Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.98 kb
#include <iostream>
#include <fstream>
#include <queue>
#define Edge pair <int, int>
#define cost first
#define y second

using namespace std;

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

int n, m;

const int MAXN = 50005;
const int inf = 0x3f3f3f;

int dist[MAXN];

vector <Edge> Graf[MAXN];

priority_queue <Edge, vector <Edge>, greater <Edge> > heap;

void citire()
{
	in >> n >> m;
	for(int i = 1; i <= n; i++)
	{
		int a, b, c;
		in >> a >> b >> c;
		Graf[a].push_back({c, b});
	}
}

void dijkstra()
{
	heap.push({0, 1});

    while(!heap.empty())
	{
		Edge New = heap.top();
		heap.pop();
        for(Edge e : Graf[New.y])
		{
			int tmp = e.cost + dist[New.y];

			if(tmp < dist[e.y])
			{
				dist[e.y] = tmp;
				heap.push(e);
			}
		}
	}
}

int main()
{
	for(int i = 0; i < 50005; i++)
		dist[i] = inf;
	dist[1] = 0;
	citire();
	dijkstra();
	for(int i = 2; i <= n; i++)
		out << dist[i] << " ";
    return 0;
}