Pagini recente » Cod sursa (job #2876777) | Cod sursa (job #3165710) | Cod sursa (job #2974599) | Cod sursa (job #1927365) | Cod sursa (job #2695023)
#include <bits/stdc++.h>
using namespace std;
const int INFINIT=INT_MAX;
const int N=5e4+10;
ifstream f("dijkstra.in");
ofstream g("dijkstra.out");
struct pct{
int nod,cost;
};
vector <pct> a[N];
int n;
bool sel[N];
int pred[N];
int d[N];
int main()
{
int m;
f>>n>>m;
for(int i=1;i<=m;i++)
{
int x,y,z;
f>>x>>y>>z;
pct aux;
aux.cost=z;
aux.nod=y;
a[x].push_back(aux);
}
for(int j=2;j<=n;j++)
{
d[j]=INFINIT;
}
d[1]=0;
queue <pct> q;
pct aux;
aux.nod=1;
aux.cost=0;
q.push(aux);
while(!q.empty())
{
if(q.empty())
{
break;
}
int x=q.front().nod;
sel[x]=true;
q.pop();
for(int i=0;i<a[x].size();i++)
{
int y=a[x][i].nod;
int c=a[x][i].cost;
if(d[x]+c<d[y])
{
d[y]=d[x]+c;
pred[y]=x;
pct aux;
aux.nod=y;
aux.cost=(-1)*d[y];
q.push(aux);
}
}
}
for(int i=2;i<=n;i++)
{
if(d[i]==INFINIT)
g<<0<<" ";
else
g<<d[i]<<" ";
}
return 0;
}