-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfromDuskTillDawn_UVA10187.cpp
212 lines (181 loc) · 5.3 KB
/
fromDuskTillDawn_UVA10187.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
#include <bits/stdc++.h>
using namespace std;
ifstream fin("fromDuskTillDawn_UVA10187.in");
ofstream fout("fromDuskTillDawn_UVA10187.out");
const int N = 100, inf = INT_MAX / 2;
struct edge
{
int t1;
int t2;
};
edge _edge(int t1, int t2)
{
edge temp{t1, t2}; return temp;
}
struct segment
{
int u;
int t;
int b;
bool operator < (const segment & temp) const
{
return b > temp.b;
}
};
segment _segment(int u, int t, int b)
{
segment temp{u, t, b}; return temp;
}
int getNum(int & n, string str, map<string, int> & ston, vector<string> & ntos)
{
if (ston.count(str) == 0)
{
ston[str] = n; ++n;
ntos.push_back(str);
}
return ston[str];
}
int getDis(int t1, int t2)
{
if (t1 <= t2)
{
return t2 - t1;
}
else
{
return t2 - t1 + 24;
}
}
int main()
{
int tcc; fin >> tcc;
for (int t = 1; t <= tcc; ++t)
{
int n = 0, m; fin >> m;
map<string, int> ston;
vector<string> ntos;
vector<vector<int>> g(N + 1);
vector<vector<vector<edge>>> a(N + 1, vector<vector<edge>>(N + 1));
for (int c = 1; c <= m; ++c)
{
string u_s, v_s; fin >> u_s >> v_s;
int u = getNum(n, u_s, ston, ntos), v = getNum(n, v_s, ston, ntos);
int t1, tp, t2; fin >> t1 >> tp;
t2 = (t1 + tp) % 24;
bool ok = false;
if (tp > 12)
{
ok = false;
}
else if (t1 >= 12 && t2 <= 12)
{
if (t1 >= 18 && t2 <= 6)
{
ok = true;
}
}
else if ((t1 >= 18 || t2 <= 6) && t1 <= t2)
{
ok = true;
}
if (ok == true)
{
if (t1 == 24) t1 = 0;
if (t2 == 24) t2 = 0;
if (a[u][v].size() == 0)
{
g[u].push_back(v);
}
a[u][v].push_back(_edge(t1, t2));
}
}
string s_s, e_s; fin >> s_s >> e_s;
int s = getNum(n, s_s, ston, ntos), e = getNum(n, e_s, ston, ntos);
if (t == 24)
{
for (int __s = 0; __s == 0; ++__s);
}
// fout << "START: " << s << " END: " << e << '\n';
// for (int u = 0; u <= n - 1; ++u)
// for (int v = 0; v <= n - 1; ++v)
// if (a[u][v].size() > 0) fout << u << ' ' << v << '\n';
// for (int u = 0; u <= n - 1; ++u)
// {
// for (int v = 0; v <= n - 1; ++v)
// {
// if (a[u][v].size() > 0)
// {
// fout << u << "-->" << v << ": ";
// int size = a[u][v].size();
// for (int i = 0; i <= size - 1; ++i)
// {
// if (i > 0) fout << "; ";
// fout << a[u][v][i].t1 << "-" << a[u][v][i].t2;
// }
// fout << '\n';
// }
// }
// }
bool isFirst = true;
int ans = -1;
vector<vector<int>> d(n, vector<int>(24, inf));
priority_queue<segment> pq; pq.push(_segment(s, 0, 0));
while (pq.empty() == false)
{
segment now = pq.top(); pq.pop();
if (isFirst == false && now.b >= d[now.u][now.t]) continue;
d[now.u][now.t] = now.b;
if (now.u == 3)
{
for (int __s = 0; __s == 0; ++__s);
}
if (now.u == e)
{
ans = now.b;
break;
}
int size0 = g[now.u].size();
for (int i = 0; i <= size0 - 1; ++i)
{
int v = g[now.u][i];
int size1 = a[now.u][v].size();
for (int j = 0; j <= size1 - 1; ++j)
{
edge nowEdge = a[now.u][v][j];
segment next;
next.u = v;
next.t = nowEdge.t2;
if (isFirst == true)
{
next.b = now.b;
}
else
{
bool find12 = 0;
for (int _t = now.t; _t != nowEdge.t1; _t = (_t + 1) % 24)
{
if (_t == 12)
{
find12 = 1;
break;
}
}
next.b = now.b + find12;
}
pq.push(next);
}
}
isFirst = false;
}
fout << "Test Case " << t << ".\n";
if (ans == -1)
{
fout << "There is no route Vladimir can take.\n";
}
else
{
fout << "Vladimir needs " << ans << " litre(s) of blood.\n";
}
}
return 0;
}