Pagini recente » Cod sursa (job #2432142) | Cod sursa (job #2300694) | Cod sursa (job #2607018) | Cod sursa (job #2670335) | Cod sursa (job #1365468)
#include <iostream>
#include <fstream>
#include <queue>
#include <vector>
#include <string.h>
using namespace std;
const int maxn = 100005;
const int oo = 0x3f3f3f3f;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
int n, m, dist[maxn];
vector <pair<int, int> > g[maxn];
priority_queue <pair<int, int> > q;
inline void dijkstra() {
memset(dist, oo, sizeof(dist));
dist[1] = 0;
q.push(make_pair(0, 1));
while(!q.empty()) {
int node = q.top().second;
int cost = -q.top().first;
q.pop();
for(vector <pair<int, int> > :: iterator it = g[node].begin() ; it != g[node].end() ; ++ it)
if(dist[it->first] > dist[node] + it->second) {
dist[it->first] = dist[node] + it->second;
q.push(make_pair(-dist[it->first], it->first));
}
}
for(int i = 2 ; i <= n ; ++ i) {
if(dist[i] == oo)
dist[i] = 0;
fout << dist[i] << ' ';
}
}
int main() {
fin >> n >> m;
for(int i = 1 ; i <= m ; ++ i) {
int x, y, z;
fin >> x >> y >> z;
g[x].push_back(make_pair(y, z));
}
dijkstra();
}