首页 > 代码库 > 全是套路——最短路径(图)
全是套路——最短路径(图)
#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;}
全是套路——最短路径(图)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。