Pagini recente » Cod sursa (job #674807) | Cod sursa (job #1449466) | Cod sursa (job #786575) | Cod sursa (job #3225192) | Cod sursa (job #2867196)
#include <bits/stdc++.h>
#define INF 0x3f3f3fLL
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
struct Neighbour {
int node, cost;
friend bool operator > (const Neighbour & a, const Neighbour & b) {
return a.cost > b.cost;
}
};
vector <vector <Neighbour>> G;
vector <int> C;
int n, m;
void Dijkstra(int start) {
vector <bool> used(n + 1, false);
C = vector <int> (n + 1, INF);
priority_queue <Neighbour, vector <Neighbour>, greater <Neighbour>> pq;
pq.push({start, 0});
C[start] = 0;
while(!pq.empty()) {
int Node = pq.top().node;
pq.pop();
if(used[Node]) continue;
used[Node] = true;
for(auto x : G[Node])
if(C[x.node] > C[Node] + x.cost) {
C[x.node] = C[Node] + x.cost;
pq.push({x.node, C[x.node]});
}
}
// class Compar {
// public:
// bool operator () (int a, int b) {
// return C[a] > C[b];
// }
// };
// priority_queue <int, vector <int>, Compar> H;
// H.push(start);
// C[start] = 0;
// for(int i = 1; i <= n; i ++) {
// int vfmin;
// bool gasit = 0;
// while(!H.empty()) {
// vfmin = H.top(); H.pop();
// if(!used[vfmin]) {
// gasit = 1;
// break;
// }
// }
// if(gasit == 0) break;
// used[vfmin] = true;
// int min = C[vfmin];
// for(auto x : G[vfmin])
// if(!used[x.node] && C[x.node] > min + x.cost) {
// C[x.node] = min + x.cost;
// H.push(x.node);
// }
// }
}
int main() {
fin >> n >> m;
G.resize(n + 1);
for(int i = 1; i <= m; i ++) {
int x, y, c; fin >> x >> y >> c;
G[x].push_back({y, c});
}
Dijkstra(1);
for(int i = 2; i <= n; i ++)
fout << (C[i] != INF ? C[i] : 0) << ' ';
return 0;
}