Pagini recente » Cod sursa (job #1456632) | Cod sursa (job #1011134) | Cod sursa (job #2907014) | Cod sursa (job #3291692) | Cod sursa (job #3284415)
#include <bits/stdc++.h>
using namespace std;
const int NMAX = 5e4;
const int MMAX = 25e4;
const int INF = 2e9;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
struct edge {
int to, cost;
};
struct cell {
int node;
int dist;
};
struct cmp {
bool operator()(const cell a, const cell b) const {
return a.dist > b.dist;
}
};
vector<edge> graph[NMAX];
int n;
int mindist[NMAX];
bool vis[NMAX];
priority_queue<cell, vector<cell>, cmp> pq;
void dijkstra(int node) {
for(int i = 0; i < n; i++)
mindist[i] = INF;
mindist[node] = 0;
pq.push({0, node});
while(!pq.empty()) {
cell c = pq.top();
pq.pop();
if(!vis[c.node]) {
vis[c.node] = true;
for(auto x : graph[c.node]) {
if(c.dist + x.cost < mindist[x.to]) {
mindist[x.to] = c.dist + x.cost;
pq.push({x.to, mindist[x.to]});
}
}
}
}
}
int main() {
int m;
fin >> n >> m;
for(int i = 0; i < m; i++) {
int a, b, c;
fin >> a >> b >> c;
a--, b--;
graph[a].push_back({b, c});
}
dijkstra(0);
for(int i = 1; i < n; i++)
fout << (mindist[i] == INF ? 0 : mindist[i]) << ' ';
return 0;
}