关键路径算法实现

ContractedBlock.gif ExpandedBlockStart.gif 代码
 
      
1 #include < stdio.h >
2 #include < stdlib.h >
3 #include < iomanip.h >
4 #include < process.h >
5
6   // #define PROJECTNUMBER 9 // 10
7
8 // #define PLANNUMBER 11 // 13
9 int SearchMapPath(Graphicmap,projectnumber,activenumber,totaltime);
10 void CreateGraphic(Graphicmap,projectnumber,activenumber);
11 typedef struct node
12 {
13 int adjvex;
14 int dut;
15 struct node * next;
16 }edgenode;
17
18 typedef struct
19 {
20 int projectname;
21 int id;
22 edgenode * link;
23 }vexnode;
24
25 // vexnode Graphicmap[PROJECTNUMBER];
26
27 void CreateGraphic(vexnode * Graphicmap, int projectnumber, int activenumber)
28 {
29 int begin,end,duttem;
30 int i,k;
31 edgenode * p;
32 for (i = 0 ;i < projectnumber;i ++ )
33 {
34 Graphicmap[i].projectname = i;
35 Graphicmap[i].id = 0 ;
36 Graphicmap[i].link = NULL;
37 }
38 printf( " 某项目的开始到结束在图中的节点输入<vi,vj,dut>\n " );
39 printf( " 如:3,4,9 回车表示第三节点到第四节点之间的活动用了9个单位时间\n " );
40 for (k = 0 ;k < activenumber;k ++ )
41 {
42 scanf( " %d,%d,%d " , & begin, & end, & duttem);
43 p = (edgenode * )malloc( sizeof (edgenode));
44 p -> adjvex = end - 1 ;
45 p -> dut = duttem;
46 Graphicmap[end - 1 ].id ++ ;
47 p -> next = Graphicmap[begin - 1 ].link ;
48 Graphicmap[begin - 1 ].link = p;
49 }
50 }
51
52 int SearchMapPath(vexnode * Graphicmap, int projectnumber, int activenumber, int totaltime)
53 {
54 int i,j,k,m = 0 ;
55 int front =- 1 ,rear =- 1 ;
56 int * topologystack = ( int * )malloc(projectnumber * sizeof ( int )); // 用来保存拓扑排列
57 int * vl = ( int * )malloc(projectnumber * sizeof ( int )); // 用来表示在不推迟整个工程的前提下,VJ允许最迟发生的时间
58 int * ve = ( int * )malloc(projectnumber * sizeof ( int )); // 用来表示Vj最早发生时间
59 int * l = ( int * )malloc(activenumber * sizeof ( int )); // 用来表示活动Ai最迟完成开始时间
60 int * e = ( int * )malloc(activenumber * sizeof ( int )); // 表示活动最早开始时间
61 edgenode * p;
62 totaltime = 0 ;
63 for (i = 0 ;i < projectnumber;i ++ ) ve[i] = 0 ;
64 for (i = 0 ;i < projectnumber;i ++ )
65 {
66 if (Graphicmap[i].id == 0 )
67 {
68 topologystack[ ++ rear] = i;
69 m ++ ;
70 }
71 }
72 while (front != rear)
73 {
74 front ++ ;
75 j = topologystack[front];
76 m ++ ;
77 p = Graphicmap[j].link ;
78 while (p)
79 {
80 k = p -> adjvex ;
81 Graphicmap[k].id -- ;
82 if (ve[j] + p -> dut > ve[k])
83 ve[k] = ve[j] + p -> dut ;
84 if (Graphicmap[k].id == 0 )
85 topologystack[ ++ rear] = k;
86 p = p -> next ;
87 }
88 }
89 if (m < projectnumber)
90 {
91 printf( " \n本程序所建立的图有回路不可计算出关键路径\n " );
92 printf( " 将退出本程序\n " );
93 return 0 ;
94 }
95 totaltime = ve[projectnumber - 1 ];
96 for (i = 0 ;i < projectnumber;i ++ )
97 vl[i] = totaltime;
98 for (i = projectnumber - 2 ;i >= 0 ;i -- )
99 {
100 j = topologystack[i];
101 p = Graphicmap[j].link ;
102 while (p)
103 {
104 k = p -> adjvex ;
105 if ((vl[k] - p -> dut ) < vl[j])
106 vl[j] = vl[k] - p -> dut ;
107 p = p -> next ;
108 }
109 }
110 i = 0 ;
111 printf( " | 起点 | 终点 | 最早开始时间 | 最迟完成时间 | 差值 | 备注 |\n " );
112 for (j = 0 ;j < projectnumber;j ++ )
113 {
114 p = Graphicmap[j].link;
115 while (p)
116 {
117 k = p -> adjvex ;
118 e[ ++ i] = ve[j];
119 l[i] = vl[k] - p -> dut;
120 printf( " | %4d | %4d | %4d | %4d | %4d | " ,Graphicmap[j].projectname + 1 ,Graphicmap[k].projectname + 1 ,e[i],l[i],l[i] - e[i]);
121 if (l[i] == e[i])
122 printf( " 关键活动 | " );
123 printf( " \n " );
124 p = p -> next ;
125 }
126 }
127 return 1 ;
128 }
129
130 void seekkeyroot()
131 {
132 int projectnumber,activenumber,totaltime = 0 ;
133 vexnode * Graphicmap;
134 system( " cls " );
135 printf( " 请输入这个工程的化成图形的节点数: " );
136 scanf( " %d " , & projectnumber);
137 printf( " 请输入这个工程的活动个数: " );
138 scanf( " %d " , & activenumber);
139 Graphicmap = (vexnode * )malloc(projectnumber * sizeof (vexnode));
140 CreateGraphic(Graphicmap,projectnumber,activenumber);
141 SearchMapPath(Graphicmap,projectnumber,activenumber,totaltime);
142 printf( " 整个工程所用的最短时间为:%d个单位时间\n " ,totaltime);
143 system( " pause " );
144 }
145
146 int main()
147 {
148 char ch;
149 int i;
150 for (;;)
151 {
152 do
153 {
154 system( " cls " );
155 printf( " | 欢迎进入求关键路径算法程序 | " );
156 for (i = 0 ;i < 80 ;i ++ )printf( " * " );
157 printf( " %s " , " (S)tart开始输入工程的节点数据并求出关键路径\n " );
158 printf( " %s " , " (E)xit退出\n " );
159 printf( " %s " , " 请输入选择: " );
160 scanf( " %c " , & ch);
161 ch = toupper(ch);
162 } while (ch != ' S ' && ch != ' E ' );
163 switch (ch)
164 {
165 case ' S ' :
166 seekkeyroot();
167 break ;
168 case ' E ' :
169 return 1 ;
170 }
171 }
172 }
173
174

 

posted on 2010-06-25 14:42  rockysy 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/whg841001/archive/2010/06/25/1765167.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值