Cod sursa(job #571931)

Utilizator DaninetDani Biro Daninet Data 4 aprilie 2011 21:30:13
Problema Algoritmul lui Dijkstra Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 0.73 kb
#include<cstdio>

#define INF 0x3f3f3f3f

struct line{
	int x, y, c;             
} G[500001];

int d[500001], n, m;
bool ok;


int main() {
	FILE *f = fopen("dijkstra.in", "r");
	FILE *g = fopen("dijkstra.out", "w");
	int first = 1;
	fscanf(f,"%d %d", &n, &m);
	for(int i=1; i<=m; i++){
		fscanf(f,"%d %d %d", &G[i].x, &G[i].y, &G[i].c);
		if(G[i].x==first)       
			d[G[i].y]=G[i].c; 
	}

	for(int i=1; i<=n; i++)
		if(d[i]==0)
			d[i]=INF;
		
	do{
		ok=true;  
		for(int i=1; i<=m; i++) 
			if(d[G[i].y] > d[G[i].x]+G[i].c) {
				d[G[i].y] = d[G[i].x]+G[i].c;
				ok=false;
			}
	} while(!ok);
	
	for(int i=2; i<=n; i++)
		if(d[i]!=INF)  
			fprintf(g,"%d ", d[i]);
		/*else     
			h<<"0 ";*/
}