请把下面这段c++语言转化为c语言template<class T> void Graph<T>::FMultiGraph(int k,int *p) {//采用程序6-8的邻接表存储图G。 float *cost=new float[n]; int q,*d=new int[n]; cost[n-1]=0,d[n-1]=-1; for (int j=n-2;j>=0;j--){ float min=INFTY; for (ENode<T> *r=a[j];r;r=r->nextArc) { int v=r->adjVex; if (r->w+cost[v]<min) { min=r->w+cost[v]; q=v; } } cost[j]=min; d[j]=q; } p[0]=0; p[k-1]=n-1; for(j=1;j<=k-2;j++) p[j]=d[p[j-1]]; delete []cost; delete []d; }
时间: 2024-03-26 12:39:34 浏览: 53
C语言版贪吃蛇,不含#include<graph.c>头文件
4星 · 用户满意度95%
这段C++代码转化为C语言如下:
```c
template<class T>
void FMultiGraph(Graph<T>* graph, int k, int* p) {
// 采用程序6-8的邻接表存储图G。
float* cost = (float*)malloc(sizeof(float) * graph->n);
int q, *d = (int*)malloc(sizeof(int) * graph->n);
cost[graph->n - 1] = 0, d[graph->n - 1] = -1;
for (int j = graph->n - 2; j >= 0; j--) {
float min = INFTY;
for (ENode<T>* r = graph->a[j]; r; r = r->nextArc) {
int v = r->adjVex;
if (r->w + cost[v] < min) {
min = r->w + cost[v];
q = v;
}
}
cost[j] = min;
d[j] = q;
}
p[0] = 0, p[k - 1] = graph->n - 1;
for (int j = 1; j <= k - 2; j++)
p[j] = d[p[j - 1]];
free(cost);
free(d);
}
```
阅读全文