結果

問題 No.859 路線A、路線B、路線C
ユーザー kk
提出日時 2021-02-08 20:29:37
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,764 bytes
コンパイル時間 2,898 ms
コンパイル使用メモリ 239,048 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-20 01:50:07
合計ジャッジ時間 3,691 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

const long long INF = 1LL<<60;

void add(map<string, vector<tuple<string, long long> > > &edges, string x, string y, long long d) {
  edges[x].emplace_back(y, d);
  edges[y].emplace_back(x, d);
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  int x, y, z;
  cin >> x >> y >> z;

  map<string, vector<tuple<string, long long> > > edges;
  string a1 = "a1", an = "a" + to_string(x);
  string b1 = "b1", bn = "b" + to_string(y);
  string c1 = "c1", cn = "c" + to_string(z);  
  
  add(edges, a1, an, x-1);
  add(edges, b1, bn, y-1);
  add(edges, c1, cn, z-1);
  add(edges, a1, b1, 1);
  add(edges, b1, c1, 1);
  add(edges, c1, a1, 1);
  add(edges, an, bn, 1);
  add(edges, bn, cn, 1);
  add(edges, cn, an, 1);

  for (int i = 0; i < 2; i++) {
    string s;
    int t;
    cin >> s >> t;
    string v = to_string(i);

    if (s == "A") {
      add(edges, v, a1, abs(1-t));
      add(edges, v, an, abs(x-t));      
    } else if (s == "B") {
      add(edges, v, b1, abs(1-t));
      add(edges, v, bn, abs(y-t));
    } else {
      add(edges, v, c1, abs(1-t));
      add(edges, v, cn, abs(z-t));
    }
  }

  map<string, long long> dist {
    {a1, INF}, {b1, INF}, {c1, INF},
    {an, INF}, {bn, INF}, {cn, INF},
    {"0", 0}, {"1", INF}
  };
  priority_queue<pair<long long, string>, vector<pair<long long, string>>, greater<pair<long long, string>> > q;
  q.emplace(0, "0");

  while (!q.empty()) {
    string v;
    long long d;
    tie(d, v) = q.top();
    q.pop();

    if (d > dist[v])
      continue;
    
    for (auto &[w, cost]: edges[v]) {
      if (d + cost < dist[w]) {
        dist[w] = d + cost;
        q.emplace(dist[w], w);
      }
    }
  }
  cout << dist["1"] << endl;
  
  return 0;
}
0