Pagini recente » Cod sursa (job #1649829) | Cod sursa (job #1495056) | Cod sursa (job #3202366) | Cod sursa (job #1245369) | Cod sursa (job #233532)
Cod sursa(job #233532)
#include <stdio.h>
struct cod
{
int x;
cod *urm;
} *que,*ultim,*next;
struct nod
{
int vf,c;
nod *urm;
};
typedef nod *lista;
//typedef cod *coada;
lista A[50001];
//coada que,ultim;
int C[50001];
void add(int x)
{
if (que==NULL)
{
next = new cod;
next->x = x;
//urm->c = c;
next->urm = NULL;
que = next;
ultim = que;
}
else {
next = new cod;
next->x = x;
//urm->c = c;
next->urm = NULL;
ultim->urm = next;
ultim = next;
}
}
void BF()
{
lista w;
int c,x,y;
while (que!=NULL)
{
w = A[que->x];
c = C[que->x];
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()
{
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;
}
add(1);
BF();
for (i=2;i<=n;i++) fprintf(out,"%d ",C[i]);
}