#include <iostream>
#include <stack>
using namespace Std;
#define MAX 999
#define BIG 101
int A[big][big];
int Dist[big]; The shortest path to save the starting point to another point
int Book[big]; Tag array
int Path[big];
Stack<int> pathout;
void Output_path (int end)
{
int i = end;
int flag = 1;
int sum = 0;
while (flag = = 1)
{
sum++;
Pathout.push (i); Press Stack
if (path[i] = = 0) flag = 2;
else i = path[i]; Points to the previous point of the point
}
cout << "Forward path for:";
while (!pathout.empty ())
{
cout << pathout.top () << ""; Stack top out Stack
if (Sum > 1)
{
sum--;
cout << ",";
}
Pathout.pop (); Remove top of stack
}
cout << Endl;
}
void Dijkstra (int sta, int end, int m,int n)//dijkstra core code
{
while (book[end]! = 1)
{
int min_i;
int min = MAX;
//////////////
for (int i = 1; I <= m; i++)//Find out the nearest point in the unmarked points
{
if (book[i] = = 0 && dist[i] < min)
{
min = Dist[i];
Min_i = i;
}
}
/////////////////
Book[min_i] = 1;
for (int i = 1; I <=m; i++)//slack
{
cout << "i=" << i << Endl;
if (A[min_i][i] < MAX)
{
if (Dist[i] > Dist[min_i] + a[min_i][i])
{
Dist[i] = Dist[min_i] + a[min_i][i];
Path[i] = Min_i;
}
}
}
}
}
int main ()
{
int m, n;
cout << "Welcome to the Path planning system" << Endl << Endl;;
while (cout << "Enter the number of places and Streets:", cin >> m >> N)
{
if (m<2| | n< (m-1| | N> (m* (m-1)/2)))
{
cout << "Input error" << Endl << Endl;
}
Else
{
The following is the initialization
memset (Dist, MAX, sizeof (Dist));
memset (book, 0, sizeof);
memset (path, 0, sizeof (path));
for (int i = 0; i < BIG; i++)
{
for (int j = 0; J < BIG; J + +)
{
if (i = = j) A[i][j] = 0;
else a[i][j] = MAX;
}
}
//////////////////////////////////
int x, y, Z;
cout << "Input Street Information:" << Endl;
for (int i = 1; I <= n; i++)
{
Cin >> x >> y >> z;
A[x][y] = Z;
}
int STA;
int end;
cout << "input departure location and destination:" << Endl;
CIN >> STA >> end;
///////////////////////
int min_i = MAX;
for (int i = 1; I <= m; i++)
{
Dist[i] = A[sta][i]; Distance from the starting point to the remaining points
if (Dist[i]<max&&dist[i] > 0 && dist[i] < min_i) Min_i = i;
}
Path[min_i] = STA;
Book[sta] = 1;
//////////////////////
Dijkstra (STA, end,m,n);
//////////////////////
cout << "The shortest distance from the point of departure to the destination is:" << dist[end] << Endl;
Output_path (end);
}
cout << "Whether to continue querying. Y or N: ";
char c;
CIN >> C;
if (c = = ' n ' | | c = = ' n ')
{
cout << "Thanks for using. Goodbye "<< Endl << Endl;
GetChar ();
GetChar ();
return 0;
}
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.