Cod sursa(job #277737)

Utilizator alex3el_n2oAlex Vladescu alex3el_n2o Data 11 martie 2009 21:22:40
Problema Algoritmul lui Dijkstra Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 1.09 kb
#include <stdio.h>
int v[50005],d[50005];
long min,c;
struct nod{
	long use,cost;
	nod *adr;
} *q[50005],*in[50005];

void add(long aa,long bb,long cc)
{
	nod *aux;
	aux=new nod;
	aux->use=bb;
	aux->cost=cc;
	aux->adr=NULL;
	if (in[aa]==NULL)
	{
		q[aa]=aux;
		in[aa]=q[aa];
	}
	else
	{
		q[aa]->adr=aux;
		q[aa]=aux;
	}
}

int main()
{
	freopen("dijkstra.in","r",stdin);
	freopen("dijkstra.out","w",stdout);
	long n,m,i,j,a,b;
	nod *aux;
	scanf("%ld %ld",&n,&m);
	for (i=1;i<=m;i++)
	{
		scanf("%ld%ld%ld",&a,&b,&c);
		//x[a][b]=c;
		add(a,b,c);
	}
	for (i=2;i<=n;i++)
		d[i]=2000000000;

	d[1]=0;
	for (i=1;i<=n;i++)
	{
		min=2000000000;
		for (j=1;j<=n;j++)
			if (d[j]<min&&!v[j])
			{
				min=d[j];
				c=j;
			}
		v[c]=1;
		aux=in[c];
		while (aux!=NULL)
		{
			if (d[c]+aux->cost<d[aux->use])
				d[aux->use]=d[c]+aux->cost;
			aux=aux->adr;
		}
		
		//while (
		
		/*
			for (j=1;j<=n;j++)
			if (x[c][j]&&!v[j]&&d[c]+x[c][j]<d[j])
				d[j]=d[c]+x[c][j];
			*/
	}
	for (i=2;i<=n;i++)
		printf("%d ",d[i]);
	printf("\n");
	return 0;
}