Pagini recente » Cod sursa (job #592447) | Cod sursa (job #869270) | Cod sursa (job #3238572) | Cod sursa (job #2033272) | Cod sursa (job #1591096)
#include <fstream>
#include <vector>
#include <queue>
#define INF 2147483647
using namespace std;
ifstream cin ("dijkstra.in");
ofstream cout ("dijkstra.out");
class node_distance {
public:
int node, dist;
};
class cmp {
public:
inline bool operator () (const node_distance a, const node_distance b) {
return a.dist > b.dist;
}
};
priority_queue <node_distance, vector <node_distance>, cmp> prio_q;
vector <node_distance> G[50005];
int vert_nr, edge_nr, length[50005];;
bool used[50005];
void read() {
cin >> vert_nr >> edge_nr;
for(int i = 1; i <= edge_nr; ++i) {
int a, b, lng;
cin >> a >> b >> lng;
G[a].push_back( {b, lng} );
}
}
void dijkstra(int node) {
for(int i = 1; i <= vert_nr; ++i) {
length[i] = INF;
}
length[node] = 0;
prio_q.push( {node, 0} );
while(!prio_q.empty() ) {
int frn = prio_q.top().node;
prio_q.pop();
if(used[frn]) {
continue;
}
used[frn] = true;
for(auto it: G[frn]) {
if(length[frn] + it.dist < length[it.node] ) {
length[it.node] = length[frn] + it.dist;
prio_q.push( {it.node, length[it.node] } );
}
}
}
}
void solve() {
dijkstra(1);
}
void print() {
for(int i = 2; i <= vert_nr; ++i) {
if(length[i] == INF) {
cout << 0 << ' ';
continue;
}
cout << length[i] << ' ';
}
cout << '\n';
}
int main() {
read();
solve();
print();
return 0;
}