結果

問題 No.859 路線A、路線B、路線C
ユーザー beetbeet
提出日時 2019-08-09 22:31:41
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 1,000 ms
コード長 1,870 bytes
コンパイル時間 2,243 ms
コンパイル使用メモリ 209,984 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-26 20:47:07
合計ジャッジ時間 3,094 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using Int = long long;
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}


template <typename T>
vector<T> dijkstra(Int s,vector<vector<pair<Int, T> > > & G){
  const T INF = numeric_limits<T>::max();
  using P = pair<T, Int>;
  Int n=G.size();
  vector<T> d(n,INF);
  vector<Int> b(n,-1);
  priority_queue<P,vector<P>,greater<P> > q;
  d[s]=0;
  q.emplace(d[s],s);
  while(!q.empty()){
    P p=q.top();q.pop();
    Int v=p.second;
    if(d[v]<p.first) continue;
    for(auto& e:G[v]){
      Int u=e.first;
      T c=e.second;
      if(d[u]>d[v]+c){
        d[u]=d[v]+c;
        b[u]=v;
        q.emplace(d[u],u);
      }
    }
  }
  return d;
}

//INSERT ABOVE HERE
signed main(){
  Int x,y,z;
  cin>>x>>y>>z;
  char s0,s1;
  Int t0,t1;
  cin>>s0>>t0;
  cin>>s1>>t1;

  Int n=8;
  using P = pair<Int, Int>;
  vector< vector<P> > G(n);
  vector<Int> vs({x,x,y,y,z,z});

  for(Int k=0;k<6;k+=2){
    G[k+0].emplace_back(k+1,vs[k]-1);
    G[k+1].emplace_back(k+0,vs[k]-1);
  }

  for(Int k=0;k<2;k++){
    G[0+k].emplace_back(2+k,1);
    G[0+k].emplace_back(4+k,1);
    G[2+k].emplace_back(0+k,1);
    G[2+k].emplace_back(4+k,1);
    G[4+k].emplace_back(0+k,1);
    G[4+k].emplace_back(2+k,1);
  }

  for(Int k=0;k<6;k+=2){
    if(s0=='A'+(k/2)){
      G[k+0].emplace_back(6,t0-1);
      G[6].emplace_back(k+0,t0-1);
      G[k+1].emplace_back(6,vs[k]-t0);
      G[6].emplace_back(k+1,vs[k]-t0);
    }
    if(s1=='A'+(k/2)){
      G[k+0].emplace_back(7,t1-1);
      G[7].emplace_back(k+0,t1-1);
      G[k+1].emplace_back(7,vs[k]-t1);
      G[7].emplace_back(k+1,vs[k]-t1);
    }
  }

  if(s0==s1){
    G[6].emplace_back(7,abs(t0-t1));
    G[7].emplace_back(6,abs(t0-t1));
  }
  cout<<dijkstra(6,G)[7]<<endl;
  return 0;
}
0