Pagini recente » Cod sursa (job #1852827) | Cod sursa (job #1863760) | Cod sursa (job #2599090) | Cod sursa (job #1485500) | Cod sursa (job #2392911)
#include <algorithm>
#include <fstream>
#include <cstring>
#include <cassert>
#include <vector>
#include <queue>
using namespace std;
const int NMAX = 50005;
const int INF = 0x3f3f3f3f;
vector<pair<int, int> > G[NMAX];
int dist[NMAX];
bool inQueue[NMAX];
int main() {
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
int n, m;
fin >> n >> m;
assert(1 <= n && n <= 50000);
assert(1 <= m && m <= 250000);
for (int i = 0; i < m; ++i) {
int from, to, cost;
fin >> from >> to >> cost;
assert(1 <= from && from <= n);
assert(1 <= to && to <= n);
assert(0 <= cost && cost <= 20000);
G[from].push_back(make_pair(to, cost));
}
memset(dist, INF, sizeof dist);
dist[1] = 0;
queue<int> q;
q.push(1);
while (!q.empty()) {
int node = q.front();
inQueue[node] = false;
q.pop();
for (vector<pair<int, int> >::iterator it = G[node].begin(); it != G[node].end(); ++it) {
int to = it->first;
int cost = it->second;
if (dist[to] > dist[node] + cost) {
dist[to] = dist[node] + cost;
if (!inQueue[to]) {
inQueue[to] = true;
q.push(to);
}
}
}
}
for (int i = 2; i <= n; ++i) {
if (dist[i] == INF) {
dist[i] = 0;
}
fout << dist[i] << ' ';
}
fout << '\n';
fin.close();
fout.close();
}