Pagini recente » Cod sursa (job #2628562) | Cod sursa (job #2568336) | Cod sursa (job #3179986) | Cod sursa (job #395503) | Cod sursa (job #2350657)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <fstream>
#define dmax 50005
#define INF 99999999
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
int dist[dmax];
int pVerif, newV;
struct Compare{
bool operator() (int e1, int e2) {
return dist[e1] > dist[e2];
}
};
int n, m, x, y, c;
vector<pair<int, int> > graf[dmax];
priority_queue<int, vector<int>, Compare> pq;
bool viz[dmax];
int main() {
int i, j;
cin >> n >> m;
for (i = 1; i <= m; i++) {
cin >> x >> y >> c;
graf[x].push_back({y, c});
graf[y].push_back({x, c});
}
for (i = 1; i <= n; i++) dist[i] = INF;
dist[1] = 0;
viz[1] = true;
pq.push(1);
while (!pq.empty()) {
pVerif = pq.top();
pq.pop();
viz[pVerif] = false;
for (i = 0; i < graf[pVerif].size(); i++) {
newV = graf[pVerif][i].first;
if (dist[newV] > dist[pVerif] + graf[pVerif][i].second) {
dist[newV] = dist[pVerif] + graf[pVerif][i].second;
if (!viz[newV]) {
pq.push(newV);
viz[newV] = true;
}
}
}
}
for (i = 2; i <= n; i++) {
if (dist[i] == INF) cout << "0 ";
else cout << dist[i] << ' ';
}
return 0;
}