Pagini recente » Cod sursa (job #1372006) | Cod sursa (job #1678681) | Cod sursa (job #1983117) | Cod sursa (job #1429702) | Cod sursa (job #3238618)
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream f("dijkstra.in");
ofstream g("dijkstra.out");
int n, m;
priority_queue < pair <int, int>, vector <pair <int, int> >, greater <pair <int, int> > > q;
void dijkstra()
{
}
int main()
{
f >> n >> m;
vector <pair <int, int> > a[n + 5];
vector <bool> viz(n + 5, 0);
vector <int> cost(n + 5, 1e9);
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();
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]) {
int val = vecin.second;
int next = vecin.first;
if (costu + val < cost[next]) {
cost[next] = val + costu;
q.push({cost[next], next});
}
}
}
for (int i = 2; i <= n; ++i) {
if (cost[i] == 1e9) {
cost[i] = 0;
}
g << cost[i] << ' ';
}
return 0;
}