Graph-DFS-Map-图的深度优先遍历-城市地图问题

Posted Archibald Witwicky

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Graph-DFS-Map-图的深度优先遍历-城市地图问题相关的知识,希望对你有一定的参考价值。

#include <iostream>

using namespace std;

/*
5 8
1 2 2
1 5 10
2 3 3
2 5 7
3 1 4
3 4 4
4 5 5
5 3 3

min: 9
--------------------------------
Process exited with return value 0
Press any key to continue . . .
*/

const int infinity = 999999;
int minnum = infinity;
int vertx, edge;
int Graph[20][20] = {0}, book[20] = {0};

void DFS(int _n, int _dis)
{
	if(_dis > minnum) return;
	
	if(_n == vertx)
	{
		if(_dis < minnum)
		{
			minnum = _dis;
		}
		return;
	}
	
	for(int i = 1; i <= vertx; i++)
	{
		if(Graph[_n][i] != infinity && book[i] == 0)
		{
			book[i] = 1;
			DFS(i, _dis + Graph[_n][i]);
			book[i] = 0;
		}
	}
}

int main()
{
	cin >> vertx >> edge;
	
	for(int i = 1; i <= vertx; i++)
	{
		for(int j = 1; j <= vertx; j++)
		{
			if(i == j)
			{
				Graph[i][j] = 0;
			}
			Graph[i][j] = infinity;
		}
	}
	
	for(int i = 1; i <= edge; i++)
	{
		int x, y, z;
		cin >> x >> y >> z;
		Graph[x][y] = z; 
	}
	
	DFS(1, 0);
	
	cout << endl << "min: " << minnum;
	
	return 0;
} 

  

以上是关于Graph-DFS-Map-图的深度优先遍历-城市地图问题的主要内容,如果未能解决你的问题,请参考以下文章

第五章 图的遍历(深度遍历,广度遍历,城市地图,最少转机)

什么是图的深度优先遍历?什么是图的广度优先遍历?

图的遍历:深度优先遍历,广度优先遍历

图的深度/广度优先遍历C语言程序

求图的深度优先遍历程序 c语言版

图的广度遍历和深度遍历