From ce1def20fc7dda51e0934fd012fa40d68b15a9bc Mon Sep 17 00:00:00 2001 From: huxin <2942218032@qq.com> Date: Mon, 8 Jun 2020 20:49:34 +0800 Subject: [PATCH] =?UTF-8?q?add=20=E6=88=91=E7=9A=84=E5=9E=83=E5=9C=BE?= =?UTF-8?q?=E4=BD=9C=E4=B8=9A.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...6\203\345\234\276\344\275\234\344\270\232" | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 "\346\210\221\347\232\204\345\236\203\345\234\276\344\275\234\344\270\232" diff --git "a/\346\210\221\347\232\204\345\236\203\345\234\276\344\275\234\344\270\232" "b/\346\210\221\347\232\204\345\236\203\345\234\276\344\275\234\344\270\232" new file mode 100644 index 0000000..78b9b58 --- /dev/null +++ "b/\346\210\221\347\232\204\345\236\203\345\234\276\344\275\234\344\270\232" @@ -0,0 +1,39 @@ +//最小生成树,prim算法(从图中找到与树中所有顶点距离最小的点,然后加入树,循环) +int CGraph::FindMinTree(Edge aPath[]) +{ + bool aVisited[MAX] = { false };//判断是否已加入到树 + int length = 0;//累计总长度 + + int k=0;//当前起始点下标,定义从0点开始 + aVisited[k] = true; + + for (int i = 0; i < m_nVexNum - 1; i++) + { + + int minweight = INT_MAX;//寻找与所有树点相关边最小的权值 + int m = 0;//暂时记录最小值下标 + for (int j = 0; j < m_nVexNum; j++) + { + //j代表树中的的点下标,/////!aVisited图中找,aVsited树中找点 + if (aVisited[j]) + { + for (int n = 0; n < m_nVexNum; n++) + { + //n代表图中的的点下标,从图中找与j点相连的最小边 + if ((!aVisited[n]) && (m_aAdjMatrix[j][n] != 0) && (m_aAdjMatrix[j][n]) < minweight) + { + minweight = m_aAdjMatrix[j][n];//取小值 + k = j;//边两点vex1,vex2,(赋值k时,循环几次,浪费时间) + m = n; + } + } + } + }//找到树中接下来添加的点了 + aVisited[m] = true;//将找到的点加入树中 + length += minweight; + aPath[i].vex1 = k; + aPath[i].vex2 = m; + aPath[i].weight = minweight; + } + return length; +} \ No newline at end of file -- Gitee