Dijkstra算法C++的实现

//
//  main.cpp
//  testC++05
//
//  Created by fei dou on 12-8-7.
//  Copyright (c) 2012年 vrlab. All rights reserved.
//

#include <iostream>
#include <iostream>
#include <vector>
#include <stack>
using namespace std;


int map[][5] = {                     //定义有向图
    {0, 1, 5,10, INT_MAX},
    {1, 0, 20, INT_MAX, INT_MAX},
    {5, 20, 0, 2, 5},
    {10, INT_MAX,2, 0, 2},
    {INT_MAX, INT_MAX, 5, 2, 0}
};



void Dijkstra(
              const int numOfVertex,    /*节点数目*/
              const int startVertex,    /*源节点*/
              int (map)[][5],          /*有向图邻接矩阵*/
              int *distance,            /*各个节点到达源节点的距离*/
              int *prevVertex           /*各个节点的前一个节点*/
              )
{
    vector<bool> isInS;                 //是否已经在S集合中
    isInS.reserve(0);
    isInS.assign(numOfVertex, false);   //初始化,所有的节点都不在S集合中
    
    /*初始化distance和prevVertex数组*/
    for(int i =0; i < numOfVertex; ++i)
    {
        distance[ i ] = map[ startVertex ][ i ];
        if(map[ startVertex ][ i ] < INT_MAX)
            prevVertex[ i ] = startVertex;
        else
            prevVertex[ i ] = -1;       //表示还不知道前一个节点是什么
    }
    prevVertex[ startVertex ] = -1;
    
    /*开始使用贪心思想循环处理不在S集合中的每一个节点*/
    isInS[startVertex] = true;          //开始节点放入S集合中
    
    
    int u = startVertex;
    
    for (i = 1; i < numOfVertex; i ++)      //这里循环从1开始是因为开始节点已经存放在S中了,还有numOfVertex-1个节点要处理
    {
        
        /*选择distance最小的一个节点*/
        int nextVertex = u;
        int tempDistance = INT_MAX;
        for(int j = 0; j < numOfVertex; ++j)
        {
            if((isInS[j] == false) && (distance[j] < tempDistance))//寻找不在S集合中的distance最小的节点
            {
                nextVertex = j;
                tempDistance = distance[j];
            }
        }
        isInS[nextVertex] = true;//放入S集合中
        u = nextVertex;//下一次寻找的开始节点
        
        
        /*更新distance*/
        for (j =0; j < numOfVertex; j ++)
        {
            if (isInS[j] == false && map[u][j] < INT_MAX)
            {
                int temp = distance[ u ] + map[ u ][ j ];
                if (temp < distance[ j ])
                {
                    distance[ j ] = temp;
                    prevVertex[ j ] = u;
                }
            }
        }
    }    
}


int main (int argc, const char * argv[])
{
    int distance[5];
    int preVertex[5];
    
	while (1)
	{
		cout<<"请输入源节点、目的节点"<<endl;
		int s,d;
		cin>>s>>d;
		if (s == -1)
		{
			break;
		}
		Dijkstra(5, s, map, distance, preVertex);
		int index = d;
		stack<int > trace;
		while (preVertex[index] != -1) 
		{
			trace.push(preVertex[index]);
			index = preVertex[index];
		}
		
		cout << "路径:";
		while (!trace.empty()) 
		{
			cout<<trace.top()<<" -- ";
			trace.pop();
		}
		cout <<d;
		cout <<" 距离是:"<<distance[d]<<endl;

	}

    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值