diff --git "a/2224020099/chapter_8/\345\256\236\351\252\2148.1.cpp" "b/2224020099/chapter_8/\345\256\236\351\252\2148.1.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..c7753bafcf0894d89c230310e6081729313871ec --- /dev/null +++ "b/2224020099/chapter_8/\345\256\236\351\252\2148.1.cpp" @@ -0,0 +1,29 @@ +#include +#include "graph.h" // Assuming "graph.cpp" contains the graph implementation and "graph.h" contains the declarations + +int main() { + matgraph g; + adjgraph *G; + int A[maxv][maxv] = { + {0, 5, INF, 7, INF, INF}, + {INF, 0, 4, INF, INF, INF}, + {8, INF, 0, INF, INF, 9}, + {INF, INF, 5, 0, INF, 6}, + {INF, INF, INF, 5, 0, INF}, + {3, INF, INF, INF, 1, 0} + }; + int n = 6, e = 10; + + createmat(&g, A, n, e); + printf("(1)图G的邻接矩阵:\n"); + dispmat(g); + + createadj(&G, A, n, e); + printf("(2)图G的邻接表:\n"); + dispadj(G); + + printf("(3)销毁图G的邻接表\n"); + destroyadj(G); + + return 0; +} diff --git "a/2224020099/chapter_8/\345\256\236\351\252\2148.14.cpp" "b/2224020099/chapter_8/\345\256\236\351\252\2148.14.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..46bcff9e38cf1d2ef7f516fbe02f0a54da769de9 --- /dev/null +++ "b/2224020099/chapter_8/\345\256\236\351\252\2148.14.cpp" @@ -0,0 +1,126 @@ +#include +#include +#include +#include +using namesapce std; +#define inf 0x3f3f3f3f +#define maxv 105 +int mat[maxv][maxv]; +int u[maxv]; +int lowcost[maxv]; +int n; +int prim(){ + memset(u,0,sizeof(u)); + memset(lowcost,0,sizeof(lowcost)); + int ans=0; + lowcost[1]=0; + for(int i=1;i<=n;i++){ + int minc=inf,k=0; + for(int j=1;j<=n;j++) + if(!u[j] &&lowcost[j]mat[k][i]) + lowcost[i]=mat[k][i]; + } + return ans; +} + +int parent[maxv]; +int rnk[maxv]; +void init(int n){ + for(int i=1;i<=n;i++) + {parent[i]=i; + rnk[i]=0; + } +} + +int find(int x){ + if(x!=parent[x]) + parent[x]=find(parent[x]); + return parent[x] +} + +void Union(int x,int y){ + int rx=find(x); + int ry=find(y); + if(rx==ry) + return; + if(rnk[rx]u=u; + this->v=v; + this->w=w; + } + bool operator(const edge &s) const + {return w e; + for(int i=1;i<=n;i++) + for(int j=1;j<=n;j++); + if(i= 0; s--) { + printf(",%d", apath[s]); + } + printf(" \t路径长度为:%d\n", A[i][j]); + } + } + } +} + +void floyd(matgraph g){ + int A[maxv][maxv], path[maxv][maxv]; + int i, j, k; + for(i = 0; i < g.n; i++) { + for(j = 0; j < g.n; j++) { + A[i][j] = g.edges[i][j]; + if(i != j && A[i][j] < INF) { + path[i][j] = i; + } else { + path[i][j] = -1; + } + } + } + for(k = 0; k < g.n; k++) { + for(i = 0; i < g.n; i++) { + for(j = 0; j < g.n; j++) { + if(A[i][k] + A[k][j] < A[i][j]) { + A[i][j] = A[i][k] + A[k][j]; + path[i][j] = path[k][j]; + } + } + } + } + dispath(g, A, path); +}