Pagini recente » Cod sursa (job #943027) | Cod sursa (job #466618) | Cod sursa (job #2572933) | Cod sursa (job #2367814) | Cod sursa (job #233530)
Cod sursa(job #233530)
#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)
{
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 = C[que->vf];
while (w!=NULL)
{
x = w->vf; y= w->c;
if (C[x]>c+y || C[x]==0) C[x]=c+y,add(x);
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);
BF();
for (i=2;i<=n;i++) fprintf(out,"%d ",C[i]);
//t2 = clock();
//printf("%f",(t2-t1)/CLK_TCK);
//scanf("\n");
}