Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void dijkstra(int v) {
- int* q = new int[n];
- vector<int> s;
- int* d = new int[n];
- int* p = new int[n];
- int min, index, amount = n;
- for (int i = 0; i < n; i++) {
- q[i] = i;
- d[i] = INT_MAX - 200;
- p[i] = -1;
- }
- d[v] = 0;
- while (amount > 0) {
- min = INT_MAX - 200;
- for (int i = 0; i < n; i++) {
- if (q[i] != -1 && min > d[i]) {
- min = d[i];
- index = i;
- }
- }
- s.push_back(q[index]);
- q[index] = -1;
- amount--;
- for (int i = 0; i < n; i++) {
- if (check(index, i) && q[i] != -1) {
- if (d[i] > d[index] + adjMatrix[index][i]) {
- d[i] = d[index] + adjMatrix[index][i];
- p[i] = index;
- }
- }
- }
- }
- for (int i = 0; i < n; i++) {
- cout << i << "\t";
- }
- cout << endl;
- for (int i = 0; i < n; i++) {
- cout << d[i] << "\t";
- }
- cout << endl;
- for (int i = 0; i < n; i++) {
- cout << p[i] << "\t";
- }
- cout << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment