# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
790974 |
2023-07-23T10:18:56 Z |
Gray |
Mecho (IOI09_mecho) |
C++17 |
|
181 ms |
11320 KB |
// Source: https://usaco.guide/general/io
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define ln "\n"
#define ff first
#define ss second
#define INF 2e9
using namespace std;
void solve(){
ll n, s; cin >> n >> s;
vector<vector<char>> gr(n, vector<char>(n));
vector<pair<ll, ll>> hvs;
pair<ll, ll> bcor, hcor;
for (ll i=0; i<n; i++){
for (ll j=0; j<n; j++){
cin >> gr[i][j];
if (gr[i][j]=='H'){
hvs.push_back({i, j});
}else if (gr[i][j]=='M'){
bcor = {i, j};
}else if (gr[i][j]=='D'){
hcor = {i, j};
}
}
}
vector<vector<ll>> hpath(n, vector<ll>(n, INF));
//Generate HPATH in O(N^2)
queue<pair<ll, ll>> q;
for (auto hive:hvs){
hpath[hive.ff][hive.ss]=0;
q.push({hive.ff, hive.ss});
}
while (!q.empty()){
pair<ll, ll> hive = q.front();
q.pop();
long long xsh, ysh;
xsh=1; ysh=0;
if ((hive.ff+xsh<n and hive.ff+xsh>=0) and hpath[hive.ff+xsh][hive.ss+ysh]==INF and (hive.ss+ysh<n and hive.ss+ysh>=0) and (gr[hive.ff+xsh][hive.ss+ysh]=='G' or gr[hive.ff+xsh][hive.ss+ysh]=='M')){
q.push({hive.ff+xsh, hive.ss+ysh});
hpath[hive.ff+xsh][hive.ss+ysh]=hpath[hive.ff][hive.ss]+1;
}
xsh=-1, ysh=0;
if ((hive.ff+xsh<n and hive.ff+xsh>=0) and hpath[hive.ff+xsh][hive.ss+ysh]==INF and (hive.ss+ysh<n and hive.ss+ysh>=0) and (gr[hive.ff+xsh][hive.ss+ysh]=='G' or gr[hive.ff+xsh][hive.ss+ysh]=='M')){
q.push({hive.ff+xsh, hive.ss+ysh});
hpath[hive.ff+xsh][hive.ss+ysh]=hpath[hive.ff][hive.ss]+1;
}
xsh=0; ysh=1;
if ((hive.ff+xsh<n and hive.ff+xsh>=0) and hpath[hive.ff+xsh][hive.ss+ysh]==INF and (hive.ss+ysh<n and hive.ss+ysh>=0) and (gr[hive.ff+xsh][hive.ss+ysh]=='G' or gr[hive.ff+xsh][hive.ss+ysh]=='M')){
q.push({hive.ff+xsh, hive.ss+ysh});
hpath[hive.ff+xsh][hive.ss+ysh]=hpath[hive.ff][hive.ss]+1;
}
xsh=0; ysh=-1;
if ((hive.ff+xsh<n and hive.ff+xsh>=0) and hpath[hive.ff+xsh][hive.ss+ysh]==INF and (hive.ss+ysh<n and hive.ss+ysh>=0) and (gr[hive.ff+xsh][hive.ss+ysh]=='G' or gr[hive.ff+xsh][hive.ss+ysh]=='M')){
q.push({hive.ff+xsh, hive.ss+ysh});
hpath[hive.ff+xsh][hive.ss+ysh]=hpath[hive.ff][hive.ss]+1;
}
}
auto bfs2 = [&](ll wt) -> bool{
pair<ll, ll> cord = bcor;
if (hpath[cord.ff][cord.ss]-1<wt) {
// cout << "Fail at start: " << hpath[cord.ff][cord.ss] << " <: " << cord.ff << "," << cord.ss << " -> " << wt << ln;
return 0;
}
vector<vector<ll>> vi(n, vector<ll>(n, INF));
queue<pair<ll, ll>> q;
q.push(cord);
vi[cord.ff][cord.ss]=wt;
while (!q.empty()){
pair<ll, ll> cur = q.front();
q.pop();
// cout << "(" << cur.ss << "," << cur.ff << ") -> hpath=" << hpath[cur.ff][cur.ss] << "; mdist=" << vi[cur.ff][cur.ss]-wt << "; enters at=" << (((vi[cur.ff][cur.ss]-wt)/(ld)s)+wt) << "; " << " bee comes in=" << hpath[cur.ff][cur.ss] << ln;
if (cur.ff>0 and vi[cur.ff-1][cur.ss]==INF and gr[cur.ff-1][cur.ss]!='T' and gr[cur.ff-1][cur.ss]!='H' and (((vi[cur.ff][cur.ss]-wt+1)/s)+wt)<(hpath[cur.ff-1][cur.ss])){
// cout << (hpath[cur.ff-1][cur.ss]-(vi[cur.ff][cur.ss]-wt)/s-1) << " <- " << cur.ss << "," << cur.ff << " to U: cmx " << vi[cur.ff][cur.ss] << " Dist from bear: " << vi[cur.ff][cur.ss]-wt+1 << ln;
vi[cur.ff-1][cur.ss]=vi[cur.ff][cur.ss]+1;
q.push({cur.ff-1, cur.ss});
}
if (cur.ss>0 and vi[cur.ff][cur.ss-1]==INF and gr[cur.ff][cur.ss-1]!='T' and gr[cur.ff][cur.ss-1]!='H' and (((vi[cur.ff][cur.ss]-wt+1)/s)+wt)<(hpath[cur.ff][cur.ss-1])){
// cout << (hpath[cur.ff][cur.ss-1]-(vi[cur.ff][cur.ss]-wt)/s-1) << " <- " << cur.ss << "," << cur.ff << " to L: cmx " << vi[cur.ff][cur.ss] << " Dist from bear: " << vi[cur.ff][cur.ss]-wt+1 << ln;
vi[cur.ff][cur.ss-1]=vi[cur.ff][cur.ss]+1;
q.push({cur.ff, cur.ss-1});
}
if (cur.ff+1<n and vi[cur.ff+1][cur.ss]==INF and gr[cur.ff+1][cur.ss]!='T' and gr[cur.ff+1][cur.ss]!='H' and (((vi[cur.ff][cur.ss]-wt+1)/s)+wt)<(hpath[cur.ff+1][cur.ss])){
// cout << (hpath[cur.ff+1][cur.ss]-(vi[cur.ff][cur.ss]-wt)/s-1) << " <- " << cur.ss << "," << cur.ff << " to D: cmx " << vi[cur.ff][cur.ss] << " Dist from bear: " << vi[cur.ff][cur.ss]-wt+1 << ln;
vi[cur.ff+1][cur.ss]=vi[cur.ff][cur.ss]+1;
q.push({cur.ff+1, cur.ss});
}
if (cur.ss+1<n and vi[cur.ff][cur.ss+1]==INF and gr[cur.ff][cur.ss+1]!='T' and gr[cur.ff][cur.ss+1]!='H' and (((vi[cur.ff][cur.ss]-wt+1)/s)+wt)<(hpath[cur.ff][cur.ss+1])){
// cout << (hpath[cur.ff][cur.ss+1]-(vi[cur.ff][cur.ss]+1-wt)/s-1) << " <- " << cur.ss << "," << cur.ff << " to R: cmx " << vi[cur.ff][cur.ss] << " Dist from bear: " << vi[cur.ff][cur.ss]-wt+1 << ln;
vi[cur.ff][cur.ss+1]=vi[cur.ff][cur.ss]+1;
q.push({cur.ff, cur.ss+1});
}
}
if (vi[hcor.ff][hcor.ss]<INF) return 1;
else return 0;
};
ll l=-1, r=n*n+1;
while (l+1<r){
ll mid = (l+r)/2;
if (bfs2(mid)) {
l=mid;
// cout << mid << " with T" << ln;
}
else {
r=mid;
// cout << mid << " with F" << ln;
}
}
cout << l << ln;
}
int main() {
ll t=1;
// cin >> t;
while (t--) solve();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
76 ms |
11216 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
304 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
300 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
304 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
304 KB |
Output is correct |
28 |
Correct |
1 ms |
304 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
10 ms |
2492 KB |
Output is correct |
34 |
Correct |
9 ms |
2492 KB |
Output is correct |
35 |
Correct |
19 ms |
2612 KB |
Output is correct |
36 |
Correct |
15 ms |
3136 KB |
Output is correct |
37 |
Correct |
13 ms |
3124 KB |
Output is correct |
38 |
Correct |
22 ms |
3140 KB |
Output is correct |
39 |
Correct |
15 ms |
3860 KB |
Output is correct |
40 |
Correct |
15 ms |
3796 KB |
Output is correct |
41 |
Correct |
32 ms |
3884 KB |
Output is correct |
42 |
Correct |
20 ms |
4644 KB |
Output is correct |
43 |
Correct |
19 ms |
4668 KB |
Output is correct |
44 |
Correct |
37 ms |
4668 KB |
Output is correct |
45 |
Correct |
26 ms |
5544 KB |
Output is correct |
46 |
Correct |
24 ms |
5524 KB |
Output is correct |
47 |
Correct |
43 ms |
5500 KB |
Output is correct |
48 |
Correct |
28 ms |
6492 KB |
Output is correct |
49 |
Correct |
27 ms |
6476 KB |
Output is correct |
50 |
Correct |
63 ms |
6500 KB |
Output is correct |
51 |
Correct |
33 ms |
7588 KB |
Output is correct |
52 |
Correct |
30 ms |
7504 KB |
Output is correct |
53 |
Correct |
70 ms |
7536 KB |
Output is correct |
54 |
Correct |
44 ms |
8828 KB |
Output is correct |
55 |
Correct |
36 ms |
8696 KB |
Output is correct |
56 |
Correct |
92 ms |
8672 KB |
Output is correct |
57 |
Correct |
45 ms |
9932 KB |
Output is correct |
58 |
Correct |
46 ms |
9960 KB |
Output is correct |
59 |
Correct |
86 ms |
9888 KB |
Output is correct |
60 |
Correct |
51 ms |
11280 KB |
Output is correct |
61 |
Correct |
46 ms |
11244 KB |
Output is correct |
62 |
Correct |
111 ms |
11176 KB |
Output is correct |
63 |
Correct |
107 ms |
11184 KB |
Output is correct |
64 |
Correct |
181 ms |
11196 KB |
Output is correct |
65 |
Correct |
149 ms |
11196 KB |
Output is correct |
66 |
Correct |
119 ms |
11196 KB |
Output is correct |
67 |
Correct |
123 ms |
11196 KB |
Output is correct |
68 |
Correct |
61 ms |
11216 KB |
Output is correct |
69 |
Correct |
72 ms |
11308 KB |
Output is correct |
70 |
Correct |
54 ms |
11272 KB |
Output is correct |
71 |
Correct |
61 ms |
11248 KB |
Output is correct |
72 |
Correct |
53 ms |
11212 KB |
Output is correct |
73 |
Correct |
41 ms |
11248 KB |
Output is correct |
74 |
Correct |
61 ms |
11280 KB |
Output is correct |
75 |
Correct |
68 ms |
11316 KB |
Output is correct |
76 |
Correct |
95 ms |
11264 KB |
Output is correct |
77 |
Correct |
63 ms |
11224 KB |
Output is correct |
78 |
Correct |
72 ms |
11300 KB |
Output is correct |
79 |
Correct |
57 ms |
11276 KB |
Output is correct |
80 |
Correct |
63 ms |
11268 KB |
Output is correct |
81 |
Correct |
70 ms |
11320 KB |
Output is correct |
82 |
Correct |
64 ms |
11272 KB |
Output is correct |
83 |
Correct |
74 ms |
11256 KB |
Output is correct |
84 |
Correct |
74 ms |
11272 KB |
Output is correct |
85 |
Correct |
80 ms |
11248 KB |
Output is correct |
86 |
Correct |
72 ms |
11256 KB |
Output is correct |
87 |
Correct |
72 ms |
11280 KB |
Output is correct |
88 |
Correct |
77 ms |
11244 KB |
Output is correct |
89 |
Correct |
73 ms |
11224 KB |
Output is correct |
90 |
Correct |
75 ms |
11228 KB |
Output is correct |
91 |
Correct |
80 ms |
11244 KB |
Output is correct |
92 |
Correct |
98 ms |
11292 KB |
Output is correct |