forked from sitz/UVa-Online-Judge
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path1006.cpp
220 lines (202 loc) · 3.2 KB
/
1006.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
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 500;
const int INF = INT_MAX;
struct edge
{
int u, v, wt;
};
struct node
{
int S, T;
bool operator<(const node &rhs) const
{
return S < rhs.S;
}
};
int n, m;
int Size[MAXN + 10];
vector<node> A[MAXN + 10];
int res[MAXN + 10];
int work_time(vector<node> &pro, int blo)
{
if (pro.size() == 0)
{
return 0;
}
if (Size[blo] < pro[0].S)
{
return INF;
}
int tmp = upper_bound(pro.begin(), pro.end(), (node){
Size[blo], 0}) -
pro.begin() - 1;
return pro[tmp].T;
}
struct KM_algorithm
{
vector<int> G[MAXN + 10];
vector<edge> Edge;
int Lx[MAXN + 10], Ly[MAXN + 10], Slack[MAXN + 10];
int nx, ny, Left[MAXN + 10];
bool S[MAXN + 10], T[MAXN + 10];
void init(int nx, int ny)
{
this->nx = nx, this->ny = ny;
for (int u = 1; u <= nx; u++)
{
G[u].clear();
}
Edge.clear();
}
void Addedge(int u, int v, int wt)
{
Edge.push_back((edge){
u, v, wt});
G[u].push_back(Edge.size() - 1);
}
bool match(int u)
{
S[u] = 1;
for (int i = 0; i < G[u].size(); i++)
{
edge &e = Edge[G[u][i]];
if (!T[e.v])
{
int t = Lx[e.u] + Ly[e.v] - e.wt;
if (t == 0)
{
T[e.v] = 1;
if (!Left[e.v] || match(Left[e.v]))
{
Left[e.v] = e.u;
return 1;
}
}
Slack[e.v] = min(Slack[e.v], t);
}
}
return 0;
}
void Update()
{
int i, a = INF;
for (i = 1; i <= ny; i++)
if (!T[i])
{
a = min(a, Slack[i]);
}
for (i = 1; i <= ny; i++)
{
if (S[i])
{
Lx[i] -= a;
}
if (T[i])
{
Ly[i] += a;
}
else
{
Slack[i] -= a;
}
}
}
int KM()
{
int i, ans = 0;
for (i = 1; i <= ny; i++)
{
Lx[i] = Ly[i] = Left[i] = 0;
}
for (i = 1; i <= nx; i++)
{
memset(Slack, 0x3f, sizeof(Slack));
while (1)
{
memset(S, 0, sizeof(S));
memset(T, 0, sizeof(T));
if (match(i))
{
break;
}
else
{
Update();
}
}
}
for (i = 1; i <= ny; i++)
{
ans += Lx[Left[i]] + Ly[i];
}
return -ans;
}
} sp;
void read_input()
{
sp.init(n, m * n);
for (int i = 1; i <= m; i++)
{
scanf("%d", &Size[i]);
}
for (int i = 1, c; i <= n; i++)
{
scanf("%d", &c);
A[i].clear();
for (int j = 1; j <= c; j++)
{
node t;
scanf("%d%d", &t.S, &t.T);
A[i].push_back(t);
}
for (int j = 1; j <= m; j++)
{
int tmp = work_time(A[i], j);
if (tmp == INF)
{
continue;
}
for (int k = 1; k <= n; k++)
{
sp.Addedge(i, (j - 1) * n + k, -k * tmp);
}
}
}
}
int Start[MAXN + 10];
void work()
{
int ans = sp.KM();
printf("Average turnaround time = %.2lf\n", 1.0 * ans / n);
for (int i = 1; i <= m; i++)
{
for (int j = n; j >= 1; j--)
{
int v = (i - 1) * n + j;
if (sp.Left[v])
{
res[sp.Left[v]] = i;
int Last = j == n ? 0 : sp.Left[v + 1];
int tmp = work_time(A[Last], i);
Start[sp.Left[v]] = tmp + Start[Last];
}
}
}
for (int i = 1; i <= n; i++)
{
printf("Program %d runs in region %d from %d to %d\n", i, res[i], Start[i], Start[i] + work_time(A[i], res[i]));
}
}
int main()
{
int t = 0;
while (scanf("%d%d",&m,&n) == 2 && (n||m))
{
read_input();
printf("Case %d\n", ++t);
work();
puts("");
}
return 0;
}