Pagini recente » Monitorul de evaluare | Cod sursa (job #2434145) | Cod sursa (job #3342522) | Cod sursa (job #3322919) | Cod sursa (job #2566964)
#include <fstream>
#define inf 2000000000
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 *p=new nod;
p=L[j];
while(p!=0)
{
if(d[p->info]>d[j]+p->cost)
{
d[p->info]=d[j]+p->cost;
c[++u]=p->info;
}
p=p->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)fout<<d[i]<<" ";
return 0;
}