Pagini recente » Cod sursa (job #1122448) | Cod sursa (job #1923728) | Cod sursa (job #2105754) | Cod sursa (job #250029) | Cod sursa (job #233524)
Cod sursa(job #233524)
#include <stdio.h>
#include <time.h>
struct nod
{
int vf,c;
nod *urm;
};
typedef nod *lista;
lista A[50001],que,ultim;
int C[50001];
float t1,t2;
void add(int x,int c)
{
lista urm;
if (que==NULL)
{
urm = new nod;
urm->vf = x;
urm->c = c;
urm->urm = NULL;
que = urm;
ultim = que;
}
else {
urm = new nod;
urm->vf = x;
urm->c = c;
urm->urm = NULL;
ultim->urm = urm;
ultim = urm;
}
}
void BF()
{
lista w;
int c,x,y;
while (que!=NULL)
{
w = A[que->vf];
c = que->c;
while (w!=NULL)
{
x = w->vf;y= w->c;
if (C[x]==0) C[x]=c+y,add(x,c+y);
else
if (C[x]>c+y) C[x]=c+y,add(x,c+y);
w = w->urm;
}
que =que->urm;
}
}
int main()
{
t1 = clock();
FILE *in = fopen("dijkstra.in","r");
FILE *out = fopen("dijkstra.out","w");
int n,i,m,x,y,c;
lista urm;
fscanf(in,"%d%d",&n,&m);
for (;m;m--)
{
fscanf(in,"%d%d%d",&x,&y,&c);
urm = new nod;
urm->vf = y;
urm->c = c;
urm->urm = A[x];
A[x] = urm;
}
//int s = 1;
//C[s] = 0;
//que = NULL;
add(1,0);
BF();
for (i=2;i<=n;i++) fprintf(out,"%d ",C[i]);
t2 = clock();
printf("%f",(t2-t1)/CLK_TCK);
scanf("\n");
}