# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
598326 | Hanksburger | Mecho (IOI09_mecho) | C++17 | 353 ms | 39140 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
vector<int> adj[640005], bee;
queue<pair<int, int> > qq;
char a[640005];
bool b[640005];
int d[640005];
queue<int> q;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, p, s, e, l=-1, r=1e6;
cin >> n >> p;
for (int i=0; i<n*n; i++)
{
cin >> a[i];
if (a[i]=='M')
s=i;
else if (a[i]=='D')
e=i;
else if (a[i]=='H')
bee.push_back(i);
if (a[i]!='T')
{
a[i]='G';
if (i>=n && a[i-n]=='G')
{
adj[i].push_back(i-n);
adj[i-n].push_back(i);
}
if (i%n && a[i-1]=='G')
{
adj[i].push_back(i-1);
adj[i-1].push_back(i);
}
}
}
for (int u:bee)
{
b[u]=1;
q.push(u);
}
while (!q.empty())
{
int u=q.front();
q.pop();
for (int v:adj[u])
{
if (!b[v] && v!=e)
{
d[v]=d[u]+1;
b[v]=1;
q.push(v);
}
}
}
while (l<r)
{
int m=(l+r+1)/2;
for (int i=0; i<n*n; i++)
b[i]=0;
qq.push({s, 0});
b[s]=1;
bool ok=0;
while (!qq.empty())
{
int u=qq.front().first, w=qq.front().second;
qq.pop();
for (int v:adj[u])
{
if (v==e)
{
ok=1;
break;
}
if (!b[v] && w+1<(d[v]-m)*p)
{
b[v]=1;
qq.push({v, w+1});
}
}
if (ok)
while (!qq.empty())
qq.pop();
}
if (ok)
l=m;
else
r=m-1;
}
cout << l;
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |