Pagini recente » Cod sursa (job #2451787) | Cod sursa (job #2343531) | Cod sursa (job #826536) | Cod sursa (job #474751) | Cod sursa (job #2572387)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
int n, m;
int d[50001];
vector<pair<int, int>>G[50001];
priority_queue<pair<int, int>>PQ;
int main()
{
fin >> n >> m;
for(int i = 1; i <= m; ++i)
{
int x, y, c;
fin >> x >> y >> c;
G[x].push_back(make_pair(y, c));
}
for(int i = 1; i <= n; ++i) d[i] = INT_MAX;
d[1] = 0;
PQ.push({0, 1});
while(!PQ.empty())
{
pair<int, int> nod = PQ.top();
PQ.pop();
for(auto it: G[nod.second])
{
if(d[it.first] > it.second + nod.first)
{
d[it.first] = it.second + nod.first;
PQ.push({d[it.first], it.first});
}
}
}
for(int i = 2; i <= n; ++i) fout << d[i] << " ";
fout << "\n";
return 0;
}