1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95

#include <fstream>
#include <functional>
#include <climits>
#include <vector>
#include <queue>
#include <list>
using namespace std;
struct node {
int vertex;
int weight;
node(int v, int w) : vertex(v), weight(w) { };
node() { }
};
class CompareGreater {
public:
bool const operator()(node &nodeX, node &nodeY) {
return (nodeX.weight > nodeY.weight) ;
}
};
vector< list<node> > adj;
vector<int> weights;
priority_queue<node, vector<node>, CompareGreater> Q;
int nrVertices, nrEdges;
void readData();
void Dijkstra(node);
void writeData();
int main(int argc, char *argv[]) {
readData();
Dijkstra(node(1, 0));
writeData();
return 0;
}
void readData() {
fstream in("dijkstra.in", ios::in);
int nodeX, nodeY, weight;
in >> nrVertices >> nrEdges;
adj.resize(nrVertices+1);
weights.resize(nrVertices+1);
for (int i = 1; i <= nrVertices; ++i) {
weights.push_back(INT_MAX);
}
for (int i = 1; i <= nrEdges; ++i) {
in >> nodeX >> nodeY >> weight;
adj[nodeX].push_back(node(nodeY, weight));
}
in.close();
}
void Dijkstra(node startNode) {
node currentNode;
weights[startNode.vertex] = 0;
Q.push(startNode);
while (!Q.empty()) {
currentNode = Q.top();
Q.pop();
if (currentNode.weight <= weights[currentNode.vertex]) {
for (list<node>::iterator it = adj[currentNode.vertex].begin(); it != adj[currentNode.vertex].end(); ++it) {
if (weights[it>vertex] > weights[currentNode.vertex] + it>weight) {
weights[it>vertex] = weights[currentNode.vertex] + it>weight;
Q.push(node((it>vertex), weights[it>vertex]));
}
}
}
}
}
void writeData() {
fstream out("dijkstra.out", ios::out);
weights.resize(nrVertices+1);
for (vector<int>::iterator it = weights.begin()+1; it != weights.end(); ++it) {
out << (*it) << " ";
}
out.close();
}
 