Cod sursa(job #557552)

Utilizator cezyGrigore Cezar cezy Data 16 martie 2011 18:24:24
Problema Algoritmul lui Dijkstra Scor 80
Compilator cpp Status done
Runda Arhiva educationala Marime 0.75 kb
#include<fstream>
#include<vector>
#include<queue>
using namespace std;
#define nmax 50100
#define pb push_back
#define inf 1000000000
vector<int> g[nmax],c[nmax];
int d[nmax],q[1<<20];
 priority_queue <int> q2;
int viz[nmax];
int n,sf,inc,m;
int main ()
{
	ifstream fin("dijkstra.in");
	ofstream fout("dijkstra.out");
	int i,j,k,a,b,t;
	fin>>n>>m;
	for(i=1;i<=m;i++)
		fin>>a>>b>>t,g[a].pb(b),c[a].pb(t);
	for(i=1;i<=n;i++)
		d[i]=inf;
	inc=sf=0;
	q2.push(1);d[1]=0;
	while(inc<=sf)
	{
		a=q2.top();
		q2.pop();inc++;
		for(i = 0; i < g[a].size(); i++)
        if( d[ g[a][i] ] > d[a] + c[a][i] )
          d[ g[a][i] ] = d[a] + c[a][i], q2.push( g[a][i]),sf++;
	}
	for(i = 2; i <= n; i++)
        if(d[i]==inf) fout<<0<<' ';
		else fout<<d[i]<<' ';

	return 0;
}