Pagini recente » Cod sursa (job #1798386) | Cod sursa (job #2541584) | Cod sursa (job #2166563) | Cod sursa (job #1860265) | Cod sursa (job #233534)
Cod sursa(job #233534)
#include <stdio.h>
#define xv w->vf
#define yc w->c
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[xv]>c+yc || C[xv]==0) C[xv]=c+yc,add(xv);
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]);
}