Pagini recente » Cod sursa (job #1376894) | Cod sursa (job #2184762) | Cod sursa (job #3166616) | Cod sursa (job #1473761) | Cod sursa (job #2198363)
#include <algorithm>
#include <fstream>
#include <cstring>
#include <vector>
#include <queue>
using namespace std;
const int NMAX = 50005;
const int INF = 0xffffffff;
vector< pair<int, int> > G[NMAX];
int dist[NMAX];
bool inQueue[NMAX];
int n, m;
ifstream in("dijkstra.in");
ofstream out("dijkstra.out");
int main() {
in >> n >> m;
for (int i = 0; i < m; ++i) {
int from, to, cost;
in >> from >> to >> cost;
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;
}
out << dist[i] << ' ';
}
out << '\n';
return 1;
}