Cod sursa(job #744284)

Utilizator mihaibogdan10Mihai Bogdan mihaibogdan10 Data 8 mai 2012 11:11:00
Problema Algoritmul lui Dijkstra Scor 50
Compilator cpp Status done
Runda Arhiva educationala Marime 0.99 kb
#include<cstdio>
#include<vector>
#include<queue>
#define inf 0xfffffff
using namespace std;

struct muchie	{int y, c;};
struct nod {int n, c;};
vector <muchie> muchii[50001];
vector <int> cost;
int n, m, poz;

struct compara{
	bool operator()(const nod &n1, const nod& n2) const
		{return n1.c < n2.c;}
};
priority_queue <nod, vector<nod>, compara> heap;

int main(){
	freopen("dijkstra.in", "r", stdin),	freopen("dijkstra.out", "w", stdout);
	scanf("%d %d", &n, &m);
	int i, x, y, c;
	
	for (i = 0; i < m; i++){
		scanf("%d %d %d", &x, &y, &c);
		muchii[x].push_back( (muchie){y, c} );
	}
	cost.assign(n+1, inf);
	cost[1] = 0;
	heap.push( (nod){1, 0} );
	
	while (!heap.empty()){
		int x = heap.top().n;
		heap.pop();
		
		for (i = 0 ;i < (int)muchii[x].size(); i++){
			y = muchii[x][i].y;
			c = muchii[x][i].c;
			if (cost[y] > cost[x] + c){
				cost[y] = cost[x] + c;
				heap.push( (nod){y, cost[y]} );
			}
		}
	}
	
	for (i = 2; i <= n; i++) printf("%d ", cost[i] != inf ? cost[i] : 0);
	return 0;
}