Pagini recente » Cod sursa (job #782453) | Cod sursa (job #2392920) | Cod sursa (job #1298217) | Cod sursa (job #1384103) | Cod sursa (job #233527)
Cod sursa(job #233527)
#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[w->vf]>c+w->c || C[w->vf]==0) C[w->vf]=c+w->c,add(w->vf,c+w->c);
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");
}