Pagini recente » Cod sursa (job #2849841) | Cod sursa (job #2292144) | Cod sursa (job #661133) | Cod sursa (job #2134405) | Cod sursa (job #2793530)
#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
using namespace std;
ifstream fin ("dijkstra.in");
ofstream fout ("dijkstra.out");
struct varf {
int nod, cost;
bool operator < (const varf & b) const {
return cost > b.cost;
}
};
vector <varf> g[50005];
int v[50005];
priority_queue<varf> q;
void dijkstra(int s)
{
q.push({s, 0});
while (!q.empty()) {
int currentNode = q.top().nod;
int currentCost = q.top().cost;
q.pop();
if (v[currentNode] != currentCost) continue;
for (auto i : g[currentNode]) {
int cost = currentCost + i.cost;
if (v[i.nod] > cost) {
v[i.nod] = cost;
q.push({i.nod, cost});
}
}
}
}
int main()
{
int n, m;
fin >> n >> m;
for (int i = 1; i <= m; i ++) {
int a, b, c;
fin >> a >> b >> c;
g[a].push_back({b, c});
}
memset(v, INF, sizeof(v));
dijkstra(1);
for (int i = 2; i <= n; i ++) {
if (v[i] >= INF) {
fout << 0 << " ";
}
else {
fout << v[i] << " ";
}
}
return 0;
}