Pagini recente » Cod sursa (job #266528) | Cod sursa (job #3262874) | Cod sursa (job #2056404) | Cod sursa (job #3277084) | Cod sursa (job #3296255)
#include <iostream>
#include <vector>
#include <queue>
#define in fin
#define out fout
#define mkp make_pair
using namespace std;
using ll = long long;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
vector< pair<int, ll> > v[50001];
ll d[50001];
void dijkstra(int nod){
d[nod] = 0;
priority_queue< pair<ll, int>, vector< pair<ll, int> >, greater< pair<ll, int> > > pq;
pq.push( mkp(d[nod], nod) );
while(!pq.empty()){
if(d[ pq.top().second ] != pq.top().first){
pq.pop(); continue;
}
nod = pq.top().second; pq.pop();
//cout << "sunt la nod = " << nod << '\n';
for(const pair<int, ll> & x : v[nod]){
if(x.first == 1) continue;
if(d[x.first] == 0 || d[x.first] > d[nod] + x.second){
pq.push( mkp(d[nod] + x.second, x.first) );
d[x.first] = d[nod] + x.second;
}
}
}
}
int main()
{
int n, m; in >> n >> m;
for(int i = 0; i < m; i++){
ll a, b, c; in >> a >> b >> c;
v[a].push_back( mkp(b, c) );
}
dijkstra(1);
for(int i = 2; i <= n; i++) out << d[i] << " ";
out << '\n';
return 0;
}