-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathp management.cpp
72 lines (67 loc) · 1.44 KB
/
p management.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
# include <fstream>
# include <algorithm>
# include <vector>
# include <queue>
# define inf 999999999
using namespace std;
ifstream f("pm2.in");
ofstream g("pm2.out");
vector <int> v[105],V[105];
queue <int> q;
int i,j,n,k,VV,var;
int minn[105],maxx[105],timp[105],in[105],out[105];
void BF ()
{
while (! q.empty())
{
k=q.front(); q.pop();
VV=max(VV, minn[k]+timp[k]);
for (i=0; i<v[k].size(); ++i)
{
--in[v[k][i]];
if (minn[v[k][i]]<minn[k]+timp[k]) minn[v[k][i]]=minn[k]+timp[k];
if (in[v[k][i]]==0) q.push(v[k][i]);
}
}
}
void maxim ()
{
int i;
for (i=1; i<=n; ++i)
if (out[i]==0) q.push(i);
while (! q.empty())
{
k=q.front(); q.pop();
for (i=0; i<V[k].size(); ++i)
{
--out[V[k][i]];
if (maxx[V[k][i]]<maxx[k]+timp[k]) maxx[V[k][i]]=maxx[k]+timp[k];
if (out[V[k][i]]==0) q.push(V[k][i]);
}
}
}
int main ()
{
f>>n;
for (i=1; i<=n; ++i)
f>>timp[i];
for (i=1; i<=n; ++i)
{
f>>k;
for (j=1; j<=k; ++j)
{
f>>var; ++in[i];
v[var].push_back(i);
++out[var];
V[i].push_back(var);
}
}
for (i=1; i<=n; ++i)
if (in[i]==0) q.push(i), minn[i]=0;
BF ();
maxim ();
g<<VV<<"\n";
for (i=1; i<=n; ++i)
g<<minn[i]<<" "<<VV-maxx[i]-timp[i]<<"\n";
return 0;
}