SJ diagram theory is very wrong, in order to save the team of knowledge as wide as possible, I directly to the figure continue, now think of all forget, start from the beginning.
The lunar beauty program can be finished by writing the storage of the graph, then writing the minimum spanning tree and the shortest short-circuit of several classical algorithms. 0 0. For a long time, there is a lot of content to write. - -
This concludes the adjacency matrix, adjacency table. Cross linked list. Adjacency multiple table, Edge set Array, these 5 kinds of commonly used diagram of the storage structure, perhaps can be used as a template.
Adjacency Matrix
#include <stdio.h> #include <stdlib.h> #include <string.h>//adjacency matrix int g[100][100];int add1 (int i,int J, int W) { g[i][j] = W;return 0;} int main () { int i,n; Map scanf ("%d", &n); for (i = 0;i < n;i++) { int a,b,w; Input starting point, end point, weight scanf ("%d%d%d", &a,&b,&w); ADD1 (a,b,w); No map plus add1 (b,a,w); } return 0;}
adjacency table
#include <stdio.h> #include <stdlib.h> #include <string.h>//adjacency table struct dot{ int D; int W; struct dot *next;}; struct hed{ int v; struct dot *next;} Head[100];int add2 (int i,int j,int w) { struct dot * p; struct dot * t = new dot; T->d = j; T->w = W; T->next = NULL; if (Head[i].next = = NULL) { head[i].next = t; return 0; } p = head[i].next; while (p->next! = NULL) p = p->next; P->next = t; return 0;} int main () { int i,n;memset (head,0,sizeof (head)); Map scanf ("%d", &n); for (i = 0;i < n;i++) { int a,b,w; Input starting point, end point, weight scanf ("%d%d%d", &a,&b,&w); ADD2 (a,b,w); No map plus add2 (b,a,w); } return 0;}
Cross linked list (with a map to use)
#include <stdio.h> #include <stdlib.h> #include <string.h>//cross-linked list struct dot{int D; int W; struct dot *next;}; struct hed{int v; struct DOT *to; struct dot *next;} Head[100];int add3 (int i,int j,int w) {struct dot * p; struct dot * t = new dot; T->d = j; T->w = W; T->next = NULL; The Positive adjacency table constructs if (Head[i].next = = NULL) {head[i].next = t; }else {p = head[i].next; while (p->next! = NULL) p = p->next; P->next = t; }//Inverse adjacency table hits cross if (head[i].to = = NULL) {head[i].to = t; return 0; }else {p = head[i].to; while (p->next! = NULL) p = p->next; P->next = t; } return 0;} int main () {int I,n;memset (head,0,sizeof (head)); Map scanf ("%d", &n); for (i = 0;i < n;i++) {int a,b,w; Input starting point, end point, Weight scanf ("%d%d%d", &a,&b,&w); ADD3 (A,B,W); } return 0;}
adjacency Multiple table (no map)
#include <stdio.h> #include <stdlib.h> #include <string.h>//adjacency multiple table (no map) struct dot{int i,j; int W; struct DOT *inext; struct dot *jnext;}; struct hed{int v; struct dot *next;} Head[100];int add4 (int i,int j,int w) {struct dot *t = new dot; struct dot *p = NULL,*TP = NULL; T->i = i; T->j = j; T->w = W; T->inext = NULL; T->jnext = NULL; if (Head[i].next = = NULL) {head[i].next = t; }else {p = head[i].next; while (P! = NULL) {TP = P; if (p->i = = i) p = p->inext; else P = p->jnext; } if (tp->i = = i) Tp->inext = t; else Tp->jnext = t; } if (Head[j].next = = NULL) {head[j].next = t; }else {p = head[j].next; while (P! = NULL) {TP = P; if (p->i = = j) p = P->inexT else P = p->jnext; } if (tp->i = = j) Tp->inext = t; else Tp->jnext = t; } return 0;} int main () {int i,n; memset (head,0,sizeof (head)); Map scanf ("%d", &n); for (i = 0;i < n;i++) {int a,b,w; Input starting point, end point, Weight scanf ("%d%d%d", &a,&b,&w); ADD4 (A,B,W); } return 0;}
Array of Edge Sets
#include <stdio.h> #include <stdlib.h> #include <string.h>//edge set array struct e{ int i,j; int w; } Eg[100];int cont;int add5 (int i,int j,int w) { eg[cont].i = i; EG[CONT].J = j; EG[CONT].W = W; return 0;} int main () { int i,n;memset (eg,0,sizeof (eg)); cont = 0; Map scanf ("%d", &n); for (i = 0;i < n;i++) { int a,b,w; Input starting point, end point, weight scanf ("%d%d%d", &a,&b,&w); There is no direction graph can be add5 (a,b,w); } return 0;}
The forward star of the edge set array
#include <stdio.h> #include <stdlib.h> #include <string.h>//front star int head[100];struct e{ int to; int fro; int W;} Eg[100];int cont;int add6 (int i,int j,int w) { eg[cont].to = j; Eg[cont].fro = Head[i]; EG[CONT].W = W; Head[i] = Cont++;return 0;} int main () { int i,n; memset (head,-1,sizeof (head)); Cont = 0; Map scanf ("%d", &n); for (i = 0;i < n;i++) { int a,b,w; Input starting point, end point, weight scanf ("%d%d%d", &a,&b,&w); Add6 (a,b,w); No map plus add6 (b,a,w); } return 0;}
A summary of the storage structure of the lunar Beauty program map