1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
|
// Shortest route.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
void shortest_Distance(int adj[][100], int distance[], int shortest[], int IN[], int n, int nodestart)
{
//adj is the adjacency matrix, IN is include
//1. Initialize Arrays distance,shortest,and IN
for (int j = 0; j < n; j++)
{
distance[j] = adj[nodestart][j];
}
for (int j = 0; j < n; j++)
{
shortest[j] = nodestart;
}
for (int j = 0; j < n; j++)
{
IN[j] = 0; //null
}
IN[0] = nodestart;
int min = distance[0];
for (int i = 1; i < n; i++)
{
//2. Find the min in the array distance
if (distance[i] < min)
min = distance[i];
//3.add the node to IN
IN[i] = IN[0]+min;
//4. Find the min of two distances.
}
}
int _tmain(int argc, _TCHAR* argv[])
{
char name[100] = { "x,1,2,3,4" };
int adj[100][100] =
{
{ 0, 3, 8, 4, 0, 10 },
{ 3, 0, 0, 6, 0, 0 },
{ 8, 0, 0, 0, 7, 0 },
{ 4, 6, 0, 0, 1, 3 },
{0, 0, 7, 1, 0, 1 },
{ 10, 0, 0, 3, 1, 0 }
};
//shortest_Distance(adj,);
return 0;
}
| |