Pagini recente » Cod sursa (job #817699) | Cod sursa (job #1401108) | Cod sursa (job #1734154) | Cod sursa (job #1422337) | Cod sursa (job #3238588)
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream f("dijkstra.in");
ofstream g("dijkstra.out");
priority_queue <pair <int, int> , vector <pair <int, int> >, greater < pair <int, int> > > q;
vector < vector < pair <int, int > > > a;
int n, m, cost[50005];
bool viz[50005];
void dijkstra()
{
for (int i = 1; i <= n; ++i) {
cost[i] = (1 << 30);
}
cost[1] = 0;
q.push({0, 1});
while (!q.empty()) {
int costu = q.top().first;
int nod = q.top().second;
q.pop();
if (viz[nod]) {
continue;
}
viz[nod] = 1;
for (auto vecin : a[nod]) {
if (costu + vecin.second < cost[vecin.first]) {
cost[vecin.first] = costu + vecin.second;
q.push({costu + vecin.second, vecin.first});
}
}
}
}
int main()
{
f >> n >> m;
a.resize(n + 5);
for (int i = 1; i <= m; ++i) {
int x, y, z;
f >> x >> y >> z;
a[x].push_back({y, z});
a[y].push_back({x, z});
}
dijkstra();
for (int i = 2; i <= n; ++i) {
if (cost[i] == (1 << 30)) {
cost[i] = 0;
}
g << cost[i] << ' ';
}
return 0;
}