Pagini recente » Cod sursa (job #3260854) | Cod sursa (job #2472653) | Cod sursa (job #3243472) | Cod sursa (job #1879873) | Cod sursa (job #1128581)
#include <fstream>
#include <vector>
#include <queue>
#include <cstring>
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
const int MAXN = 50005;
const int INF = 0x3f3f3f3f;
typedef pair<int, int> cuplu;
struct cmp {
bool operator()(const cuplu &A, const cuplu &B) const {
return A.second > B.second;
}
};
int N, M;
int best[MAXN];
vector<cuplu> G[MAXN];
priority_queue< cuplu, vector<cuplu>, cmp > PQ;
bool marked[MAXN];
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));
do {
int node = PQ.top().first;
PQ.pop();
marked[node] = true;
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;
if (!marked[it->first])
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]) << ' ';
}
int main() {
Read();
memset(best, INF, sizeof(best));
best[1] = 0;
Dijkstra();
Write();
fin.close();
fout.close();
return 0;
}