Pagini recente » Cod sursa (job #552971) | Cod sursa (job #2725531) | Cod sursa (job #2036858) | Cod sursa (job #1572592) | Cod sursa (job #277737)
Cod sursa(job #277737)
#include <stdio.h>
int v[50005],d[50005];
long min,c;
struct nod{
long use,cost;
nod *adr;
} *q[50005],*in[50005];
void add(long aa,long bb,long cc)
{
nod *aux;
aux=new nod;
aux->use=bb;
aux->cost=cc;
aux->adr=NULL;
if (in[aa]==NULL)
{
q[aa]=aux;
in[aa]=q[aa];
}
else
{
q[aa]->adr=aux;
q[aa]=aux;
}
}
int main()
{
freopen("dijkstra.in","r",stdin);
freopen("dijkstra.out","w",stdout);
long n,m,i,j,a,b;
nod *aux;
scanf("%ld %ld",&n,&m);
for (i=1;i<=m;i++)
{
scanf("%ld%ld%ld",&a,&b,&c);
//x[a][b]=c;
add(a,b,c);
}
for (i=2;i<=n;i++)
d[i]=2000000000;
d[1]=0;
for (i=1;i<=n;i++)
{
min=2000000000;
for (j=1;j<=n;j++)
if (d[j]<min&&!v[j])
{
min=d[j];
c=j;
}
v[c]=1;
aux=in[c];
while (aux!=NULL)
{
if (d[c]+aux->cost<d[aux->use])
d[aux->use]=d[c]+aux->cost;
aux=aux->adr;
}
//while (
/*
for (j=1;j<=n;j++)
if (x[c][j]&&!v[j]&&d[c]+x[c][j]<d[j])
d[j]=d[c]+x[c][j];
*/
}
for (i=2;i<=n;i++)
printf("%d ",d[i]);
printf("\n");
return 0;
}