Pagini recente » Cod sursa (job #3003495) | Cod sursa (job #3151210) | Cod sursa (job #621708) | Cod sursa (job #2459133) | Cod sursa (job #1906877)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream in("dijkstra.in");
ofstream out("dijkstra.out");
typedef pair<int, int> edge;
int n, m, d[50001];
bool viz[50001];
vector<edge> g[50001];
priority_queue<edge, vector<edge>, greater<edge> > q;
const int INF = 2e9 + 1;
void citire();
int main()
{
citire();
q.push(make_pair(0, 1));
while (!q.empty()) {
int x = q.top().second;
int cost = q.top().first;
q.pop();
if (viz[x]) continue;
viz[x] = true;
for (int i = 0; i < g[x].size(); ++i) {
int y = g[x][i].second;
//if (!viz[y]) {
if (cost + g[x][i].first < d[y]) {
d[y] = cost + g[x][i].first;
q.push(make_pair(d[y], y));
}
//}
}
}
for (int i = 2; i <= n; ++i) {
if (d[i] == INF) {
out << 0 << " ";
} else {
out << d[i] << " ";
}
}
return 0;
}
void citire() {
in >> n >> m;
for (int i = 1, x, y, cost; i <= m; ++i) {
in >> x >> y >> cost;
g[x].push_back(make_pair(cost, y));
}
for (int i = 1; i <= n; ++i)
d[i] = INF;
in.close();
}