This repository was archived by the owner on Dec 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCPM.cpp
More file actions
189 lines (178 loc) · 4.33 KB
/
CPM.cpp
File metadata and controls
189 lines (178 loc) · 4.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/* Finished in the semester of 2020 spring, before final exam of Economics and Management. */
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
#include <cassert>
using namespace std;
constexpr int MAXN(105);
constexpr int INF(0x7fffffff);
struct Edge
{
int to, w;
};
struct Vertex
{
char ID;
int DU; // duration of event
int ES, EF, LS = INF, LF = INF; // Earlist Start/Finish, Latest Start/Finish
int TF; // Total Float, TF = LS - ES = LF - EF
int FF = INF; // Free Float
vector<Edge> to, frm; // to:邻接表, frm: 逆邻接表
int ESpre = -1; // CriticalPath
} event[MAXN];
int nEvent, nEdge;
int indeg[MAXN], outdeg[MAXN];
vector<int> topo; // Topological Sort
bool TopologicalSort()
{
queue<int> q;
for (int i = 0; i < nEvent; i++)
{
if (indeg[i] == 0)
q.push(i);
}
int cnt = 0;
while (!q.empty())
{
int cur = q.front();
q.pop();
cnt++;
topo.push_back(cur);
for (Edge edge : event[cur].to)
{
indeg[edge.to]--;
if (indeg[edge.to] == 0)
{
q.push(edge.to);
}
}
}
return cnt == nEvent;
}
void dispAll()
{
for (int i = 0; i < nEvent; i++)
{
Vertex &ev = event[i];
printf("%5d %5d %5d\n", ev.ES, ev.TF, ev.EF);
printf("%5c %5c %5d\n", 32, ev.ID, ev.DU);
printf("%5d %5d %5d\n", ev.LS, ev.FF, ev.LF);
printf("\n");
}
}
vector<int> criticalpath;
void getCriticalPath(int ed)
{
stack<int> rpath;
for (int cur = ed; cur != -1; cur = event[cur].ESpre)
{
rpath.push(cur);
}
while (!rpath.empty())
{
criticalpath.push_back(rpath.top());
rpath.pop();
}
}
int main()
{
scanf("%d %d", &nEvent, &nEdge);
for (int i = 0; i < nEvent; i++)
{
char c;
int x;
scanf(" %c %d", &c, &x);
event[c - 'A'].ID = c;
event[c - 'A'].DU = x;
}
for (int i = 0; i < nEdge; i++)
{
char c1, c2;
char rel[5];
int det = 0;
scanf(" %c %c %s", &c1, &c2, rel);
int i1 = c1 - 'A', i2 = c2 - 'A';
if (rel[0] == '0')
det = 0;
else
{
scanf("%d", &det);
// ensure `FS`
if (rel[0] == 'S')
det -= event[i1].DU;
if (rel[1] == 'F')
det -= event[i2].DU;
}
event[i1].to.push_back({i2, det});
event[i2].frm.push_back({i1, det});
indeg[i2]++;
outdeg[i1]++;
}
/* ---- Topological Sort ---- */
if (!TopologicalSort())
{
perror("Cycles exist. ");
exit(0);
}
/* ---- Dp on Topological Sort ---- */
int maxi = 0;
for (int evid : topo)
{
Vertex &ev = event[evid];
ev.EF = ev.ES + ev.DU;
for (const Edge &eg : ev.to)
{
Vertex &evt = event[eg.to];
if (ev.EF + eg.w > evt.ES)
{
evt.ES = ev.EF + eg.w;
evt.ESpre = evid;
}
}
if (ev.EF > event[maxi].EF)
maxi = evid;
}
reverse(topo.begin(), topo.end());
for (int evid : topo)
{
Vertex &ev = event[evid];
if (outdeg[evid] == 0)
{
ev.LF = event[maxi].EF;
ev.FF = 0;
}
ev.LS = ev.LF - ev.DU;
ev.TF = ev.LS - ev.ES;
if (ev.FF < 0)
ev.FF = 0;
for (const Edge &eg : ev.frm)
{
Vertex &evp = event[eg.to];
// int ew = eg.w < 0 ? 0 : eg.w;
int ew = eg.w;
if (ev.LS - ew < evp.LF)
{
evp.LF = ev.LS - ew;
}
if (ev.ES - ew - evp.EF < evp.FF)
{
evp.FF = ev.ES - ew - evp.EF;
}
}
}
dispAll();
getCriticalPath(maxi);
/* ---- Display the Critical Path ---- */
for (auto it = criticalpath.begin(), iend = criticalpath.end(); it != iend; it++)
{
printf("%c", event[*it].ID);
if (next(it) != iend)
printf("->");
assert(event[*it].TF == 0 && event[*it].FF == 0);
}
return 0;
}