zoukankan      html  css  js  c++  java
  • 最短路径

    //最短路径:迪杰斯算法
    void DJS(vector<vector<int>> &graph,vector<bool> &certain, vector<int> &pre,vector<int> &length,int v)
    {
    	certain[v] = true;
    	pre[v] = v;
    	length[v] = 0;
    	for (int i = 0; i < graph.size() - 1; i++)
    	{
    		int min = 255;
    		int mark = -1;
    		int pre_temp = 0;
    		for (int j = 0; j < certain.size(); j++)
    		{
    			if (certain[j] == true)
    			{
    				for (int k = 0; k < graph[j].size(); k++)
    				{
    					if (graph[j][k] != 0 && certain[k] == false && min > (graph[j][k] + length[j]))
    					{
    						pre_temp = j;
    						mark = k;
    						min = (graph[j][k] + length[j]);
    					}
    
    				}
    			}
    		}
    		if (mark != -1)
    		{
    			certain[mark] = true;
    			pre[mark] = pre_temp;
    			length[mark] = min;
    		}
    		
    	}	
    }
    
    
    int main()
    {
    	int n = 6;
    	vector<vector<int>>  graph(n, vector<int>(n));	
    	vector<bool> certain(n);
    	vector<int> pre(n);
    	vector<int> length(n);
    	for (int i = 0; i < length.size(); i++)
    	{	
    		certain[i] = false;
    		pre[i] = -1;
    		length[i] = 255;
    
    	}
    	graph[0] = { 0, 0, 10, 0, 30, 100 };
    	graph[1] = { 0, 0, 5, 0, 0, 0 };
    	graph[2] = { 0, 0, 0, 50, 0, 0 };
    	graph[3] = { 0, 0, 0, 0, 0, 10 };
    	graph[4] = { 0, 0, 0, 20, 0, 60 };
    	graph[5] = { 0, 0, 0, 0, 0, 0 };
    	show_graph(graph);
    	cout << endl;
    	DJS(graph, certain, pre, length, 0);
    	for (int i = 0; i < graph.size(); i++)
    	{
    		cout << "certain[" << i << "] = " << certain[i] << "    ";
    		cout << "pre[" << i << "] = " << pre[i] << "    ";
    		cout << "length[" << i << "] = " << length[i] <<endl;
    	}	
    	system("pause");
    	return 0;
    }
    

      

  • 相关阅读:
    Python if语句
    Pyhton数据类型总结
    Flask系列之自定义中间件
    Flask系列之蓝图中使用动态URL前缀
    python+Nginx+uWSGI使用说明
    python之threading.local
    python之偏函数
    Flask系列之源码分析(一)
    Python扩展之类的魔术方法
    Flask系列(十一)整合Flask中的目录结构(sqlalchemy-utils)
  • 原文地址:https://www.cnblogs.com/zhizhi25/p/5895821.html
Copyright © 2011-2022 走看看