結果

提出番号 978
提出者 ei1333
言語 C++
提出日時 2017-08-01 23:37:20
問題名 (44)玉ねぎの収穫をするkotamanegi
結果 AC
点数 100%

テストケース

テストケース 結果 得点 実行時間 メモリ使用量
1 AC 100% 31ms 38192KB
2 AC 100% 26ms 37152KB
3 AC 100% 62ms 38208KB
4 AC 100% 55ms 38192KB
5 AC 100% 42ms 38192KB
6 AC 100% 40ms 39760KB
7 AC 100% 6ms 36384KB
8 AC 100% 77ms 39760KB
9 AC 100% 31ms 38192KB
10 AC 100% 11ms 38192KB
11 AC 100% 15ms 38192KB
12 AC 100% 17ms 36640KB
13 AC 100% 87ms 38192KB
14 AC 100% 8ms 36912KB
15 AC 100% 108ms 39744KB
16 AC 100% 17ms 36624KB
17 AC 100% 27ms 38192KB
18 AC 100% 60ms 38192KB
19 AC 100% 24ms 39760KB
20 AC 100% 7ms 36384KB

ソースコード

#include<bits/stdc++.h>

using namespace std;

typedef long long int64;
const int64 INF = 1LL << 58;

const int vy[] = {1, 0, -1, 0};
const int vx[] = {0, 1, 0, -1};

int main()
{
  int H, W;
  int gy, gx;
  int Y[1000], X[1000];
  int yl, yr, xl, xr;

  cin >> H >> W;
  cin >> gy >> gx;
  --gy, --gx;
  for(int i = 0; i < H; i++) cin >> Y[i];
  for(int i = 0; i < W; i++) cin >> X[i];
  cin >> yl >> yr >> xl >> xr;
  --yl, --yr, --xl, --xr;

  int64 dp[1000000];
  fill_n(dp, 1000000, INF);
  dp[0] = 0;

  typedef pair< int64, int > Pi;
  priority_queue< Pi, vector< Pi >, greater< Pi > > que;
  que.emplace(0, 0);
  dp[0] = 0;
  while(!que.empty()) {
    auto p = que.top();
    que.pop();
    if(p.first > dp[p.second]) continue;
    int y = p.second / 1000, x = p.second % 1000;
    if(gy == y && gx == x) {
      cout << p.first << endl;
      return (0);
    }
    for(int i = 0; i < 4; i++) {
      int ny = y + vy[i], nx = x + vx[i];
      if(ny < 0 || nx < 0 || ny >= H || nx >= W) continue;
      if(yl <= ny && ny <= yr && xl <= nx && nx <= xr) continue;
      int64 cost = p.first + (i % 2 == 1 ? Y[y] : X[x]);
      int nidx = ny * 1000 + nx;
      if(cost >= dp[nidx]) continue;
      dp[nidx] = cost;
      que.emplace(cost, nidx);
    }

  }

  cout << -1 << endl;
}