Cod sursa(job #1700343)

Utilizator mihai995mihai995 mihai995 Data 10 mai 2016 01:58:09
Problema Algoritmul lui Dijkstra Scor 80
Compilator cpp Status done
Runda Arhiva educationala Marime 0.94 kb
#include <iostream>
#include <cstring>
#include <vector>
#include <set>
using namespace std;

const int N = 1 + 5e4, inf = 0x3f3f3f3f;

struct Node{
	int y, c;
	Node(int y, int c) : y(y), c(c) {}
};

vector<Node> graph[N];
set< pair<int, int> > S;
int dist[N], n;

inline void push(int x, int d){
	if ( dist[x] < d ) return;
	S.erase( make_pair(dist[x], x) );
	S.emplace(dist[x] = d, x);
}
inline int pop(){
	int x = S.begin() -> second;
	S.erase( S.begin() );
	return x;
}
void dijkstra(int x){
	memset(dist, inf, sizeof(dist));
	push(x, 0);
	while ( !S.empty() ){
		x = pop();
		for (Node e : graph[x])
			push(e.y, dist[x] + e.c);
	}
}

int main(){
	freopen("dijkstra.in", "r", stdin);
	freopen("dijkstra.out", "w", stdout);
	int m, x, y, c;

	cin >> n >> m;
	while (m--){
		cin >> x >> y >> c;
		graph[x].push_back( Node(y, c) );
	}
	dijkstra(1);

	for (int i = 2; i <= n; i++)
		cout << (dist[i] != inf ? dist[i] : 0) << ' ';
	cout << '\n';

	return 0;
}