Pagini recente » Cod sursa (job #2255423) | Cod sursa (job #433346) | Cod sursa (job #586088) | Cod sursa (job #3158132) | Cod sursa (job #2796986)
#include <fstream>
#include <queue>
#include <vector>
#define NMAX 50000
using namespace std;
ifstream cin ("dijkstra.in");
ofstream cout ("dijkstra.out");
struct hatz {
int node;
int cost;
bool operator < (const hatz &A) const {
return cost > A.cost;
}
};
int vdist[NMAX + 1];
priority_queue <hatz> pq;
vector <hatz> vnext[NMAX + 1];
void bfs() {
int i, distcur, newnode, nodecur, newcost, n;
pq.push({1, 0});
while (!pq.empty()) {
nodecur = pq.top().node;
distcur = pq.top().cost;
pq.pop();
if (vdist[nodecur] == -1) {
vdist[nodecur] = distcur;
n = vnext[nodecur].size();
for (i = 0; i < n; i++) {
newnode = vnext[nodecur][i].node;
newcost = vnext[nodecur][i].cost;
if (vdist[newnode] == -1)
pq.push({newnode, distcur + newcost});
}
}
}
}
void init_vdist(int n) {
int i;
for (i = 1; i <= n; i++)
vdist[i] = -1;
}
int main() {
int n, m, i, a, b, c;
cin >> n >> m;
init_vdist(n);
for (i = 0; i < m; i++) {
cin >> a >> b >> c;
vnext[a].push_back({b, c});
}
bfs();
for (i = 2; i <= n; i++)
if (vdist[i] > -1)
cout << vdist[i] << " ";
else
cout << "0 ";
return 0;
}