-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsoftwareAllocation_UVA259OJ.cpp
155 lines (121 loc) · 3.02 KB
/
softwareAllocation_UVA259OJ.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
#include <bits/stdc++.h>
using namespace std;
const int n = 40, inf = INT_MAX / 2;
const string trans2 = "sABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789e";
int trans1(char ch)
{
if (ch == 's')
{
return (1 - 1);
}
else if (ch == 'e')
{
return 37;
}
else if ('A' <= ch && ch <= 'Z')
{
return (ch - 'A') + 1;
}
else
{
return (ch - '0') + 27;
}
}
int solve(int start, int end, vector<bool> & visit, vector<char> & computer, vector<vector<int>> & a)
{
int n = a.size() - 1;
queue<int> q;
vector<int> f(n + 1, -1);
q.push(start);
while (!q.empty())
{
int u = q.front();
q.pop();
if (u == end)
{
int _min = INT_MAX;
int now = end;
while (now != start)
{
_min = min(_min, a[f[now]][now]);
if ('0' <= trans2[now] && trans2[now] <= '9')
{
computer[(trans2[now] - '0')] = trans2[f[now]];
}
now = f[now];
}
now = end;
while (now != start)
{
a[now][f[now]] += _min;
a[f[now]][now] -= _min;
now = f[now];
}
return _min;
}
for (int v = 0; v <= 37; ++v)
{
if (a[u][v] <= 0 || visit[v] == true) continue;
visit[v] = true;
f[v] = u;
q.push(v);
}
}
return -1;
}
int main()
{
ios_base::sync_with_stdio(false);
std::cin.tie(NULL);
while (true)
{
bool _end = true;
int total = 0;
vector<vector<int>> a(n, vector<int>(n, 0));
while (true)
{
string in = "";
getline(cin, in);
if (in == "") break;
_end = false;
a[trans1('s')][trans1(in[0])] = (in[1] - '0');
total += (in[1] - '0');
for (int i = 3; in[i] != ';'; ++i)
{
a[trans1(in[0])][trans1(in[i])] = inf;
a[trans1(in[i])][trans1('e')] = 1;
}
}
if (_end == true) break;
int ans = 0;
vector<bool> visit(n, false);
vector<char> computer(n, '_');
while (true)
{
int res = solve(trans1('s'), trans1('e'), visit, computer, a);
if (res == -1)
{
break;
}
ans += res;
for (int i = 1; i <= n; ++i)
{
visit[i] = false;
}
}
if (ans < total)
{
cout << "!\n";
}
else
{
for (int i = 0; i <= 9; ++i)
{
cout << computer[i];
}
cout << '\n';
}
}
cout.flush();
return 0;
}