Pagini recente » Cod sursa (job #2258405) | Cod sursa (job #592680) | Cod sursa (job #693070) | Cod sursa (job #1727248) | Cod sursa (job #390672)
Cod sursa(job #390672)
#include <stdio.h>
#include <string.h>
#define max 100010
const int inf=0x3f3f3f3f;
struct lista
{
int nod,cost;
lista *next;
};
lista *g[max],*p;
int q[max],d[max],n,m,i,j,c,u,ultim,prim,w;
char inq[max];
void push(int i,int j,int c)
{
lista *p=new lista;
p->cost=c;
p->nod=j;
p->next=g[i];
g[i]=p;
}
int main()
{
freopen("dijkstra.in","r",stdin);
freopen("dijkstra.out","w",stdout);
scanf("%d%d",&n,&m);
for(; m--; )
{
scanf("%d%d%d",&i,&j,&c);
push(i,j,c);
}
memset(d,inf,sizeof(d));
d[1]=0; q[0]=1;
while(prim<=ultim)
{
u=q[prim++];
inq[u]=0;
for(p=g[u]; p!=NULL; p=p->next)
{
w=p->nod;
if(d[w]>d[u]+p->cost)
{
if(!inq[w])
{
inq[w]=1;
q[++ultim]=w;
}
d[w]=d[u]+p->cost;
}
}
}
for(i=2; i<=n; i++)
if(d[i]==inf) printf("0 ");
else printf("%d ",d[i]);
return 0;
}