Cod sursa(job #658714)

Utilizator alexdmotocMotoc Alexandru alexdmotoc Data 9 ianuarie 2012 13:33:27
Problema Algoritmul lui Dijkstra Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.17 kb
#include <iostream>
#include <cstdio>
#include <vector>
#include <queue>

using namespace std;

#define maxN 50005
#define INF 0x3f3f3f3f

struct nod
{
	int b , c;
};

vector <nod> lista[maxN];
queue <int> Q;

long long cost[maxN] , cost_aux;
int N , M;

int main ()
{
	freopen ("dijkstra.in" , "r" , stdin);
	freopen ("dijkstra.out" , "w" , stdout);
	
	scanf ("%d %d" , &N , &M);
	
	int a;
	nod t;
	
	for (int i = 1 ; i <= M ; ++i)
	{
		scanf ("%d %d %d" , &a , &t.b , &t.c);
		
		lista[a].push_back (t);
	}
	
	for (int i = 2 ; i <= N ; ++i)
		cost[i] = INF;
	
	int nodd;
	
	Q.push (1);
	cost[1] = 0;
	
	while (!Q.empty ())
	{
		nodd = Q.front ();
		
		for (unsigned i = 0 ; i < lista[nodd].size () ; ++i)
		{
			
			cost_aux = cost[nod] + lista[nod][i].c;
			
			if (cost[lista[nod][i].b] == INF)
				cost[lista[nod][i].b] = cost_aux;
			
			else
				if (cost[lista[nod][i].b] > cost_aux)
					cost[lista[nod][i].b] = cost_aux;
			
			
			Q.push (lista[nod][i].b);
		}
		
		Q.pop ();
	}
	
	for (int i = 2 ; i <= N ; ++i)
		if (cost[i] != INF)
			printf ("%lld " , cost[i]);
		
		else printf ("0 ");
	
	return 0;
}