Today, we will review the method of storage construction with weighted network. Now the traffic is in all directions, the road network is dense, from one place to another place, there can be a lot of paths to choose, today I am talking about the network connectivity and non-connectivity and the cost of the route (such as distance or bump level, etc. as the criteria for evaluating the route, take the value) in the computer how to store and reproduce.
Assuming there are 6 locations and there are 8 directly connected roads between points and points, the TXT file is divided into three main parts:
The first part stores the number of points and directly connected routes, the second part stores the coordinates of the points, the third part of the storage route (such as: 2 4 5, indicating that the 2nd point and 4 points directly connected, this route allocation of weights is 5), such as the left.
So how do these relationships show up in the structure of the program?
We can use an array vertex a[points] to store the coordinates of each point, the subscript of the array corresponds to a point number, and then a two-dimensional array int weight[points] [points] to correspond to the connection between the point and point and the weight value, such as 2 4 5, expressed as weight[2][4]= Weight[4][2]=5.
Well, don't say much, go straight to the code it's easier to be rude. (Naughty face) ~^.^~
#include"stdafx.h"#include<stdlib.h>#include"Graph.h"#include"Targetver.h"#defineMax_vertex_num 20typedefstructvertex{Doublex, y;} Vertex;typedefstructgraph{Vertex ver[max_vertex_num]; intWeight[max_vertex_num][max_vertex_num]; intVexnum; intArcnum;} Graph;voidMain () {graph*G; G= (graph *)malloc(sizeof(graph)); FILE* fp = fopen ("Information.txt","R"); if(FP) {fscanf (FP,"%d", & (G->vexnum));//number of verticesFSCANF (FP,"%d", & (G->arcnum));//Number of sides for(inti =0; I <g->vexnum; i++)//Vertex ArrayFSCANF (FP,"%LF,%LF", & (g->ver[i].x),& (g->ver[i].y)); intV1, v2, W;//V1,v2 starting from 0 for(inti =0; i < G->arcnum; i++)//Constructing adjacency Matrix and the weight-value relationship of edges{fscanf (FP)," %d%d%d", &v1, &v2, &W); G->WEIGHT[V1][V2] =W; }}fclose (FP); Setorig (0,0); for(inti =0; I < g->vexnum; i++) {DrawRectangle (G->ver[i].x,g->ver[i].y); Charlabel[5]; sprintf (Label,"%d", i); DrawText (G->ver[i].x, g->ver[i].y, label); for(intj =0; J <G->vexnum; J + +) { if(g->weight[i][j]>0) {moveTo (G->ver[i].x, g->ver[i].y); LineTo (G->ver[j].x,g->ver[j].y); }}} GetChar ();}
Next is the time to witness miracles! duang~~duang~~duang~~
Constructing the belt-weighted network