結果

問題 No.1283 Extra Fee
ユーザー Suu0313Suu0313
提出日時 2020-11-06 23:14:01
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,245 ms / 2,000 ms
コード長 6,251 bytes
コンパイル時間 2,714 ms
コンパイル使用メモリ 229,680 KB
実行使用メモリ 23,468 KB
最終ジャッジ日時 2023-08-10 06:09:06
合計ジャッジ時間 18,188 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 24 ms
4,376 KB
testcase_12 AC 36 ms
4,504 KB
testcase_13 AC 29 ms
4,380 KB
testcase_14 AC 155 ms
6,872 KB
testcase_15 AC 256 ms
8,952 KB
testcase_16 AC 46 ms
4,552 KB
testcase_17 AC 446 ms
13,200 KB
testcase_18 AC 1,082 ms
20,720 KB
testcase_19 AC 1,200 ms
22,580 KB
testcase_20 AC 1,110 ms
21,624 KB
testcase_21 AC 1,117 ms
21,664 KB
testcase_22 AC 970 ms
19,728 KB
testcase_23 AC 951 ms
23,080 KB
testcase_24 AC 1,031 ms
23,064 KB
testcase_25 AC 1,225 ms
23,144 KB
testcase_26 AC 1,244 ms
23,212 KB
testcase_27 AC 1,245 ms
23,252 KB
testcase_28 AC 1,245 ms
23,468 KB
testcase_29 AC 477 ms
13,500 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

//typedef
//------------------------------------------
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL,LL> PLL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef vector<bool> VB;
typedef vector<VB> VVB;
typedef vector<double> VD;
typedef vector<VD> VVD;
typedef vector<string> VS;
typedef vector<VS> VVS;
typedef vector<char> VC;
typedef vector<VC> VVC;
typedef vector<PII> VPII;
typedef vector<PLL> VPLL;
typedef priority_queue<int> PQGI; //大きい順
typedef priority_queue<int, VI, greater<int>> PQLI;
typedef priority_queue<PII> PQGP;
typedef priority_queue<PII, VPII, greater<PII>> PQLP;

//container util
//------------------------------------------
#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define EB emplace_back
#define EF emplace_front
#define PB push_back
#define PF push_front
#define POB pop_back
#define POF pop_front
#define MP make_pair
#define MT make_tuple
#define SZ(a) (int)((a).size())
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())
#define SORTR(c) sort((c).rbegin(), (c).rend())
#define LB lower_bound
#define UB upper_bound
#define NEXP(a) next_permutation((a).begin(),(a).end())
#define FI first
#define SE second

//repetition
//------------------------------------------
#define FOR(i,a,b) for(decltype(b) i=(a); i<(b); i++)
#define REP(i,n)  FOR(i,0,n)
#define FORR(i,a,b) for(decltype(b) i = (b-1); i>=(a); i--)
#define REPR(i,n) FORR(i,0,n)
#define CFOR(i,a,b) for(decltype(b) i=(a); i<=(b); i++)
#define CREP(i,n)  CFOR(i,0,n)
#define CFORR(i,a,b) for(decltype(b) i = (b); i>=(a); i--)
#define CREPR(i,n) CFORR(i,0,n)
#define BFOR(bit,a,b) for(LL bit=(a); bit<(1ll<<(b)); bit++)
#define BREP(bit,n) BFOR(bit,0,n)
#define EACH(ptr,c) for(auto ptr=(c).begin(); ptr!=(c).end(); )

//constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI  = acos(-1.0);
const int INF = INT_MAX/2;
const LL LINF = LLONG_MAX/3;
const int RINF = INT_MIN/2;
const LL RLINF = LLONG_MIN/3;
const LL MOD = 1e9+7;
const LL MODD = 998244353;
const int MAX = 510000;

//math
//-------------------------------------------
template<class T> inline T sqr(T x) { return x*x; }
inline bool Eq(double a, double b) { return fabs(b - a) < EPS; }
template<class T> inline T CEIL(T x, T y) { return (x+y-1)/y; }
template<class T> inline int BC(T x) { return __builtin_popcountll(x); }

//conversion
//------------------------------------------
inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
inline LL toLL(string s) {LL v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toStr(T x) {ostringstream sout;sout<<x;return sout.str();}

//other
//-------------------------------------------
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; }

template<typename T>
T Vsum(vector<T> &v){T res(0);for(const auto&x:v) res += x; return res; }
template<typename T>
T Vgcd(vector<T> &v){T res(0);for(const auto&x:v) res=gcd(res,x); return res; }
template<typename T>
T Vlcm(vector<T> &v){T res(1);for(const auto&x:v) res=lcm(res,x); return res; }

template<typename T> T Vmax(vector<T> &v){return *max_element(v.begin(),v.end()); }
template<typename T> T Vmin(vector<T> &v){return *min_element(v.begin(),v.end()); }
template<typename T> void Vadd(vector<T> &v, T a){for(auto&& x : v) x += a; }

template<typename T>
vector<T> make_v(size_t a,T b){return vector<T>(a,b);}
template<typename... Ts>
auto make_v(size_t a,Ts... ts){
  return vector<decltype(make_v(ts...))>(a,make_v(ts...));
}

const vector<int> dx = {1,0,-1,0,1,1,-1,-1};
const vector<int> dy = {0,1,0,-1,1,-1,1,-1};
bool ISIN(int x,int l,int r) { return (l<=x) && (x<r); }

bool cYN(bool fl=true){cout << (fl?"Yes":"No") << endl; return fl; }
bool CYN(bool fl=true){cout << (fl?"YES":"NO") << endl; return fl; }
template<class T>
void COUT(T&& t){ cout << t << endl; }
template<class T,class... Ts>
void COUT(T&& t,Ts&&... ts){ cout << t << " "; COUT(forward<Ts>(ts)...); }

template< typename T1, typename T2 >
istream &operator>>(istream &is, pair< T1, T2 > &p) {
  is >> p.first >> p.second; return is;
}
template< typename T1, typename T2 >
ostream &operator<<(ostream &os, const pair< T1, T2 >& p) {
  os << p.first << " " << p.second; return os;
}
template< typename T >
istream &operator>>(istream &is, vector< T > &v) {
  for(T &in : v) is >> in;
  return is;
}
template< typename T >
ostream &operator<<(ostream &os, const vector< T > &v) {
  for(int i = 0; i < SZ(v); i++) {
    os << v[i] << (i + 1 != SZ(v) ? " " : "");
  }
  return os;
}



int main() {

  // cin.tie(0);
  // ios::sync_with_stdio(false);
  cout << fixed << setprecision(12);

  int N, M;
  cin >> N >> M;
  map<int, LL> mp;

  REP(i,M){
    int h,w;
    LL c;
    cin >> h >> w >> c;
    h--;w--;
    mp[h*N+w] = c;
  }

  using P = tuple<LL,int,int,int>;

  priority_queue<P,vector<P>,greater<P>> pq;

  LL ans = LINF;

  pq.emplace(0,0,0,0);
  VVB seen(N,VB(N,false));
  VVB seen2(N,VB(N,false));
  VVLL dist(N,VLL(N,LINF));
  VVLL dist2(N,VLL(N,LINF));
  dist.at(0).at(0) = 0;

  while(!pq.empty()){
    auto [d,a,b,c] = pq.top(); pq.pop();

    if(c == 0){

      if(seen.at(a).at(b)) continue;
      seen.at(a).at(b) = true;

      REP(i,4){
        int x = a+dx[i], y = b+dy[i];
        if(x<0||N<=x||y<0||N<=y) continue;
        LL cost = d+1;
        if(chmin(dist2.at(x).at(y),cost)){
          pq.emplace(cost,x,y,1);
        }
        int e = x*N+y;
        if(mp.count(e)) cost += mp[e];
        if(chmin(dist.at(x).at(y),cost)){
          pq.emplace(cost,x,y,0);
        }
      }
    }else{
      if(seen2.at(a).at(b)) continue;
      seen2.at(a).at(b) = true;

      REP(i,4){
        int x = a+dx[i], y = b+dy[i];
        if(x<0||N<=x||y<0||N<=y) continue;
        LL cost = d+1;
        int e = x*N+y;
        if(mp.count(e)) cost += mp[e];
        if(chmin(dist2.at(x).at(y),cost)){
          pq.emplace(cost,x,y,1);
        }
      }
    }
  }

  COUT(dist2.at(N-1).at(N-1));

  return 0;
}
0