Pagini recente » Cod sursa (job #1845774) | Cod sursa (job #1427724) | Cod sursa (job #71655) | Cod sursa (job #1642975) | Cod sursa (job #869790)
Cod sursa(job #869790)
#include <algorithm>
#include <fstream>
#include <queue>
#include <vector>
using namespace std;
ifstream f("dijkstra.in");
ofstream g("dijkstra.out");
#define FORIT(it, v) for (typeof((v).begin()) it = (v).begin(); it != (v).end(); ++it)
const int N = 50005;
int n, m;
int cost[N];
vector <pair <int, int> > graph[N];
struct cmp {
bool operator() (pair <int, int> &a, pair <int, int> &b) {
return a.second > b.second;
}
};
priority_queue <pair <int, int>, vector <pair <int, int> >, cmp> heap;
void dijkstra(int start) {
for (int i = 0; i <= n; ++i)
cost[i] = 0x3f3f3f3f;
cost[start] = 0;
heap.push(make_pair(start, 0));
while (!heap.empty()) {
pair <int, int> top = heap.top();
heap.pop();
FORIT(it, graph[top.first]) {
int cost_curent = cost[top.first] + it->second;
if (cost_curent < cost[it->first]) {
cost[it->first] = cost_curent;
heap.push(make_pair(it->first, cost[it->first]));
}
}
}
}
int main() {
f >> n >> m;
for (int i = 1; i <= m; ++i) {
int a, b, c;
f >> a >> b >> c;
graph[a].push_back(make_pair(b, c));
}
dijkstra(1);
for (int i = 2; i <= n; ++i)
g << cost[i] << ' ';
}