Cod sursa(job #744278)

Utilizator mihaibogdan10Mihai Bogdan mihaibogdan10 Data 8 mai 2012 10:37:06
Problema Algoritmul lui Dijkstra Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1 kb
#include<cstdio>
#include<vector>
#include<set>
#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()(nod n1, nod n2)	const
		{return n1.c < n2.c;}
};
multiset <nod, compara> heap;

int main(){
	freopen("dijkstra.in", "r", stdin);
	freopen("dijkstra.out", "w", stdout);
	citeste(n); citeste(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.insert( (nod){1, 0} );
	
	while (!heap.empty()){
		multiset<nod, compara>::iterator it = heap.begin();
		int x = (*it).n;
		
		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.insert( (nod){y, cost[y]} );
			}
		}
		heap.erase(it);
	}
	
	for (i = 2; i<=n; i++) printf("%d ", cost[i] != inf ? cost[i] : 0);
	return 0;
}