Cod sursa(job #2203183)

Utilizator WebDesignbyTMGhiorghiu Ioan-Viorel WebDesignbyTM Data 11 mai 2018 12:05:56
Problema Algoritmul lui Dijkstra Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 0.9 kb
#define DM 500001
#define inf 0x3f3f3f3f
#include <cstring>
#include <fstream>
#include <queue>
#include <vector>
using namespace std;

ifstream fi ("dijkstra.in");
ofstream fo ("dijkstra.out");
int n, m, dst[DM], a, b, c;
struct str
{
	int n, c;
	bool operator < (const str &other) const
	{
		return c > other.c;
	}
} x;
priority_queue <str> pq;
vector <str> v[DM];

void dijkstra()
{
	dst[1] = 0;
	pq.push({1, 0});
	while (!pq.empty())
	{
		x = pq.top();
		pq.pop();
		if (dst[x.n] < x.c)	
			continue;
		for (auto i:v[x.n])
			if (i.c + x.c < dst[i.n])
			{
				dst[i.n] = x.c + i.c;
				pq.push({i.n, dst[i.n]});
			}
	}
}

int main()
{
	fi >> n >> m;
	for (int i = 1; i <= n; ++i)
	{
		fi >> a >> b >> c;
		v[a].push_back({b,c});
		v[b].push_back({a, c});
	}
	memset(dst, inf, sizeof(dst));
	dijkstra();
	for (int i = 2; i <= n; ++i)
		if (dst[i] != inf)
			fo << dst[i] << ' ';
		else
			fo << 0 << ' ';
	return 0;
}