Created
December 26, 2012 22:37
-
-
Save agasiev/4383678 to your computer and use it in GitHub Desktop.
Dijkstra path search algorithm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <vector> | |
#include <climits> | |
using namespace std; | |
int main(int, char**) { | |
int n = 0; | |
cin >> n; | |
vector<vector<pair<int, int> > > g(n); | |
vector<bool> u(n, false); | |
vector<int> d(n, INT_MAX); d[0] = 0; | |
vector<int> p(n, -1); | |
for (int i = 0; i < g.size(); i++) { | |
int vc = 0; | |
cin >> vc; | |
g[i].resize(vc); | |
int node = 0, weight = 0; | |
for (int j = 0; j < vc; j++) { | |
cin >> node >> weight; | |
g[i][j] = make_pair(node, weight); | |
} | |
} | |
int from = 0, to = 0; | |
cin >> from >> to; | |
while (true) { | |
int ix = 0, mv = INT_MAX; | |
bool exists = false; | |
for (int i = 0; i < d.size(); i++) { | |
if (u[i] == false && d[i] <= mv) { | |
ix = i; | |
mv = d[i]; | |
exists = true; | |
} | |
} | |
if (!exists || d[ix] == INT_MAX) break; | |
u[ix] = true; | |
for (int i = 0; i < g[ix].size(); i++) { | |
if (d[g[ix][i].first] > d[ix] + g[ix][i].second) { | |
d[g[ix][i].first] = d[ix] + g[ix][i].second; | |
p[g[ix][i].first] = ix; | |
} | |
} | |
} | |
int ix = to; | |
do { | |
cout << ix << " <- "; | |
ix = p[ix]; | |
} while (p[ix] != -1); | |
cout << from << endl; | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 | |
3 1 3 2 2 3 2 | |
1 2 1 | |
1 5 3 | |
2 2 2 4 1 | |
1 5 1 | |
0 | |
0 5 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment