Pagini recente » Cod sursa (job #3210204) | Cod sursa (job #2546179) | Cod sursa (job #2580420) | Cod sursa (job #2887182) | Cod sursa (job #2604341)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
const int VMAX = 50005;
const int Inf = 1 << 30;
int n, m;
vector<int> D(VMAX);
vector<bool> inq(VMAX);
vector<pair<int, int>> graf[VMAX];
struct compare
{
bool operator()(int i, int j){
return D[i] > D[j];}
};
priority_queue<int, vector<int>, compare> q;
void Dijkstra(int startnode)
{
for (int i = 1; i <= n; ++i) D[i] = Inf;
D[startnode] = 0;
q.push(startnode);
inq[startnode] = true;
while (!q.empty())
{
int currentNode = q.top();
q.pop();
inq[currentNode] = false;
for (size_t i = 0; i < graf[currentNode].size(); ++i)
{
if (D[currentNode] + graf[currentNode][i].second < D[graf[currentNode][i].first])
{
D[graf[currentNode][i].first] = D[currentNode] + graf[currentNode][i].second;
if (!inq[graf[currentNode][i].first])
{
q.push(graf[currentNode][i].first);
inq[graf[currentNode][i].first] = true;
}
}
}
}
}
int main()
{
ios_base::sync_with_stdio(false);
fin.tie(NULL), fout.tie(NULL);
int x, y, c;
fin >> n >> m;
for (int i = 1; i <= m; ++i)
{
fin >> x >> y >> c;
graf[x].push_back({y, c});
}
Dijkstra(1);
for (int i = 2; i <= n; ++i)
if (D[i] != Inf) fout << D[i] << " ";
else fout << "0 ";
fin.close(), fout.close();
return 0;
}