Pagini recente » Cod sursa (job #3248792) | Cod sursa (job #1002170) | Cod sursa (job #957590) | Cod sursa (job #2219306) | Cod sursa (job #1825780)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream fin ("dijkstra.in");
ofstream fout ("dijkstra.out");
const int maxn = 5e4 + 5;
const int oo = 1 << 29;
vector <pair <int,int> > G[maxn];
int Dist[maxn], n;
class Cmp {
public:
inline bool operator () (pair <int,int> &x, pair <int,int> &y) {
return x.second > y.second;
}
};
void Dijkstra(int source) {
priority_queue <int, vector < pair <int,int> >, Cmp> Heap;
vector < pair<int,int> > :: iterator it;
int i, node, cost;
for (i = 1; i <= n; i++) {
Dist[i] = oo;
}
Dist[source] = 0;
Heap.push(make_pair(source, Dist[source]));
while (!Heap.empty()) {
node = Heap.top().first;
cost = Heap.top().second;
Heap.pop();
if (Dist[node] != cost) {
continue;
}
for (it = G[node].begin(); it != G[node].end(); it++) {
if (Dist[it->first] > Dist[node] + it->second) {
Dist[it->first] = Dist[node] + it->second;
Heap.push(make_pair(it->first, Dist[it->first]));
}
}
}
}
int main() {
ios_base :: sync_with_stdio (false);
int m, i, x, y, c;
fin >> n >> m;
for (i = 1; i <= m; i++) {
fin >> x >> y >> c;
G[x].push_back(make_pair(y, c));
}
Dijkstra(1);
for (i = 2; i <= n; i++) {
if (Dist[i] == oo) {
fout << "0 ";
} else {
fout << Dist[i] << " ";
}
}
fin.close();
fout.close();
return 0;
}