Pagini recente » Cod sursa (job #1787733) | Cod sursa (job #1906602) | Cod sursa (job #2203983) | Cod sursa (job #1212745) | Cod sursa (job #2350208)
#include <iostream>
#include <vector>
#include <cstring>
#include <set>
#include <fstream>
#define dmax 50005
#define INF 10000009
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
int n, m, x, y, w;
vector<pair<int, int> > graf[dmax];
int dist[dmax];
bool viz[dmax];
void Dijkstra() {
dist[1] = 0;
//viz[1] = true;
multiset<pair<int, int> > ms;
pair<int, int> pVerif;
int newV, cost, cur_x, cur_c;
ms.insert({0, 1});
while (!ms.empty()) {
pVerif = *ms.begin();
ms.erase(ms.begin());
cur_x = pVerif.second;
cur_c = pVerif.first;
if (viz[cur_x]) continue;
viz[cur_x] = true;
//cout << "hi";
for (int i = 0; i < graf[cur_x].size(); i++) {
newV = graf[cur_x][i].first;
cost = graf[cur_x][i].second;
if (dist[cur_x] + cost < dist[newV]) {
dist[newV] = dist[cur_x] + cost;
ms.insert({dist[newV], newV});
}
}
}
}
int main()
{
int i, j;
fin >> n >> m;
for (i = 1; i <= m; i++) {
fin >> x >> y >> w;
graf[x].push_back({y, w});
graf[y].push_back({x, w});
}
for (i = 1; i <= n; i++) dist[i] = INF;
Dijkstra();
for (i = 2; i <= n; i++) {
fout << dist[i] << ' ';
}
return 0;
}