Pagini recente » Cod sursa (job #2472006) | Cod sursa (job #1020354) | Cod sursa (job #1477920) | Cod sursa (job #788917) | Cod sursa (job #2695015)
#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;
deque <pct> q;
pct aux;
aux.nod=1;
aux.cost=0;
q.push_front(aux);
while(!q.empty())
{
while(!q.empty() && sel[q.front().nod])
{
q.pop_front();
}
if(q.empty())
{
break;
}
int x=q.front().nod;
sel[x]=true;
q.pop_front();
for(auto m:a[x])
{
int y=m.nod;
int c=m.cost;
if(d[x]+c<d[y])
{
d[y]=d[x]+c;
pred[y]=x;
pct aux;
aux.nod=y;
aux.cost=-d[y];
q.push_front(aux);
}
}
}
for(int i=2;i<=n;i++)
{
if(d[i]==INFINIT)
g<<0<<" ";
else
g<<d[i]<<" ";
}
return 0;
}