-
Notifications
You must be signed in to change notification settings - Fork 0
/
任务调度问题.cpp
115 lines (93 loc) · 2.16 KB
/
任务调度问题.cpp
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
#include <iostream>
#include <vector>
#include <algorithm>
using std::vector;
using std::cin;
using std::cout;
using std::endl;
using std::sort;
using std::min;
using std::max;
enum COLOR{white, gray, black};
void critical_path(int node, vector<int> &est, vector<int>& eft, vector<int> &CritDep, vector<COLOR> &color, const vector<vector<int> > &edge, const vector<int> &task_time);
int main()
{
int n(0), task(0), t_time(0);
int x(0), y(0);
cin >> n;
vector<int> task_time(n, 0);
vector<vector<int> > edge;
vector<vector<int> > none_use_edge;
vector<int> est(n, 0);
vector<int> CritDep(n, 0);
vector<COLOR> color(n, white);
vector<int> eft(n, 0);
for (int i(0); i < n; ++i)
{
edge.push_back(*(new vector<int>));
none_use_edge.push_back(*(new vector<int>));
}
for (int i(0); i < n; ++i)
{
cin >> task >> t_time;
task_time[task - 1] = t_time; // ¶ÔӦλÖÃ
}
while (cin >> x >> y)
{
edge[y - 1].push_back(x - 1);
none_use_edge[x - 1].push_back(y - 1);
}
cout << "I have read the rules about plagiarism punishment" << endl;
int result(0);
for (int i(0); i < n; ++i)
{
if (none_use_edge[i].size() == 0)
{
critical_path(i, est, eft, CritDep, color, edge, task_time);
}
int j(0);
for (; j < n; ++j)
{
if (color[j] == white)
break;
}
if (j == n)
break;
}
for (int i(0); i < n; ++i)
{
result = max(result, eft[i]);
}
cout << result;
return 0;
}
void critical_path(int node, vector<int>& est, vector<int>& eft, vector<int>& CritDep, vector<COLOR>& color, const vector<vector<int> >& edge, const vector<int>& task_time)
{
color[node] = gray;
est[node] = 0;
CritDep[node] = -1;
for (int i(0); i < edge[node].size(); ++i)
{
int w = edge[node][i];
if (color[w] == white)
{
critical_path(w, est, eft, CritDep, color, edge, task_time);
if (eft[w] >= est[node])
{
est[node] = eft[w];
CritDep[node] = w;
}
}
else
{
if (eft[w] >= est[node])
{
est[node] = eft[w];
CritDep[node] = w;
}
}
}
eft[node] = est[node] + task_time[node];
color[node] = black;
return;
}