Cod sursa(job #584803)

Utilizator andunhillMacarescu Sebastian andunhill Data 26 aprilie 2011 19:07:44
Problema Algoritmul lui Dijkstra Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1 kb
#include<fstream>
#include<queue>
#include<vector>
using namespace std;

#define oo 2147483647

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

struct nod
{	int to,cost;
	nod(int y,int c) { to = y , cost = c; }
};
typedef vector<nod>:: iterator it;

int N,M;
vector<nod>a[50001];
int d[50001];
bool in[50001];

struct cmp
{	public: 
	bool operator ()(int i,int j)
	{	if(d[i] > d[j]) return 1;
		return 0;
	}
};
priority_queue<int,vector<int>,cmp>q;

int main()
{	int i,j,x,y,c;
	f>>N>>M;
	for(i = 1; i <= M; i++)
	{	f>>x>>y>>c;
		a[x].push_back(nod(y,c));
	}
	for(i = 2; i <= N; i++)
		d[i] = oo;
	d[1] = 0; q.push(1); in[1] = 1;
	while(!q.empty())
	{	x = q.top(); q.pop(); in[x] = 0;
		for(it k = a[x].begin(); k != a[x].end(); k++)
			if(d[k->to] > d[x] + k->cost)
			{	d[k->to] = d[x] + k->cost;
				if(!in[k->to]) q.push(k->to) , in[k->to] = 1;
			}
	}
	for(i = 2; i <= N; i++)
		if(d[i] != oo) g<<d[i]<<" ";
		else g<<0<<" ";
	f.close();
	g.close();
	return 0;
}