Cod sursa(job #759321)

Utilizator VisuianMihaiMihai Visuian VisuianMihai Data 17 iunie 2012 16:00:03
Problema Algoritmul lui Dijkstra Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.01 kb
#include<fstream>
#include<vector>
#include<queue>
#define mp make_pair
#define INF 1000000000
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("djikstra.out");
queue<int>q;
vector< pair<int,int> >a[50001]; 
int c[50001];
void dijkstra(int n)
{
	int curent = 1;
	for ( int i = 2; i <= n; i++ )
	{
		c[i] = INF;
	}
	q.push(curent);
	while ( !q.empty() )
	{
		curent = q.front();
		for ( int i = 0; i < a[curent].size(); i++ )
		{
			if ( c[curent] != INF )
			{
				if ( a[curent][i].second + c[curent] < c[a[curent][i].first] )
				{
					c[a[curent][i].first] = a[curent][i].second + c[curent];
					q.push(a[curent][i].first);
				}
			}
		}
		q.pop();
	}
}
int main()
{
	int n, m, x, y, cost, i;
	fin >> n >> m;
	for ( i = 1; i <= m; i++ )
	{
		fin >> x >> y >> cost;
		a[x].push_back(mp(y,cost));
	}
	dijkstra(n);
	for ( i = 2; i <= n; i++ )
	{
		if ( c[i] == INF )
			fout << 0 << ' ';
		else
			fout << c[i] << ' ';
	}
	fin.close();
	fout.close();
	return 0;
}