Pagini recente » Cod sursa (job #851742) | Cod sursa (job #1734165) | Cod sursa (job #3238561) | Cod sursa (job #2859913) | Cod sursa (job #1802112)
#include <iostream>
#include <fstream>
#include <queue>
using namespace std;
const int MAX = 50010, INF = 0x3f3f3f3f;
vector<int> DIST;
vector<pair<int, int> > ARC[MAX];
priority_queue<pair<int, int> > Q;
void dijkstra(int n) {
Q.push(make_pair(0, n));
DIST[n] = 0;
while(!Q.empty()) {
int node = Q.top().second;
int dist = Q.top().first;
Q.pop();
if(DIST[node] != dist) continue;
for(vector<pair<int, int> > :: iterator it = ARC[node].begin(); it != ARC[node].end(); ++ it) {
int dest = it->first;
if(DIST[dest] > DIST[node] + it->second) {
DIST[dest] = DIST[node] + it->second;
Q.push(make_pair(DIST[dest], dest));
}
}
}
}
int main() {
int N, M, A, B, C;
ifstream in("dijkstra.in");
ofstream out("dijkstra.out");
in >> N >> M;
for(int i = 0; i <= N; i++) {
DIST.push_back(INF);
}
for(int i = 0; i < M; i++) {
in >> A >> B >> C;
if(!(C && (A == B))) {
ARC[A].push_back(make_pair(B, C));
}
}
dijkstra(1);
for(int i = 2; i <=N; i++) {
int result = DIST[i];
if(INF == result) {
result = 0;
}
out << result << " ";
}
return 0;
}