// Single-source shortest path Bellman_Ford algorithm. cpp: Defines the entry point for the console application.
//
# Include "stdafx. h"
# Include <iostream>
# Deprecision MAX 100
# Define Infinity 65535
Typedef int WeiType;
Using namespace std;
Struct edgeNode
{
Int no; // The number of the end of the edge
Char info; // edge name
WeiType weight; // Edge weight
Struct edgeNode * next; // next
};
Struct vexNode
{
Char info; // node name
Struct edgeNode * link; // endpoint connected to it
};
// Store node Information
VexNode adjlist [MAX];
// Precursor Node
Int parent [MAX];
// The cost of the shortest path from the source point to the node j
WeiType lowcost [MAX];
// Create an adjacent table Storage
// Adjlist indicates the node set, n indicates the number of nodes, and e indicates the number of edges.
// Return the Source Vertex number
Int createGraph (vexNode * adjlist, int n, int e)
{
Int I;
For (I = 1; I <= n; I ++)
{
Cout <"Enter the node" <I <"name :";
Cin> adjlist [I]. info;
Adjlist [I]. link = NULL;
Parent [I] = I;
Lowcost [I] = Infinity;
}
EdgeNode * p1;
Int v1, v2;
WeiType weight;
For (I = 1; I <= e; I ++)
{
Cout <"Enter the start and end node numbers of <I <:";
Cin> v1> v2;
Cout <"Enter the weight of this edge :";
Cin> weight;
P1 = (edgeNode *) malloc (sizeof (edgeNode ));
P1-> no = v2;
P1-> weight = weight;
P1-> info = adjlist [v2]. info;
P1-> next = adjlist [v1]. link;
Adjlist [v1]. link = p1;
}
Cout <"Enter the Source Vertex number :";
Int v0;
Cin> v0;
Lowcost [v0] = 0;
Return v0;
}
/*
//
Void relax (WeiType * lowcost, int * parent, const int u, const int v, const WeiType w)
{
If (lowcost [v]> lowcost [u] + w)
{
Lowcost [v] = lowcost [u] + w;
Parent [v] = u;
}
}
*/
//
Bool Bellman_Ford (vexNode * adjlist, WeiType * lowcost, int * parent, const int n)
{
Int I, j;
For (j = 1; j <n; j ++)
{
For (I = 1; I <= n; I ++)
{
EdgeNode * p1 = adjlist [I]. link;
While (p1! = NULL)
{
If (lowcost [I] + p1-> weight <lowcost [p1-> no])
{
Lowcost [p1-> no] = lowcost [I] + p1-> weight;
Parent [p1-> no] = I;
}
P1 = p1-> next;
}
}
}
// Check whether there is a negative Loop
For (I = 1; I <= n; I ++)
{
EdgeNode * p2 = adjlist [I]. link;
While (p2! = NULL)
{
If (lowcost [p2-> no]> lowcost [I] + p2-> weight)
Return false;
P2 = p2-> next;
}
}
Return true;
}
// Print the shortest path from the source point to node I
Void print_it (vexNode * adjlist, int * parent, const int v0, const int I)
{
If (I = v0)
Cout <"(" <v0 <":" <adjlist [v0]. info <")";
Else
{
Print_it (adjlist, parent, v0, parent [I]);
Cout <"(" <I <":" <adjlist [I]. info <")";
}
}
Int _ tmain (int argc, _ TCHAR * argv [])
{
Int cases;
Cout <"Enter the number of cases :";
Cin> cases;
While (cases --)
{
Int n, e;
Cout <"Enter the number of nodes :";
Cin> n;
Cout <"Enter the number of sides :";
Cin> e;
// Create an adjacent table
Int v0 = createGraph (adjlist, n, e );
// Call the Bellman-Ford Algorithm
Bool flag = Bellman_Ford (adjlist, lowcost, parent, n );
Int I;
// The shortest path from the output source point to each other (node No.: node name)
For (I = 1; I <= n; I ++)
{
Cout <"Slave source" <adjlist [v0]. info <"to-point" <adjlist [I]. info <"Path:" <endl;
Print_it (adjlist, parent, v0, I );
Cout <endl;
Cout <"The path cost is:" <lowcost [I] <endl;
}
}
System ("pause ");
Return 0;
}
-------------------------------------------------------- Program test --------------------------------------------------------
Enter the number of cases: 1
Enter the number of nodes: 5
Enter the number of edges: 10
Enter Node 1 Name: s
Enter the name of Node 2: t
Enter the name of Node 3: x
Enter node 4 name: y
Enter node 5 Name: z
Enter the start and end nodes of edge 1: 1 2
Enter the value of this edge: 6
Enter the start and end nodes of edge 2: 1 4
Enter the value of this edge: 7
Enter the start and end nodes of edge 3: 2 3
Enter the weight of this edge: 5
Enter the start and end nodes of edge 4: 2 4
Enter the value of this edge: 8
Enter the Start Node and end node number of edge 5: 2 5
Enter the weight of this edge:-4
Enter the start and end node numbers of edge 6: 3 2
Enter the weight of this edge:-2
Enter the start and end nodes of edge 7: 4 3
Enter the weight of this edge:-3
Enter the Start Node and end node number of edge 8: 4 5
Enter the weight of this edge: 9
Enter the Start Node and end node number of edge 9: 5 1
Enter the weight of this edge: 2
Enter the Start Node and end node number of edge 10: 5 3
Enter the value of this edge: 7
Enter the Source Vertex number: 1
The path from s to s is:
(1: s)
The cost of this path is: 0
The path from s to t is:
(1: s) (4: y) (3: x) (2: t)
The cost of this path is: 2
The path from s to x is:
(1: s) (4: y) (3: x)
The cost of this path is: 4
The path from s to y is:
(1: s) (4: y)
The cost of this path is: 7
The path from s to z from the source point is:
(1: s) (4: y) (3: x) (2: t) (5: z)
The path cost is-2.
Press any key to continue...
From heyongluoyao8