Pagini recente » Cod sursa (job #459611) | Cod sursa (job #1226081) | Cod sursa (job #2847817) | Cod sursa (job #564611) | Cod sursa (job #3218101)
#include <bits/stdc++.h>
#define pii pair<int,int>
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
const int nmax = 5e4, inf = 2e9;
vector<pair<int, int>> edge[nmax + 1];
vector<int> minn(nmax + 1, inf);
int n, m, x, y, c;
void djikstra() {
priority_queue<pii, vector<pii>, greater<pii>> pq;
pq.push({0, 1});
minn[1] = 0;
while(pq.empty() == 0) {
int cc = pq.top().first, cn = pq.top().second;
pq.pop();
if(minn[cn] < cc)
continue;
for(auto nxt : edge[cn]) {
int nc = cc + nxt.second, nn = nxt.first;
if(minn[nn] > nc) {
minn[nn] = nc;
pq.push({nc, nn});
}
}
}
}
int main() {
fin >> n >> m;
for(int i = 1; i <= m; i++) {
fin >> x >> y >> c;
edge[x].push_back({y, c});
}
djikstra();
for(int i = 2; i <= n; i++)
fout << ((minn[i] == inf) ? 0 : minn[i]) << " ";
return 0;
}