Submission #956821

# Submission time Handle Problem Language Result Execution time Memory
956821 2024-04-02T14:08:12 Z DeltaStruct Mecho (IOI09_mecho) C++17
84 / 100
181 ms 11816 KB
#include <bits/stdc++.h>
using namespace std;
 
int main(){
#define int long long
  int n,m,inf=numeric_limits<int>::max(); cin >> n >> m; int sx,sy,gx,gy;
  vector<string> A(n); for (auto& a:A) cin >> a;
  queue<pair<int,int>> que; vector hd(n,vector<int>(n,inf));
  for (int i(0);i < n;++i) for (int k(0);k < n;++k){
    if (A[i][k]=='M') sx=i,sy=k;
    if (A[i][k]=='D') gx=i,gy=k;
    if (A[i][k]=='H') que.emplace(i,k),hd[i][k] = 0;
  }
  bool psf = true; auto can = [&psf,&A,&n](int x,int y){
    if (x==-1||x==n||y==-1||y==n) return false;
    if (A[x][y]=='T') return false;
    if (psf&&A[x][y]=='D') return false;
    return true;
  };
  while(!que.empty()){
    auto [i,k] = que.front(); que.pop();
    if (can(i+1,k)&&hd[i+1][k]==inf) hd[i+1][k]=hd[i][k]+m,que.emplace(i+1,k);
    if (can(i-1,k)&&hd[i-1][k]==inf) hd[i-1][k]=hd[i][k]+m,que.emplace(i-1,k);
    if (can(i,k+1)&&hd[i][k+1]==inf) hd[i][k+1]=hd[i][k]+m,que.emplace(i,k+1);
    if (can(i,k-1)&&hd[i][k-1]==inf) hd[i][k-1]=hd[i][k]+m,que.emplace(i,k-1);
  }
  psf = false; int l = -1,r = 1e9,mid;
  while(abs(l-r)>1){
    mid = (l+r)/2;
    vector dist(n,vector<int>(n,inf)); dist[sx][sy] = mid*m,que.emplace(sx,sy);
    while(!que.empty()){
      auto [i,k] = que.front(); que.pop();
      if (can(i+1,k)&&dist[i][k]+1<hd[i+1][k]&&dist[i+1][k]==inf) dist[i+1][k] = dist[i][k]+1,que.emplace(i+1,k);
      if (can(i-1,k)&&dist[i][k]+1<hd[i-1][k]&&dist[i-1][k]==inf) dist[i-1][k] = dist[i][k]+1,que.emplace(i-1,k);
      if (can(i,k+1)&&dist[i][k]+1<hd[i][k+1]&&dist[i][k+1]==inf) dist[i][k+1] = dist[i][k]+1,que.emplace(i,k+1);
      if (can(i,k-1)&&dist[i][k]+1<hd[i][k-1]&&dist[i][k-1]==inf) dist[i][k-1] = dist[i][k]+1,que.emplace(i,k-1);
    }
    if (dist[gx][gy]==inf) r = mid;
    else l = mid;
  }
  cout << l << endl;
}

Compilation message

mecho.cpp: In function 'int main()':
mecho.cpp:38:20: warning: 'gy' may be used uninitialized in this function [-Wmaybe-uninitialized]
   38 |     if (dist[gx][gy]==inf) r = mid;
      |                    ^
mecho.cpp:38:16: warning: 'gx' may be used uninitialized in this function [-Wmaybe-uninitialized]
   38 |     if (dist[gx][gy]==inf) r = mid;
      |                ^
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 111 ms 11408 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 344 KB Output is correct
12 Incorrect 0 ms 348 KB Output isn't correct
13 Incorrect 1 ms 348 KB Output isn't correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 1 ms 600 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
30 Correct 1 ms 432 KB Output is correct
31 Correct 1 ms 348 KB Output is correct
32 Correct 1 ms 348 KB Output is correct
33 Correct 12 ms 2560 KB Output is correct
34 Correct 12 ms 2632 KB Output is correct
35 Correct 22 ms 2556 KB Output is correct
36 Correct 14 ms 3280 KB Output is correct
37 Correct 14 ms 3244 KB Output is correct
38 Correct 30 ms 3292 KB Output is correct
39 Correct 19 ms 3960 KB Output is correct
40 Correct 19 ms 3864 KB Output is correct
41 Correct 38 ms 4152 KB Output is correct
42 Correct 23 ms 4980 KB Output is correct
43 Correct 21 ms 4964 KB Output is correct
44 Correct 49 ms 4924 KB Output is correct
45 Correct 28 ms 5844 KB Output is correct
46 Correct 26 ms 6096 KB Output is correct
47 Correct 61 ms 5924 KB Output is correct
48 Correct 34 ms 6788 KB Output is correct
49 Correct 33 ms 6784 KB Output is correct
50 Correct 72 ms 6780 KB Output is correct
51 Correct 39 ms 7888 KB Output is correct
52 Correct 39 ms 7784 KB Output is correct
53 Correct 99 ms 7976 KB Output is correct
54 Correct 47 ms 8936 KB Output is correct
55 Correct 45 ms 9000 KB Output is correct
56 Correct 111 ms 8980 KB Output is correct
57 Correct 51 ms 10176 KB Output is correct
58 Correct 56 ms 10140 KB Output is correct
59 Correct 139 ms 10164 KB Output is correct
60 Correct 62 ms 11416 KB Output is correct
61 Correct 59 ms 11484 KB Output is correct
62 Correct 158 ms 11428 KB Output is correct
63 Correct 130 ms 11488 KB Output is correct
64 Correct 181 ms 11456 KB Output is correct
65 Correct 181 ms 11492 KB Output is correct
66 Correct 138 ms 11444 KB Output is correct
67 Correct 124 ms 11416 KB Output is correct
68 Correct 85 ms 11464 KB Output is correct
69 Correct 81 ms 11460 KB Output is correct
70 Correct 72 ms 11664 KB Output is correct
71 Correct 71 ms 11552 KB Output is correct
72 Incorrect 69 ms 11536 KB Output isn't correct
73 Incorrect 64 ms 11556 KB Output isn't correct
74 Correct 88 ms 11428 KB Output is correct
75 Correct 99 ms 11816 KB Output is correct
76 Correct 88 ms 11492 KB Output is correct
77 Correct 89 ms 11440 KB Output is correct
78 Correct 90 ms 11548 KB Output is correct
79 Correct 95 ms 11484 KB Output is correct
80 Correct 86 ms 11524 KB Output is correct
81 Correct 95 ms 11484 KB Output is correct
82 Correct 84 ms 11440 KB Output is correct
83 Correct 103 ms 11464 KB Output is correct
84 Correct 96 ms 11492 KB Output is correct
85 Correct 94 ms 11436 KB Output is correct
86 Correct 114 ms 11592 KB Output is correct
87 Correct 97 ms 11492 KB Output is correct
88 Correct 124 ms 11720 KB Output is correct
89 Correct 104 ms 11440 KB Output is correct
90 Correct 118 ms 11452 KB Output is correct
91 Correct 134 ms 11420 KB Output is correct
92 Correct 123 ms 11676 KB Output is correct