Pagini recente » Cod sursa (job #2777302) | Cod sursa (job #2685968) | Razvy | Cod sursa (job #587413) | Cod sursa (job #2159546)
#include <iostream>
#include <algorithm>
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
vector <pair <int, int >> v[50001];
pair <int, int > x;
queue <int > q;
int cost[50001], a, b, inf=999999999, c, n, m, aux;
int main()
{
fin>>n>>m;
for(int i=1;i<=m;i++)
{
fin>>a>>b>>c;
v[a].push_back(make_pair(b,c));
}
for(int i=2;i<=n;i++)
cost[i]=inf;
q.push(1);
while(!q.empty())
{
aux=q.front();
q.pop();
for(int i=0;i<v[aux].size();i++)
{
x=v[aux][i];
if(cost[x.first]>cost[aux]+x.second)
{
cost[x.first]=cost[aux]+x.second;
q.push(x.first);
}
}
}
for(int i=2;i<=n;i++)
{
if(cost[i]==inf)
fout<<0<<' ';
else
fout<<cost[i]<<" ";
}
return 0;
}