Home > Back-end >  The principle and the path is a non decreasing how to do?? To explain, for code
The principle and the path is a non decreasing how to do?? To explain, for code

Time:10-11

1. Realize the directed hypergraph shortest path (benchmark points 4.5)
Topic 1. The traditional simple weighted graph, each have to have a simple while rights, such as W: e (1, 2)=3 means nodes 1 and 2 of the edge has a weight of 3, now considered a complex weighted hypergraph (have to) structure: each have to side with a weight set W {w1, w2,... Wn}, where n is any positive integer greater than or equal to 1, and w1, w2,... , wn in descending than that between any two nodes with multiple weighted edge, now design a non-recursive algorithm, using C/C + + realize the shortest path to any specified node, and in the path from source node to the destination node according to the order of the edge, weight conforms to the principle of diminishing,
The principle and the path is a non decreasing how to do?? Please explain,,,

#include
#include
using namespace std;

Const int MAX=99999;
Const int MAX_VEX_NUM=20;



The class MGraph
{
Private:
Int vexs [MAX_VEX_NUM];//the vertex array
Int arcs [MAX_VEX_NUM] [MAX_VEX_NUM];//adjacency matrix
Int vexnum;//vertices
Int arcnum;//number of edges
Int weight [20] [20] [20] [20].

Public:
Void Create_MG ()
{
Int I, j, k;
cout<& lt;" The input figure on the number of vertices and edges, ";
Cin> Vexnum> Arcnum;
cout<& lt;" Enter a name for each vertex: ";
for(i=0; iCin> Vexs [I];

for(i=0; ifor(int j=0; jArcs [I] [j]=MAX;

For (I=0; i<20; I++)
{
For (int n=0; n<20; N++)
{
For (int m=0; M<20; M++)
{
For (int r=0; r<20; R++)
{
Weight [I] [n] [m] [r]=MAX;
}
}
}
}

For (k=0; k{
cout<& lt;" Enter each edge corresponds to the starting point and end point, and the number of the article: "& lt; Int v1, v2,
Int w, ls;
Cin> V1 & gt;> V2 & gt;> Ls.
For (int s=0; S{
cout<& lt;" Input weight to 99999 end, "& lt; for(int i=0; 1<20; I++)
{
Cin> Weight/v1/v2 [s] [I];
If (weight/v1/v2 [s] [I]==MAX)
break;
}
}
W=weight/v1/v2 [0] [0].
For (int m=0; M<20; M++)
{
For (int a=0; A<20; +)
{


If (w> Weight/v1/v2 [m] [a])
W=weight/v1/v2 [m] [a];
Arcs [v1-1] [] v2-1=w;//the value of right of use arcs to record more than the minimum
}
}
}
cout<& lt;" Figure constructed "& lt; }



Void Floyd_Short_Path (int the path [20] [20], int Dist [20] [20])
{
//Dist [v] [w] to save the shortest path length from the vertex v to w
//path [v] [w] save w previous summits

For (int v=0; VFor (int w=0; W{
Dist [v] [w]=arcs [v] [w];//is used to store the shortest path dist
If (Dist [v] [w] The path [v] [w]=v;
The else
The path [v] [w]=1;
}
For (int u=0; UFor (int v=0; VFor (int w=0; WIf (v!=w & amp; & Dist [v] [u] + Dist [u] [w] {
Dist [v] [w]=Dist [v] [u] + Dist [u] [w];
The path [v] [w]=path [u] [w];
}
}

//the following output in the process of the shortest path between two vertices algorithm, is to use the path [] [] array, the characteristics of reverse output
Void Print (int the path [20] [20], int Dist [20] [20])
{
cout<& lt;" ___ output per the shortest path between two vertices and after vertex ___ "& lt; Int j, a [10], c;
For (int v=0; VFor (int w=0; WIf (Dist [v] [w] {
cout<& lt;" Vertex "& lt; cout<& lt;" After a vertex is: ";
J=w;
C=0;
While (the path [v] [j].=1)
{
A [c]=path [v] [j];
J=path [v] [j];
C + +;
}
For (j=c - 1; j>=0; J -)
cout<[j] vexs [a] <& lt;" - & gt; ";
cout}
}
};

Int main ()
{
MGraph G;
Right reate_MG ();

Int Dist [20] [20].
Int the path [20] [20];

cout<& lt;" O have to the network of the shortest path between every two vertices in "& lt; G.F loyd_Short_Path (path, Dist);
cout
G.P rint (path, Dist);
cout
return 0;
}
  • Related