Pagini recente » Cod sursa (job #3342544) | Cod sursa (job #3322920) | Cod sursa (job #2853756) | Cod sursa (job #2205441) | Cod sursa (job #2567017)
#include <fstream>
#define inf 1000000000
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
struct nod{int info,cost; nod *urm;};
nod *L[50010];
int n,m,x,y,cc,d[50010],c[250010];
void adauga(int x,int y,int cc)
{
nod *p=new nod;
p->info=y;
p->cost=cc;
p->urm=L[x];
L[x]=p;
}
void bellf(int nodx)
{
for(int i=1;i<=n;i++)d[i]=inf;
d[nodx]=0;
int p=1,u=1;
c[u]=nodx;
while(p<=u)
{
int j=c[p];
p++;
nod *h=new nod;
h=L[j];
while(h!=0)
{
if(d[h->info]>d[j]+h->cost)
{
d[h->info]=d[j]+h->cost;
c[++u]=h->info;
}
h=h->urm;
}
}
}
int main()
{
fin>>n>>m;
for(int i=1;i<=m;i++)
{
fin>>x>>y>>cc;
adauga(x,y,cc);
}
bellf(1);
for(int i=1;i<=n;i++)
{
if(i!=1)
{
if(d[i]==inf)fout<<0<<" ";
else fout<<d[i]<<" ";
}
}
return 0;
}