Pagini recente » Cod sursa (job #2412032) | Cod sursa (job #1101350) | Cod sursa (job #6118) | Cod sursa (job #2512932) | Cod sursa (job #2495542)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
#define cin fin
#define cout fout
/*
*/
const int NMAX=5e4+10;
const int oo=1e9;
int n, m, x, y, c;
vector<pair<int, int>>rel[NMAX];
set<pair<int, int>>sord;
int d[NMAX];
int t[NMAX];
void read()
{
cin>>n>>m;
for(int i=1; i<=m; i++)
{
cin>>x>>y>>c;
rel[x].push_back({y, c});
}
}
void solve()
{
for(int i=2; i<=n; i++)
d[i]=oo;
sord.insert({0, 1});
while(!sord.empty())
{
int nod=sord.begin()->second;
int dist=sord.begin()->first;
sord.erase(sord.begin());
for(auto el:rel[nod])
{
int to=el.first;
int cost=el.second;
if(d[to]>d[nod]+cost)
{
if(d[to]!=oo)
sord.erase({d[to], to});
t[to]=nod;
d[to]=d[nod]+cost;
sord.insert({d[to], to});
}
}
}
for(int i=2; i<=n; i++)
{
if(d[i]==oo)
d[i]=0;
cout<<d[i]<<" ";
}
}
int main()
{
read();
solve();
return 0;
}