码迷,mamicode.com
首页 > 其他好文 > 详细

全是套路——最短路径(图)

时间:2016-08-26 20:04:21      阅读:132      评论:0      收藏:0      [点我收藏+]

标签:

#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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!