Pagini recente » Cod sursa (job #2125559) | Cod sursa (job #723423) | Cod sursa (job #901407) | Cod sursa (job #1356662) | Cod sursa (job #3275903)
#include <fstream>
#include <iostream>
#include <queue>
#include <vector>
#include <algorithm>
#define inf 1e18
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
typedef long long ll;
typedef pair<int , int> pii;
int const NM = 1e5 + 3 , MM = 25e4 + 3;
int X[MM] , Y[MM] , W[MM];
vector<int> g[NM];
ll dp[NM];
int N , M;
int oth(int x , int e) {
return x ^ X[e] ^ Y[e];
}
int main() {
fin >> N >> M;
for (int i = 1 ; i <= M ; ++i) {
fin >> X[i] >> Y[i] >> W[i];
g[X[i]].push_back(i);
// g[Y[i]].push_back(i); *
}
fill(dp , dp + 1 + N , inf);
priority_queue<pii , vector<pii> > h;
dp[1] = 0;
h.emplace(0 , 1);
while (!h.empty()) {
auto [w , x] = h.top();
h.pop();
if (-w != dp[x]) {
continue;
}
for (int e : g[x]) {
int y = oth(x , e);
if (dp[y] > dp[x] + W[e]) {
dp[y] = dp[x] + W[e];
h.emplace(-dp[y] , y);
}
}
}
for (int i = 2 ; i <= N ; ++ i) {
fout << (dp[i] == inf ? 0 : dp[i]) << ' '; // *
}
fout << '\n';
return 0;
}