求助:用MATLAB编程问题
请各位高手帮帮忙……用MATLAB对一下C++程序重新编程下……拜托大家了!!#include <stdio.h>
#include <stdlib.h>
struct node{
int to;
int value;
node *next;
};
struct graph{
node **from;
int n;
};
void CreatGraph(graph &G)
{
printf("请输入网络计划图拥有的节点数: ");
scanf("%d", &G.n);
G.from = (node **)malloc(sizeof(node *) * G.n);
for (int i=0; i<G.n; i++) { G.from[i] = NULL; }
printf("请输入每个工序所持续的时间: \n");
printf("(格式:i节点 j节点 工时) \n");
int x, y, w;
while (scanf("%d%d%d", &x, &y, &w) != EOF)
{
x--; y--;
node *p = (node *)malloc(sizeof(node));
p->to = y;
p->value = w;
p->next = G.from[x];
G.from[x] = p;
}
}
void FindInDegree(graph &G, int indegree[])
{
int i;
node *p;
for (i=0; i<G.n; i++) { indegree[i] = 0; }
for (i=0; i<G.n; i++) {
for (p=G.from[i]; p; p=p->next) {
indegree[p->to]++;
}
}
}
bool TopologicalOrder(graph &G, int ve[], int T[], int &topt)
{
int *indegree = (int *)malloc(sizeof(int) * G.n);
FindInDegree(G, indegree);
int *s = (int *)malloc(sizeof(int) * G.n);
int tops = -1;
for (int i=0; i<G.n; i++) {
ve[i] = 0;
if (indegree[i] == 0) { s[++tops] = i; }
}
int count = 0;
topt = -1;
while (tops != -1) {
int j = s[tops--];
T[++topt] = j;
count++;
for (node *p=G.from[j]; p; p=p->next) {
int k = p->to;
if (--indegree[k] == 0) { s[++tops] = k; }
if (ve[j] + p->value > ve[k]) {
ve[k] = ve[j] + p->value;
}
}
}
if (count < G.n) return false;
else return true;
}
bool CriticalPath(graph &G)
{
int *T = (int *)malloc(sizeof(int) * G.n);
int i, topt;
int *ve = (int *)malloc(sizeof(int) * G.n); /
int *vl = (int *)malloc(sizeof(int) * G.n);
if (!TopologicalOrder(G, ve, T, topt)) return false;
for (i=0; i<G.n; i++) {
vl[i] = ve[G.n - 1];
}
while (topt != -1)
{
int j = T[topt--];
for (node *p=G.from[j]; p; p=p->next) {
int k = p->to;
if (vl[k] - p->value < vl[j]) {
vl[j] = vl[k] - p->value;
}
}
}
for (i=0; i<G.n; i++) {
for (node *p=G.from[i]; p; p=p->next) {
if (ve[i] == vl[p->to]-p->value)
{
printf("<%d, %d> \n", i+1, p->to+1);
}
}
}
return true;
}
int main()
{
graph G;
CreatGraph(G);
printf("关键路径如下: \n");
CriticalPath(G);
return 0;
}