Pagini recente » Cod sursa (job #3331168) | Cod sursa (job #3327750) | Cod sursa (job #1501405) | Cod sursa (job #1262620) | Cod sursa (job #3325462)
#include<iostream>
#include<vector>
#include<queue>
#include<fstream>
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
vector<vector<pair<int,int>>> G;
const int INF = 1000000005;
vector<long long> distanta(50001, INF);
vector<int> fol(50001, 0);
int n, m, x, y, w;
int main()
{
fin >> n >> m;
G.resize(n + 1);
for (int i = 0; i < m; i++)
{
fin >> x >> y >> w;
G[x].push_back({y, w});
}
int start = 1;
distanta[start] = 0;
priority_queue<pair<long long,int>> pq;
pq.push({-distanta[start], start});
while (!pq.empty())
{
int nod = pq.top().second;
pq.pop();
if (fol[nod]) continue;
fol[nod] = 1;
for (auto it : G[nod])
{
int nxt = it.first;
int cost = it.second;
if (distanta[nod] + cost < distanta[nxt])
{
distanta[nxt] = distanta[nod] + cost;
pq.push({-distanta[nxt], nxt});
}
}
}
for (int i = 2; i <= n; i++)
{
if (distanta[i] == INF) fout << 0 << " ";
else fout << distanta[i] << " ";
}
return 0;
}