結果

問題 No.2213 Neq Move
ユーザー umezoumezo
提出日時 2023-02-11 05:05:28
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 3 ms / 2,000 ms
コード長 1,696 bytes
コンパイル時間 2,478 ms
コンパイル使用メモリ 211,824 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-22 04:25:38
合計ジャッジ時間 3,143 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(v) v.begin(), v.end()
typedef long long ll;

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

const ll INF=1LL<<60;

struct Edge{
  int to;
  ll w;
  Edge(int to,ll w) : to(to),w(w) {}
};

using Graph=vector<vector<Edge>>;
using pli=pair<ll,int>;

template<class T> bool chmin(T& a,T b){
  if(a>b){
    a=b;
    return true;
  }
  return false;
}

vector<ll> dijkstra(Graph G,int s){
  int n=G.size();
  vector<ll> dist(n,INF);
  dist[s]=0;
  
  priority_queue<pli,vector<pli>,greater<pli>> que;
  que.push({dist[s],s});
  
  while(!que.empty()){
    int v=que.top().second;
    ll d=que.top().first;
    que.pop();
    
    if(d>dist[v]) continue;
    
    for(auto e:G[v]){
      if(chmin(dist[e.to],dist[v]+e.w)){
        que.push({dist[e.to],e.to});
      }
    }
  }
  return dist;
}

int main(){
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  
  int t;
  cin>>t;
  while(t--){
    Graph G(6);
    ll a,b,c,d;
    cin>>a>>b>>c>>d;
    G[0].push_back(Edge(3,1));
    G[0].push_back(Edge(4,1));
    if(a>b && c>d && c>=a && d>=b) G[0].push_back(Edge(5,c+d-a-b));
    if(a<b && c<d && c>=a && d>=b) G[0].push_back(Edge(5,c+d-a-b));
    G[1].push_back(Edge(2,3));
    G[2].push_back(Edge(1,3));
    if(c>d) G[1].push_back(Edge(5,c+d-1));
    else G[2].push_back(Edge(5,c+d-1));
    if(a>1) G[3].push_back(Edge(2,2));
    if(b>1) G[4].push_back(Edge(1,2));
    if(a==1) G[3].push_back(Edge(1,0));
    if(b==1) G[4].push_back(Edge(2,0));
    if(c>d && c>=a) G[3].push_back(Edge(5,c+d-a));
    if(c<d && d>=b) G[4].push_back(Edge(5,c+d-b));
    
    vector<ll> dis=dijkstra(G,0);
    cout<<dis[5]<<'\n';
  }
  
  return 0;
}
0