Pagini recente » Cod sursa (job #1058162) | Cod sursa (job #1790139) | Cod sursa (job #1583517) | Cod sursa (job #1963129) | Cod sursa (job #1976925)
#include <bits/stdc++.h>
using namespace std;
#define NMAX 50002
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
vector< pair<int, int> > graf[NMAX];
int dist[NMAX];
void Dijkstra(const int N, const int start) {
priority_queue< pair<int, int> > Heap;
for (int i = 1; i <= N; ++i)
dist[i] = INT_MAX;
dist[start] = 0;
Heap.push(make_pair(dist[start], start));
while (!Heap.empty()) {
int nod = Heap.top().second;
Heap.pop();
for (auto adj: graf[nod]) {
if (dist[adj.first] > dist[nod] + adj.second) {
dist[adj.first] = dist[nod] + adj.second;
Heap.push(make_pair(dist[adj.first], adj.first));
}
}
}
}
string Buffer;
string :: iterator it;
inline int readInt() {
int nr = 0;
while (*it < '0' || *it > '9')
it++;
while (*it >= '0' && *it <= '9') {
nr = nr * 10 + (*it - '0');
it++;
}
return nr;
}
int main() {
int N, M;
getline(fin, Buffer, (char)0);
it = Buffer.begin();
//fin >> N >> M;
N = readInt();
M = readInt();
int x, y, c;
while (M--) {
//fin >> x >> y >> c;
x = readInt();
y = readInt();
c = readInt();
graf[x].push_back(make_pair(y, c));
}
Dijkstra(N, 1);
for (int i = 2; i <= N; ++i)
fout << (dist[i] == INT_MAX ? 0 : dist[i]) << " ";
return 0;
}