Submission #472735

# Submission time Handle Problem Language Result Execution time Memory
472735 2021-09-14T08:56:52 Z hackerbhaiya Mecho (IOI09_mecho) C++17
95 / 100
561 ms 11304 KB
#include<bits/stdc++.h>
// #pragma GCC optimize("Ofast")
// #pragma GCC target("avx,avx2,fma")
// #pragma GCC optimization("unroll-loops")
// #pragma GCC optimize("unroll-loops")
// #pragma GCC optimize("fast-math")
// #pragma GCC optimize("no-stack-protector")
// #define ll __int128
#define ll long long
// #define ll int
#define f(i,a,b) for(int i=a;i<b;i++)
#define mod 1000000007
// #define mod 998244353 
#define mp make_pair
#define uniq(v) (v).erase(unique(all(v)),(v).end())
#define ff first
#define ss second
#define rf(i,a,b) for(int i=a;i>=b;i--)
#define sc(a) scanf("%lld",&a)
#define pf printf
#define sz(a) (int)(a.size())
#define psf push_front
#define ppf pop_front
#define ppb pop_back
#define pb push_back
#define pq priority_queue
#define all(s) s.begin(),s.end()
#define sp(a) setprecision(a)
#define rz resize
#define ld long double
#define inf (ll)1e18
#define ub upper_bound
#define lb lower_bound
#define bs binary_search
#define eb emplace_back
const double pi = acos(-1);
ll binpow(ll a, ll b){ll res=1;while(b!=0){if(b&1)res*=a;a*=a;b>>=1;}return res;}
// ll binpow(ll a, ll b, ll md){ll res=1;a%=mod;while(b!=0){if(b&1)res*=a,res%=md;a*=a,a%=md;b>>=1;}return res%md;}
 
using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    // freopen("xortransform.in","r",stdin);
    // freopen("xortransform.out","w",stdout);
// #ifndef ONLINE_JUDGE
//     freopen("input.txt","r",stdin);
//     freopen("output.txt","w",stdout);
// #endif
    int z=1;
    // cin>>z;
    f(i,1,z+1)
    {
        //cout<<"Case #"<<i<<": ";
        ll n,d,sx,sy,fx,fy;
        int dx[]={0,1,0,-1};
        int dy[]={1,0,-1,0};
        cin>>n>>d;
        vector<string> s(n);
        f(i,0,n)
            cin>>s[i];
        ll l=0,r=n*n+1;
        queue<vector<ll> > q;
        vector<vector<ll> > dis(n,vector<ll> (n,inf));
        f(i,0,n)
        {
            f(j,0,n)
            {
                if(s[i][j]=='H')
                    q.push({i,j}),dis[i][j]=0;
                else if(s[i][j]=='M')
                    sx=i,sy=j;
                else if(s[i][j]=='D')
                    fx=i,fy=j;
            }
        }
        while(!q.empty())
        {
            auto temp=q.front();
            q.pop();
            ll x=temp[0],y=temp[1];
            f(i,0,4)
            {
                ll xx=x+dx[i],yy=y+dy[i];
                if(xx>=0 && yy>=0 && xx<n && yy<n && s[xx][yy]!='T' && dis[xx][yy]>d+dis[x][y])
                    dis[xx][yy]=d+dis[x][y],q.push({xx,yy});             
            }
        }
        while(l<=r)
        {
            ll mid=(l+r)/2;
            queue<vector<ll> > q;
            vector<vector<ll> > dis2(n,vector<ll> (n,inf));
            if(dis[sx][sy]>mid*d)
                q.push({sx,sy});
            dis2[sx][sy]=mid*d;
            while(!q.empty())
            {
                auto temp=q.front();
                q.pop();
                ll x=temp[0],y=temp[1];
                if(x==fx && y==fy)
                    break;
                f(i,0,4)
                {
                    ll xx=x+dx[i],yy=y+dy[i];
                    if(xx>=0 && yy>=0 && xx<n && yy<n && s[xx][yy]!='T' && dis2[xx][yy]>1+dis2[x][y])
                    {
                        if((xx==fx && yy==fy) || (1+dis2[x][y]<dis[xx][yy]))
                            dis2[xx][yy]=1+dis2[x][y],q.push({xx,yy});       
                    }
                }
            }
            if(dis2[fx][fy]<inf)
                l=mid+1;
            else
                r=mid-1;
        }
        cout<<r<<"\n";
    }
} 

Compilation message

mecho.cpp: In function 'int main()':
mecho.cpp:116:27: warning: 'fy' may be used uninitialized in this function [-Wmaybe-uninitialized]
  116 |             if(dis2[fx][fy]<inf)
      |                           ^
mecho.cpp:116:23: warning: 'fx' may be used uninitialized in this function [-Wmaybe-uninitialized]
  116 |             if(dis2[fx][fy]<inf)
      |                       ^
mecho.cpp:96:26: warning: 'sy' may be used uninitialized in this function [-Wmaybe-uninitialized]
   96 |             if(dis[sx][sy]>mid*d)
      |                          ^
mecho.cpp:96:22: warning: 'sx' may be used uninitialized in this function [-Wmaybe-uninitialized]
   96 |             if(dis[sx][sy]>mid*d)
      |                      ^
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 320 ms 11264 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 0 ms 204 KB Output is correct
12 Incorrect 1 ms 332 KB Output isn't correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 2 ms 332 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 1 ms 332 KB Output is correct
22 Correct 1 ms 332 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 1 ms 332 KB Output is correct
25 Correct 1 ms 332 KB Output is correct
26 Correct 1 ms 332 KB Output is correct
27 Correct 1 ms 332 KB Output is correct
28 Correct 1 ms 332 KB Output is correct
29 Correct 1 ms 332 KB Output is correct
30 Correct 1 ms 332 KB Output is correct
31 Correct 1 ms 332 KB Output is correct
32 Correct 1 ms 332 KB Output is correct
33 Correct 13 ms 2392 KB Output is correct
34 Correct 13 ms 2460 KB Output is correct
35 Correct 71 ms 2412 KB Output is correct
36 Correct 16 ms 3076 KB Output is correct
37 Correct 24 ms 3056 KB Output is correct
38 Correct 97 ms 3100 KB Output is correct
39 Correct 22 ms 3800 KB Output is correct
40 Correct 23 ms 3772 KB Output is correct
41 Correct 156 ms 3796 KB Output is correct
42 Correct 26 ms 4580 KB Output is correct
43 Correct 28 ms 4604 KB Output is correct
44 Correct 182 ms 4580 KB Output is correct
45 Correct 33 ms 5440 KB Output is correct
46 Correct 34 ms 5452 KB Output is correct
47 Correct 190 ms 5452 KB Output is correct
48 Correct 37 ms 6424 KB Output is correct
49 Correct 40 ms 6436 KB Output is correct
50 Correct 237 ms 6456 KB Output is correct
51 Correct 46 ms 7556 KB Output is correct
52 Correct 50 ms 7520 KB Output is correct
53 Correct 284 ms 7460 KB Output is correct
54 Correct 52 ms 8688 KB Output is correct
55 Correct 55 ms 8688 KB Output is correct
56 Correct 369 ms 8592 KB Output is correct
57 Correct 60 ms 9880 KB Output is correct
58 Correct 65 ms 9988 KB Output is correct
59 Correct 407 ms 9920 KB Output is correct
60 Correct 79 ms 11196 KB Output is correct
61 Correct 74 ms 11244 KB Output is correct
62 Correct 451 ms 11252 KB Output is correct
63 Correct 305 ms 11276 KB Output is correct
64 Correct 561 ms 11188 KB Output is correct
65 Correct 424 ms 11168 KB Output is correct
66 Correct 353 ms 11168 KB Output is correct
67 Correct 331 ms 11232 KB Output is correct
68 Correct 152 ms 11188 KB Output is correct
69 Correct 159 ms 11176 KB Output is correct
70 Correct 123 ms 11260 KB Output is correct
71 Correct 129 ms 11252 KB Output is correct
72 Correct 107 ms 11196 KB Output is correct
73 Correct 131 ms 11292 KB Output is correct
74 Correct 240 ms 11272 KB Output is correct
75 Correct 271 ms 11220 KB Output is correct
76 Correct 240 ms 11304 KB Output is correct
77 Correct 244 ms 11172 KB Output is correct
78 Correct 321 ms 11168 KB Output is correct
79 Correct 197 ms 11212 KB Output is correct
80 Correct 238 ms 11220 KB Output is correct
81 Correct 270 ms 11196 KB Output is correct
82 Correct 219 ms 11136 KB Output is correct
83 Correct 299 ms 11224 KB Output is correct
84 Correct 261 ms 11216 KB Output is correct
85 Correct 294 ms 11216 KB Output is correct
86 Correct 269 ms 11180 KB Output is correct
87 Correct 276 ms 11224 KB Output is correct
88 Correct 298 ms 11224 KB Output is correct
89 Correct 297 ms 11208 KB Output is correct
90 Correct 309 ms 11184 KB Output is correct
91 Correct 349 ms 11204 KB Output is correct
92 Correct 354 ms 11184 KB Output is correct