Cod sursa(job #942768)

Utilizator tudorv96Tudor Varan tudorv96 Data 23 aprilie 2013 15:10:44
Problema Algoritmul lui Dijkstra Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.85 kb
#include <fstream>
#include <queue>
#include <vector>
using namespace std;

#define in "dijkstra.in"
#define out "dijkstra.out"
#define N 50005
#define INF 1 << 30
#define pb push_back
#define c first
#define y second

typedef unsigned u;
typedef pair<int, int> nod;

vector <int> d (N, INF);
queue <int> Q;
vector <nod> LIST[N];
int n, m;

int main () {
	ifstream fin (in);
	fin >> n >> m;
	for (int i = 0; i < m; ++i) {
		int x, y, c;
		fin >> x >> y >> c;
		LIST[x].pb (nod (c, y));
	}
	fin.close();
	Q.push (1);
	d[1] = 0;
	while (Q.size()) {
		int x = Q.front();
		Q.pop();
		for (u i = 0; i < LIST[x].size(); ++i) {
			if (d[x] + LIST[x][i].c < d[LIST[x][i].y]) {
				d[LIST[x][i].y] = d[x] + LIST[x][i].c;
				Q.push (LIST[x][i].y);
			}
		}
	}
	ofstream fout (out);
	for (int i = 2; i <= n; ++i)
		fout << (d[i] == INF ? 0 : d[i]) << " ";
	fout.close();
	return 0;
}