結果

問題 No.859 路線A、路線B、路線C
ユーザー tsutajtsutaj
提出日時 2019-08-09 22:14:33
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 1,000 ms
コード長 3,633 bytes
コンパイル時間 1,239 ms
コンパイル使用メモリ 117,004 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-26 18:14:10
合計ジャッジ時間 2,086 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

// #define _GLIBCXX_DEBUG // for STL debug (optional)
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <fstream>
#include <functional>
#include <bitset>
using namespace std;
using ll = long long int;
using int64 = long long int;
 
template<typename T> void chmax(T &a, T b) {a = max(a, b);}
template<typename T> void chmin(T &a, T b) {a = min(a, b);}
template<typename T> void chadd(T &a, T b) {a = a + b;}
 
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
const ll INF = 1LL << 60;
const ll MOD = 1000000007LL;

const int A1 = 0;
const int AX = 1;
const int B1 = 2;
const int BY = 3;
const int C1 = 4;
const int CZ = 5;
 
int main() {
    ll X, Y, Z; cin >> X >> Y >> Z;
    char S0, S1;
    ll T0, T1;
    cin >> S0 >> T0 >> S1 >> T1;
    ll sid, gid;

    // A1, Ax, C1, Cz, B1, By, S0, S1 だけ使う
    int N = 6;

    vector< tuple<ll, ll, ll> > edges;
    edges.emplace_back(A1, B1, 1);
    edges.emplace_back(A1, C1, 1);
    edges.emplace_back(B1, C1, 1);

    edges.emplace_back(A1, AX, X-1);
    edges.emplace_back(B1, BY, Y-1);
    edges.emplace_back(C1, CZ, Z-1);
    
    edges.emplace_back(AX, BY, 1);
    edges.emplace_back(AX, CZ, 1);
    edges.emplace_back(BY, CZ, 1);
    
    function<void(char, ll, bool)> add_vertex = [&](char c, ll k, bool sg) {
        if(c == 'A') {
            if(1 < k and k < X) {
                int id = N++;
                edges.emplace_back(A1, id, k - 1);
                edges.emplace_back(id, AX, X - k);
                (sg ? sid : gid) = id;
            }
            else if(k == 1) {
                (sg ? sid : gid) = A1;
            }
            else if(k == X) {
                (sg ? sid : gid) = AX;
            }
            else assert(false);
        }
        if(c == 'B') {
            if(1 < k and k < Y) {
                int id = N++;
                edges.emplace_back(B1, id, k - 1);
                edges.emplace_back(id, BY, Y - k);
                (sg ? sid : gid) = id;
            }
            else if(k == 1) {
                (sg ? sid : gid) = B1;
            }
            else if(k == Y) {
                (sg ? sid : gid) = BY;
            }
            else assert(false);
        }
        if(c == 'C') {
            if(1 < k and k < Z) {
                int id = N++;
                edges.emplace_back(C1, id, k - 1);
                edges.emplace_back(id, CZ, Z - k);
                (sg ? sid : gid) = id;
            }
            else if(k == 1) {
                (sg ? sid : gid) = C1;
            }
            else if(k == Z) {
                (sg ? sid : gid) = CZ;
            }
            else assert(false);
        }
    };
    add_vertex(S0, T0, true);
    add_vertex(S1, T1, false);

    if(S0 == S1) {
        edges.emplace_back(sid, gid, llabs(T0 - T1));
    }

    vector< vector<ll> > dist(N, vector<ll>(N, INF));
    for(auto e : edges) {
        ll u, v, d; tie(u, v, d) = e;
        chmin(dist[u][v], d);
        chmin(dist[v][u], d);
    }
    for(int i=0; i<N; i++) {
        chmin(dist[i][i], 0LL);
    }

    for(int k=0; k<N; k++) {
        for(int i=0; i<N; i++) {
            for(int j=0; j<N; j++) {
                dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
            }
        }
    }

    cout << dist[sid][gid] << endl;
    return 0;
}
0