답안 #631647

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
631647 2022-08-18T11:21:54 Z Abrar_Al_Samit Soccer (JOI17_soccer) C++17
100 / 100
411 ms 20004 KB
#include<bits/stdc++.h>
using namespace std;

const int MX = 505;
const long long INF = 1e18;

int h, w;
int n; 
long long A, B, C;
long long nearest[MX][MX];
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
//0->down, 1->up, 2->right, 3->left
long long dist[MX][MX][5];
void PlayGround() {
  cin>>h>>w;
  cin>>A>>B>>C;
  cin>>n;

  for(int i=0; i<=h; ++i) {
    for(int j=0; j<=w; ++j) {
      nearest[i][j] = INF;

      for(int k=0; k<5; ++k) {
        dist[i][j][k] = INF;
      }
    }
  }

  auto valid = [&] (int i, int j) {
    return i<=h && i>-1 && j>-1 && j<=w;
  };

  vector<pair<int,int>>oneandn;

  queue<pair<int,int>>q;
  for(int i=0; i<n; ++i) {
    int x, y;
    cin>>x>>y;
    nearest[x][y] = 0;
    q.emplace(x, y);
    if(oneandn.size()==2) oneandn.pop_back();
    oneandn.emplace_back(x, y);
  }

  while(!q.empty()) {
    int x, y;
    tie(x, y) = q.front(); q.pop();
    for(int j=0; j<4; ++j) {
      int newx = x + dx[j], newy = y + dy[j];
      if(valid(newx, newy)) {
        if(nearest[newx][newy]>nearest[x][y]+1) {
          nearest[newx][newy] = nearest[x][y]+1;
          q.emplace(newx, newy);
        }
      }
    }
  }

  dist[oneandn[0].first][oneandn[0].second][0] = 0;
  priority_queue<tuple<long long, int, int, int>>pq;
  pq.emplace(0, oneandn[0].first, oneandn[0].second, 0);
  while(!pq.empty()) {
    long long cost;
    int x, y, dir;
    tie(cost, x, y, dir) = pq.top(); pq.pop();
    cost = -cost;
    if(cost!=dist[x][y][dir]) continue;

    if(dir) {
      int newx = x + dx[dir-1], newy = y + dy[dir-1];
      if(valid(newx, newy)) {
        if(dist[newx][newy][dir]>cost+A) { //continue travelling
          dist[newx][newy][dir] = cost+A;
          pq.emplace(-cost-A, newx, newy, dir);
        }
      }
      if(dist[x][y][0]>cost+nearest[x][y]*C) { //stop and call a player
        dist[x][y][0] = cost+nearest[x][y]*C;
        pq.emplace(-dist[x][y][0], x, y, 0);
      }
    } else {
      for(int j=0; j<4; ++j) {
        int newx = x + dx[j], newy = y + dy[j];
        if(valid(newx, newy)) {
          if(dist[newx][newy][0]>cost+C) { //run
            dist[newx][newy][0] = cost+C;
            pq.emplace(-cost-C, newx, newy, 0);
          }
          if(dist[newx][newy][j+1]>cost+A+B) { //kick
            dist[newx][newy][j+1] = cost+A+B;
            pq.emplace(-cost-A-B, newx, newy, j+1);
          }
        }
      }
    }
  }

  long long ans = INF;
  for(int k=0; k<5; ++k) {
    ans = min(ans, dist[oneandn[1].first][oneandn[1].second][k]);
  }
  cout<<ans<<'\n';
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  PlayGround();
  return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 70 ms 6736 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 275 ms 18504 KB Output is correct
4 Correct 275 ms 18492 KB Output is correct
5 Correct 70 ms 6868 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 363 ms 18500 KB Output is correct
2 Correct 310 ms 18652 KB Output is correct
3 Correct 231 ms 16056 KB Output is correct
4 Correct 225 ms 18420 KB Output is correct
5 Correct 242 ms 18496 KB Output is correct
6 Correct 248 ms 18516 KB Output is correct
7 Correct 326 ms 18624 KB Output is correct
8 Correct 275 ms 18488 KB Output is correct
9 Correct 338 ms 18636 KB Output is correct
10 Correct 49 ms 7524 KB Output is correct
11 Correct 305 ms 18620 KB Output is correct
12 Correct 326 ms 18488 KB Output is correct
13 Correct 201 ms 16060 KB Output is correct
14 Correct 301 ms 18524 KB Output is correct
15 Correct 249 ms 18580 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 70 ms 6736 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 275 ms 18504 KB Output is correct
4 Correct 275 ms 18492 KB Output is correct
5 Correct 70 ms 6868 KB Output is correct
6 Correct 363 ms 18500 KB Output is correct
7 Correct 310 ms 18652 KB Output is correct
8 Correct 231 ms 16056 KB Output is correct
9 Correct 225 ms 18420 KB Output is correct
10 Correct 242 ms 18496 KB Output is correct
11 Correct 248 ms 18516 KB Output is correct
12 Correct 326 ms 18624 KB Output is correct
13 Correct 275 ms 18488 KB Output is correct
14 Correct 338 ms 18636 KB Output is correct
15 Correct 49 ms 7524 KB Output is correct
16 Correct 305 ms 18620 KB Output is correct
17 Correct 326 ms 18488 KB Output is correct
18 Correct 201 ms 16060 KB Output is correct
19 Correct 301 ms 18524 KB Output is correct
20 Correct 249 ms 18580 KB Output is correct
21 Correct 81 ms 7192 KB Output is correct
22 Correct 349 ms 18520 KB Output is correct
23 Correct 361 ms 14220 KB Output is correct
24 Correct 411 ms 15560 KB Output is correct
25 Correct 318 ms 18468 KB Output is correct
26 Correct 393 ms 18652 KB Output is correct
27 Correct 197 ms 13780 KB Output is correct
28 Correct 235 ms 14156 KB Output is correct
29 Correct 336 ms 16888 KB Output is correct
30 Correct 229 ms 13832 KB Output is correct
31 Correct 318 ms 18532 KB Output is correct
32 Correct 372 ms 20004 KB Output is correct
33 Correct 268 ms 18488 KB Output is correct
34 Correct 391 ms 18548 KB Output is correct
35 Correct 193 ms 13756 KB Output is correct