void MiniSpanTree_Prim(AMGraph* G, int u) { int k = 0; k = LocateVex(G, u); for (int j = 0; j < G->vexnum; j++) { if (j != k) closedge[j] = { u,G->arcs[k][j] }; } closedge[k].lowcost = 0; for (int i = 0; i < G->vexnum; i++) { k = Min(closedge); int u0 = closedge[k].adjvex; int v0 = G->vexs[k]; printf("%d %d", u0, v0); closedge[k].lowcost = 0; for (int j = 0; j < G->vexnum; j++) { if (G->arcs[i][j] < closedge[j].lowcost) { closedge[i] = { G->vexs[k],G->arcs[i][j] }; } } } }
时间: 2024-03-26 14:39:34 浏览: 47
最小生成树(Prim)算法java实现
以下是将该段代码转换为C语言的代码:
```c
void MiniSpanTree_Prim(AMGraph* G, int u) {
int k = 0;
k = LocateVex(G, u);
for (int j = 0; j < G->vexnum; j++) {
if (j != k) {
closedge[j].adjvex = u;
closedge[j].lowcost = G->arcs[k][j];
}
}
closedge[k].lowcost = 0;
for (int i = 0; i < G->vexnum; i++) {
k = Min(closedge);
int u0 = closedge[k].adjvex;
int v0 = G->vexs[k];
printf("%d %d", u0, v0);
closedge[k].lowcost = 0;
for (int j = 0; j < G->vexnum; j++) {
if (G->arcs[i][j] < closedge[j].lowcost) {
closedge[j].adjvex = G->vexs[k];
closedge[j].lowcost = G->arcs[i][j];
}
}
}
}
```
阅读全文