Pagini recente » Cod sursa (job #998460) | Cod sursa (job #2684514) | Cod sursa (job #1799126) | Cod sursa (job #1947580) | Cod sursa (job #1207114)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#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;
int N, M;
int best[MAXN];
vector<cuplu> G[MAXN];
struct cmp {
bool operator() (const cuplu A, const cuplu B) const {
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 Init() {
memset(best, INF, sizeof(best));
best[1] = 0;
PQ.push( make_pair(1, 0) );
}
void Dijkstra() {
do {
cuplu node = PQ.top();
PQ.pop();
vector<cuplu>::iterator it;
for (it = G[node.first].begin(); it != G[node.first].end(); ++it) {
if (best[it->first] > best[node.first] + it->second) {
best[it->first] = best[node.first] + it->second;
PQ.push( make_pair(it->first, best[it->first]) );
}
}
}while (!PQ.empty());
}
void Write() {
for (int i = 2; i <= N; ++i) {
fout << (best[i] == INF ? 0 : best[i]) << ' ';
}fout << '\n';
}
int main() {
Read();
Init();
Dijkstra();
Write();
fin.close();
fout.close();
return 0;
}