-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMachine-Scheduling.cpp
More file actions
71 lines (70 loc) · 1.89 KB
/
Copy pathMachine-Scheduling.cpp
File metadata and controls
71 lines (70 loc) · 1.89 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
#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
int n,x;
printf("Please enter the number of task and number of machines: ");
cin>>n>>x;
int task[n+1];
cout<<"Please enter the starting time for "<<n<<" works:\n";
int start[n+1];
for(int i = 0; i < n; i++) task[i] = i;
for(int i = 0; i < n; i++) cin>>start[i];
int finish[n+1];
cout<<"Please enter the finishing time for "<<n<<" works:\n";
for(int i = 0; i < n; i++) cin>>finish[i];
for(int i = 0; i < n; i++)
{
for(int j = i+1; j < n; j++)
{
if(finish[i]>finish[j])
{
swap(task[i],task[j]);
swap(start[i],start[j]);
swap(finish[i],finish[j]);
}
}
}
for(int i = 0; i < n; i++)
{
for(int j = i+1; j < n; j++)
{
if(finish[i]==finish[j]){
if(start[i]>start[j])
{
swap(task[i],task[j]);
swap(start[i],start[j]);
swap(finish[i],finish[j]);
}
}
}
}
int machine[x][n], m[x], state[n], cnt = 0, mfns;
for(int i = 0; i < x; i++) m[i] = 0;
for(int i = 0; i < n; i++) state[i] = 0;
for(int i = 0; i < x; i++)
{
mfns = 0;
for(int j = 0; j < n; j++)
{
if(state[j]==0&&mfns<=start[j])
{
machine[i][m[i]++] = task[j];
state[j] = 1;
mfns = finish[j];
cnt++;
}
}
if(cnt==n) break;
}
cout<<"Solution:\n";
for(int i = 0; i < x; i++)
{
cout<<"Machine "<<i+1<<": ";
for(int j = 0; j < m[i]; j++)
printf("%c ",machine[i][j]+'a');
printf("\n");
}
return 0;
}