Pagini recente » Cod sursa (job #2630122) | Cod sursa (job #2347343) | Borderou de evaluare (job #385102) | Cod sursa (job #1658201) | Cod sursa (job #2136955)
#include <fstream>
#include <vector>
#include <utility>
#include <queue>
#include <iostream>
#define nmax 50005
#define INF (1 << 30)
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
vector <pair <int, int> > v[nmax];
vector <int> cost (nmax, INF);
struct comp {
bool operator() (const pair <int, int> &a, const pair <int, int> &b) {
return a.second > b.second;
}
};
priority_queue <pair <int, int>, vector <pair <int, int> >, comp> q;
int main()
{
int n, m, x, y, c;
fin >> n >> m;
for (; m; m--) {
fin >> x >> y >> c;
v[x].push_back(make_pair(y, c));
}
q.push(make_pair(1, 0));
cost[1] = 0;
while (!q.empty()) {
x = q.top().first;
c = q.top().second;
q.pop();
if (cost[x] != c) continue;
for (auto it: v[x]) {
if (c + it.second < cost[it.first]) {
cost[it.first] = cost[x] + it.second;
q.push(make_pair(it.first, cost[it.first]));
}
}
}
for (int i = 2; i <= n; ++i)
if (cost[i] == INF) fout << 0 << " ";
else fout << cost[i] << " ";
return 0;
}