Cod sursa(job #2220094)

Utilizator mihai50000Mihai-Cristian Popescu mihai50000 Data 10 iulie 2018 15:30:33
Problema Algoritmul lui Dijkstra Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.86 kb
#include <bits/stdc++.h>

using namespace std;

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

#define pb push_back

const int oo = 0x3f3f3f3f;
const int N =5e4 + 17;

vector <pair <int, int> > v[N];

int D[N];

bitset <N> viz;

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

priority_queue <int, vector <int>, cmp> Q;

int n;

void Dijkstra()
{
	for(int i = 2; i <= n; i++)
		D[i] = oo;
	Q.push(1);
	viz[1] = 1;
	while(!Q.empty())
	{
		int nod = Q.top();
		Q.pop();
		viz[nod] = 0;
		for(auto& i : v[nod])
		{
			if(D[nod] + i.second < D[i.first] && !viz[i.first])
				viz[i.first] = 1, D[i.first] = i.second + D[nod], Q.push(i.first);
		}
	}
}

int main()
{
	int m;
	f >> n >> m;
	while(m--)
	{
		int x, y, c;
		f >> x >> y >> c;
		v[x].pb({y, c});
	}
	Dijkstra();
	for(int i = 2; i <= n; i++)
		g << ((D[i] == oo) ? 0 : D[i]) << ' ';
}