Pagini recente » Cod sursa (job #1271887) | Cod sursa (job #451737) | Cod sursa (job #2072826) | Cod sursa (job #2492945) | Cod sursa (job #3256840)
#include <algorithm>
#include <fstream>
#include <utility>
#include <vector>
#include <bitset>
#include <queue>
using namespace std;
const int nmax = 50050;
const int inf = 0x3f3f3f;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
int n, m, x, y, c;
vector<pair<int, int> > g[nmax+1];
vector<int> dist(nmax + 1, inf);
struct cmp {
inline bool operator()(const int i, const int j) {
return dist[i] > dist[j];
}
};
void dijkstra(int source) {
priority_queue<int, vector<int>, cmp> q;
bitset<nmax> visited;
dist[source] = 0;
q.push(source);
while(!q.empty()) {
int node = q.top();
q.pop();
visited[node] = false;
for(auto it = g[node].begin(); it < g[node].end(); ++it) {
if(dist[node] + (*it).second < dist[(*it).first]) {
dist[(*it).first] = dist[node] + (*it).second;
if(!visited[(*it).first]) {
q.push((*it).first);
visited[(*it).first] = true;
}
}
}
}
}
int main() {
fin >> n >> m;
while (fin >> x >> y >> c) {
g[x].push_back({y, c});
}
dijkstra(1);
for (int i = 2; i <= n; i++) {
if(dist[i] == inf)
fout << 0 << ' ';
else
fout << dist[i] << ' ';
}
return 0;
}