标签:
#include<iostream> #include<cstring> #include<string> #include <vector> #include <algorithm> #include <stack> using namespace std; int map[6][6] = { 999, 999, 999, 999, 999, 999, 999, 999, 10, 999, 30, 100, 999, 999, 999, 50, 999, 999, 999, 999, 999, 999, 999, 10, 999, 999, 999, 20, 999, 60, 999, 999, 999, 999, 999, 999, }; vector<int> res, res1; int path[10]; void dij() { bool visit[6] = { false }; int dist[6] = { 999 }; for (int i = 1; i <= 5; i++) { dist[i] = map[1][i]; } visit[1] = true; path[1] = 1; for (int i = 1; i <= 5; i++) { int min = 777; int min_index = 0; for (int k = 2; k <= 5; k++) { if (visit[k] != true && dist[k] <= min) { min = dist[k]; min_index = k; } } visit[min_index] = true; res.push_back(min_index); for (int k = 2; k <= 5; k++) { if (visit[k] != true && (dist[k] > map[min_index][k] + min)) { dist[k] = map[min_index][k] + min; path[k] = min_index; } } } int jjj = 0; } void showPath(int *path, int v, int v0) //打印最短路径上的各个顶点 { stack<int> s; int u = v; while (v != v0) { s.push(v); v = path[v]; } s.push(v); while (!s.empty()) { cout << s.top() << " "; s.pop(); } } int main() { dij(); showPath(path, 5, 1); return 0; }
标签:
原文地址:http://www.cnblogs.com/wyc199288/p/5811528.html