Cod sursa(job #706204)

Utilizator andrici_cezarAndrici Cezar andrici_cezar Data 5 martie 2012 18:58:33
Problema Algoritmul lui Dijkstra Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 0.94 kb
#include <cstdio>
#include <vector>
#include <queue>
#include <string.h>
#define maxn 50001
using namespace std;

struct nodulet {long d; long lg;} arc;

vector < nodulet > A[maxn];
queue < long > q;
long x, i, N, K, elc, C[maxn], G[maxn];
bool viz[maxn];

int main() {
	freopen("dijkstra.in","r",stdin);
	freopen("dijkstra.out","w",stdout);
	
		scanf("%ld %ld", &N, &K);
		
		for (i = 0; i < K; ++i) {
			scanf("%ld %ld %ld", &x, &arc.d, &arc.lg);
			A[x].push_back(arc);
		}
		
		for (i = 1; i <= N; ++i) {
			G[i] = A[i].size();
		}
		memset(C, 5000000, sizeof(C));
		C[1] = 0;
		q.push(1);
		
		for (; !q.empty(); q.pop()) {
			elc = q.front();
			
			for (i = 0; i < G[elc]; ++i) {
				if ( C[elc] + A[elc][i].lg < C[A[elc][i].d] ) {
					C[ A[elc][i].d ] = C[elc] + A[elc][i].lg;
					q.push(A[elc][i].d);
				}
			}
		}
		
		for (i = 2; i <= N; ++i) {
			printf("%ld ", C[i]);
		}
		printf("\n");
	
	return 0;
}