Pagini recente » Cod sursa (job #2809031) | Cod sursa (job #2266783) | Cod sursa (job #2615267) | Autentificare | Cod sursa (job #3215661)
#include <fstream>
#include <queue>
#include <cstring>
using namespace std;
const int nmax = 50000;
const int INF = (1<<30);
struct dijkstra {
int node, cost;
bool operator<(const dijkstra &other)const{
return other.cost < cost;
}
};
vector <dijkstra> graf[nmax + 1];
priority_queue <dijkstra> pq;
int dist[nmax + 1];
int n;
bool viz[nmax + 1];
void drumin (){
for (int i = 2; i <= n; i++){
dist[i] = INF;
}
pq.push({1, 0});
while (!pq.empty()){
int curnode = pq.top().node;
int curdist = pq.top().cost;
pq.pop();
if (viz[curnode])
continue;
viz[curnode] = true;
for (auto neighbour: graf[curnode]){
if (dist[neighbour.node] > curdist + neighbour.cost){
dist[neighbour.node] = curdist + neighbour.cost;
pq.push({neighbour.node, curdist + neighbour.cost});
}
}
}
}
int main(){
ifstream fin ("dijkstra.in");
ofstream fout ("dijkstra.out");
int m;
fin >> n >> m;
for (int i = 0; i < m; i++){
int a, b, c;
fin >> a >> b >> c;
graf[a].push_back({b, c});
}
drumin();
for (int i = 2; i <= n; i++){
if (dist[i] == INF)
dist[i] = 0;
fout << dist[i] << " ";
}
return 0;
}