Pagini recente » Cod sursa (job #207928) | Cod sursa (job #1717548) | Cod sursa (job #1589744) | Cod sursa (job #1273724) | Cod sursa (job #2233844)
#include <fstream>
#include <vector>
#include <queue>
#define NMAX 50005
#define MMAX 250005
#define pb push_back
#define INF 1e9
using namespace std;
ifstream f("dijkstra.in");
ofstream g("dijkstra.out");
struct Node{
int y, cost;
Node(const int y, const int cost): y{y}, cost{cost} {}
bool operator<(const Node ot) const{
return this->cost > ot.cost;
}
};
vector<Node> G[NMAX];
priority_queue<Node> pq;
int dist[NMAX];
int n, m;
void read_data(){
f >> n >> m;
for(int i = 0; i<m; i++){
int x, y, c;
f >> x >> y >> c;
G[x].pb({y, c});
}
}
void dijkstra(int node){
for(int i = 0; i<=n; i++){
dist[i] = INF;
}
dist[node] = 0;
pq.push({node, dist[node]});
while(!pq.empty()){
auto node = pq.top();
pq.pop();
if(dist[node.y] != node.cost){
continue;
}
int nod = node.y;
for(int i = 0; i<G[nod].size(); i++){
auto new_node = G[nod][i];
if(dist[nod] + new_node.cost < dist[new_node.y]){
dist[new_node.y] = dist[nod] + new_node.cost;
pq.push({new_node.y, dist[new_node.y]});
}
}
}
}
int main(){
read_data();
dijkstra(1);
for(int i = 2; i<=n; i++){
dist[i] == INF ? g << 0 << ' ' : g << dist[i] << ' ';
}
return 0;
}