标题:各位MATLAB高手赶快帮下忙啊!
只看楼主
zzxgenius529
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2009-10-8
 问题点数:0 回复次数:1 
各位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;
}
搜索更多相关主题的帖子: MATLAB 
2009-10-11 11:27
johnliu1983
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:14
专家分:117
注 册:2009-10-9
得分:0 
你这个是做什么的?没有推导过程吗?如果有推导过程可能比直接看着这个代码编的还快呢。
2009-10-15 10:32



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-288120-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.607789 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved