Pagini recente » Cod sursa (job #537604) | Cod sursa (job #265101) | Cod sursa (job #1309978) | Cod sursa (job #669695) | Cod sursa (job #306182)
Cod sursa(job #306182)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define VMAX 51000
#define EMAX 260000
#define INF 2000000000
int start[EMAX],target[EMAX],prev[EMAX],cost[EMAX],list[EMAX],last[VMAX],dist[VMAX];
char viz[VMAX];
int edgeNo,nodeNo,push,pop;
int main()
{
int i,x,y,c;
freopen("dijkstra.in","r",stdin);
freopen("dijkstra.out","w",stdout);
scanf("%d %d",&nodeNo,&edgeNo);
for (i=0;i<nodeNo;i++) last[i]=-1;
for (i=0;i<edgeNo;i++)
{
scanf("%d %d %d",&x,&y,&c);x--;y--;
start[i]=x;target[i]=y;prev[i]=last[x];last[x]=i;
cost[i]=c;
}
for (i=1;i<nodeNo;i++) dist[i]=INF;
pop=1;push=1;
list[push]=0;viz[0]=0;
while (pop<=push)
{
x=list[pop];
for (i=last[x];i!=-1;i=prev[i])
if (dist[x]+cost[i]<dist[target[i]])
if (!viz[target[i]])
{
viz[target[i]]=1;
dist[target[i]]=dist[x]+cost[i];
list[++push]=target[i];
}
else
{
dist[target[i]]=dist[x]+cost[i];
}
viz[x]=0;
pop++;
}
for (i=1;i<nodeNo;i++)
if (dist[i]==INF) printf("0 ");
else printf("%d ",dist[i]);
fclose(stdout);
return 0;
}