Pagini recente » Cod sursa (job #2986679) | Cod sursa (job #3228520) | Diferente pentru implica-te/arhiva-educationala intre reviziile 107 si 223 | Cod sursa (job #3291719) | Cod sursa (job #2807566)
#include <bits/stdc++.h>
#define lsb(x) (x & (-x))
#define ll long long
#define ull unsigned long long
#define uint unsigned int
using namespace std;
class InputReader {
public:
// used for reading from stdin
InputReader(FILE* file) : file(file) {}
InputReader(const char* filename) {
file = fopen(filename, "r");
}
~InputReader() {
if (file != stdin) {
fclose(file);
}
}
InputReader& operator>>(int& number) {
number = read_number();
return *this;
}
private:
char get_char() {
if (pbuff == BUFF_SIZE) {
fread(buff, 1, BUFF_SIZE, file);
pbuff = 0;
}
return buff[pbuff++];
}
int read_number() {
char ch = get_char();
while (ch != '-' && !isdigit(ch)) {
ch = get_char();
}
int sign = 1;
if (ch == '-') {
sign = -1;
ch = get_char();
}
int number = 0;
while (isdigit(ch)) {
number = number * 10 + ch - '0';
ch = get_char();
}
return number * sign;
}
private:
FILE* file;
static constexpr int BUFF_SIZE = (1 << 17);
char buff[BUFF_SIZE];
int pbuff = BUFF_SIZE;
};
class OutputWriter {
public:
// used for writing to stdout
OutputWriter(FILE* file) : file(file) {}
OutputWriter(const char* filename) {
file = fopen(filename, "w");
}
~OutputWriter() {
flush();
if (file != stdout) {
fclose(file);
}
}
OutputWriter& operator<<(int number) {
auto str = std::to_string(number);
for (char ch : str) {
add_char(ch);
}
return *this;
}
OutputWriter& operator<<(const char* str) {
while (*str != 0) {
add_char(*str);
str++;
}
return *this;
}
void flush() {
fwrite(buff, sizeof(char), pbuff, file);
pbuff = 0;
}
private:
void add_char(char ch) {
if (pbuff == BUFF_SIZE) {
flush();
}
buff[pbuff++] = ch;
}
private:
FILE* file;
static constexpr int BUFF_SIZE = (1 << 17);
char buff[BUFF_SIZE];
int pbuff = 0;
};
const ll INF = 1e18;
int main() {
#ifdef HOME
ifstream cin("A.in");
ofstream cout("A.out");
#endif
InputReader cin("dijkstra.in");
OutputWriter cout("dijkstra.out");
int i, n, m;
// ios::sync_with_stdio(false);
// cin.tie(0), cout.tie(0);
cin >> n >> m;
vector <vector <pair <int, int>>> g(n + 1);
for(i = 1; i <= m; i++) {
int x, y, z; cin >> x >> y >> z;
g[x].push_back({y, z});
}
vector <ll> dst(n + 1, INF);
vector <bool> vis(n + 1);
priority_queue <pair <ll, int>> pq;
pq.push({0, 1}), dst[1] = 0;
while(pq.size()) {
auto cur = pq.top();
pq.pop(), cur.first *= -1;
if(vis[cur.second]) continue;
dst[cur.second] = cur.first;
vis[cur.second] = 1;
for(auto it : g[cur.second]) {
if(dst[it.first] > cur.first + it.second) {
dst[it.first] = cur.first + it.second;
pq.push({-dst[it.first], it.first});
}
}
}
for(i = 2; i <= n; i++) {
if(dst[i] == INF) dst[i] = 0;
cout << dst[i] << " ";
}
return 0;
}