結果

問題 No.859 路線A、路線B、路線C
ユーザー tsutajtsutaj
提出日時 2019-08-09 21:53:10
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,407 bytes
コンパイル時間 1,196 ms
コンパイル使用メモリ 115,732 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-26 17:48:27
合計ジャッジ時間 2,030 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 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,384 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 WA -
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 2 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 = 1001001001001001LL;
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, int, bool)> add_vertex = [&](char c, int k, bool sg) {
        if(c == 'A') {
            if(k != 1 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;
            }
        }
        if(c == 'B') {
            if(k != 1 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;
            }
        }
        if(c == 'C') {
            if(k != 1 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;
            }
        }
    };
    add_vertex(S0, T0, true);
    add_vertex(S1, T1, false);

    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 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