Cod sursa(job #458904)

Utilizator digital_phreakMolache Andrei digital_phreak Data 26 mai 2010 21:38:55
Problema Algoritmul lui Dijkstra Scor 90
Compilator cpp Status done
Runda Arhiva educationala Marime 1.14 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <cstdio>

using namespace std;

const int maxn = 60010;
const int maxm = 250010;

int dist[maxn];
int parent[maxn];
int inqueue[maxn];

vector<pair<int,int> > G[maxn];
queue<int> Q;

ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");

int main() {
	
	ios::sync_with_stdio(false);
	
	int n,m,x,y,c;
	
	fin >> n >> m;
	
	for (int i=0;i<m;++i) {
		fin >> x >> y >> c;
		x--;
		y--;
		G[x].push_back(make_pair(y,c));
	}

	memset(dist,-1,sizeof(dist));
	memset(inqueue,0,sizeof(inqueue));
	
	dist[0] = 0;
	Q.push(0);
	inqueue[0] = 1;
	int ops = 0;
	
	while (!Q.empty()) {
		x = Q.front();
		Q.pop();
		inqueue[x] = 0;
		for (int i=0;i<G[x].size();++i) {
			ops++;
			if (dist[G[x][i].first] == -1 || dist[G[x][i].first] > dist[x] + G[x][i].second) {
				dist[G[x][i].first] = dist[x] + G[x][i].second;
				if (inqueue[G[x][i].first] == 0) Q.push(G[x][i].first);
			}
		}
	}
	
	cerr << ops << "\n";
	
	for (int i=1;i<n;++i) 
		fout << (dist[i] == -1 ? 0 : dist[i]) << " ";
	
	fin.close();
	fout.close();
	
	return 0;
}