Pagini recente » Cod sursa (job #1577830) | Cod sursa (job #52471) | Cod sursa (job #406954) | Profil andrici_cezar | Cod sursa (job #1339949)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <cstdlib>
#include <cstring>
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
typedef pair<int, int> cuplu;
const int MAXN = 50005;
const int INF = 0x3f3f3f3f;
vector<cuplu> G[MAXN];
int N, M;
int best[MAXN];
struct cmp {
bool operator()(cuplu A, cuplu B) {
return A.second > B.second;
}
};
priority_queue<cuplu, vector<cuplu>, cmp> PQ;
void Read() {
fin >> N >> M;
int a, b, c;
for (int i = 0; i < M; ++i) {
fin >> a >> b >> c;
G[a].push_back( make_pair(b, c) );
}
}
void Dijkstra() {
PQ.push( make_pair(1, 0) );
memset(best, INF, sizeof(best));
best[1] = 0;
do {
int node = PQ.top().first;
PQ.pop();
vector<cuplu>:: iterator it;
for (it = G[node].begin(); it != G[node].end(); ++it) {
if (best[it->first] > best[node] + it->second) {
best[it->first] = best[node] + it->second;
PQ.push( make_pair(it->first, best[it->first]) );
}
}
}while (!PQ.empty());
}
void Write() {
for (int i = 2; i <= N; ++i) {
if (best[i] == INF)
fout << 0 << ' ';
else
fout << best[i] << ' ';
}
}
int main() {
Read();
Dijkstra();
Write();
fin.close();
fout.close();
return 0;
}