結果

提出番号 985
提出者 algon_320
言語 C++
提出日時 2017-08-02 20:53:03
問題名 (44)玉ねぎの収穫をするkotamanegi
結果 AC
点数 100%

テストケース

テストケース 結果 得点 実行時間 メモリ使用量
1 AC 100% 44ms 13600KB
2 AC 100% 34ms 11760KB
3 AC 100% 76ms 18928KB
4 AC 100% 82ms 22048KB
5 AC 100% 55ms 14960KB
6 AC 100% 276ms 40976KB
7 AC 100% 3ms 7680KB
8 AC 100% 105ms 20896KB
9 AC 100% 69ms 16848KB
10 AC 100% 82ms 20432KB
11 AC 100% 59ms 16256KB
12 AC 100% 13ms 8272KB
13 AC 100% 97ms 20224KB
14 AC 100% 40ms 11856KB
15 AC 100% 276ms 37744KB
16 AC 100% 15ms 8320KB
17 AC 100% 87ms 17200KB
18 AC 100% 108ms 17008KB
19 AC 100% 77ms 19376KB
20 AC 100% 11ms 7680KB

ソースコード

#include <bits/stdc++.h>
using namespace std;
#define int long long
using ll=long long;
using vi=vector<int>;
using vl=vector<long long>;
using pii=pair<int,int>;
using pll=pair<long long,long long>;
#define ITR(i,c) for(auto i=begin(c);i!=end(c);++i)
#define FORE(x,c) for(auto &x:c)
#define REPF(i,a,n) for(int i=a,i##len=(int)(n);i<i##len;++i)
#define REP(i,n) REPF(i,0,n)
#define REPR(i,n) for(int i=(int)(n);i>=0;--i)
#define REPW(i,n) for(i=0;i<(int)(n);++i)
#define ALL(c) begin(c),end(c)
#define RALL(c) rbegin(c),rend(c)
#define SZ(c) ((int)c.size())
#define CONTAIN(c,x) (c.find(x)!=end(c))
#define OUTOFRANGE(y,x,h,w) (y<0||x<0||y>=h||x>=w)
#define dump(...)
const int DX[9]={0,1,0,-1,1,1,-1,-1,0},DY[9]={-1,0,1,0,-1,1,1,-1,0};
#define INF (1001001001)
#define INFLL (1001001001001001001ll)
template<class T> ostream& operator << (ostream &os,const vector<T> &v) {
    ITR(i,v) os << *i << (i==end(v)-1 ? "" : "\n"); return os; }
template<class T> istream& operator >> (istream &is,vector<T> &v) {
    ITR(i,v) is >> * i; return is; }
template<class T> istream& operator >> (istream &is, pair<T,T> &p) {
        is >> p.first >> p.second; return is; }
template<class T>bool chmax(T &a,const T &b){if(a<b){a=b;return 1;}return 0;}
template<class T>bool chmin(T &a,const T &b){if(b<a){a=b;return 1;}return 0;}
//------------------------------------------------------------------------------
struct before_main_function {
    before_main_function() {
        #ifdef int
            #undef INF
            #define INF INFLL
            #define stoi stoll
        #endif
        cin.tie(0);ios::sync_with_stdio(false);
        cout<<setprecision(15)<<fixed;
    }
} before_main_function;
//------------------8<------------------------------------8<--------------------

signed main() {
    int H,W,gy,gx,t,b,l,r;
    cin>>H>>W>>gy>>gx;
    gy--,gx--;
    vector<int>Y(H),X(W);
    cin>>Y>>X>>t>>b>>l>>r;
    t--,b--,l--,r--;
    vector<vector<int>> dist(H,vector<int>(W,INF));
    priority_queue<pair<int,pii>,vector<pair<int,pii>>,greater<pair<int,pii>>> pq;
    dist[0][0]=0;
    pq.push({0,{0,0}});
    while(!pq.empty()) {
        int d=pq.top().first;
        int y=pq.top().second.first;
        int x=pq.top().second.second;
        pq.pop();
        REP(dir,4) {
            int yy=y+DY[dir];
            int xx=x+DX[dir];
            if(OUTOFRANGE(yy,xx,H,W)) continue;
            if(t<=yy && yy<=b && l<=xx && xx<=r) continue;
            int cost=0;
            if(dir==0 || dir==2) cost=X[x];
            if(dir==1 || dir==3) cost=Y[y];
            if(chmin(dist[yy][xx],d+cost)) {
                pq.push({d+cost,{yy,xx}});
            }
        }
    }
    cout<<(dist[gy][gx]==INF?-1:dist[gy][gx])<<endl;
    return 0;
}