結果
| 問題 |
No.859 路線A、路線B、路線C
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2022-10-16 21:43:54 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 2 ms / 1,000 ms |
| コード長 | 2,661 bytes |
| コンパイル時間 | 2,155 ms |
| コンパイル使用メモリ | 205,356 KB |
| 最終ジャッジ日時 | 2025-02-08 07:19:11 |
|
ジャッジサーバーID (参考情報) |
judge5 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 12 |
ソースコード
#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;
// g <- pair < v , cost >
template < class T >
vector< T > dijkstra(vector<vector<pair<int, T>>> &graph, int s) {
T INF = numeric_limits< T >::max();
vector<T> dist(graph.size(), INF);
priority_queue<pair<T,int>, vector<pair<T,int>>, greater<pair<T,int>>> q;
q.push({dist[s] = T(0), s});
while(!q.empty()){
auto [uc, ui] = q.top(); q.pop();
if(uc != dist[ui]) continue;
for(auto [vi, vc] : graph[ui]) if(dist[vi] > uc + vc)
q.push({dist[vi] = uc + vc, vi});
}
return dist;
}
int main(){
cin.tie(0);
ios::sync_with_stdio(0);
int x,y,z; cin >> x >> y >> z;
char s0, s1; int t0, t1;
cin >> s0 >> t0;
cin >> s1 >> t1;
int N = 8;
vector<vector<pair<int,int>>> G(N);
int A1 = 0, B1 = 1, C1 = 2;
int Ax = 3, By = 4, Cz = 5;
G[A1].push_back({B1, 1});
G[B1].push_back({A1, 1});
G[B1].push_back({C1, 1});
G[C1].push_back({B1, 1});
G[C1].push_back({A1, 1});
G[A1].push_back({C1, 1});
G[Ax].push_back({By, 1});
G[By].push_back({Ax, 1});
G[By].push_back({Cz, 1});
G[Cz].push_back({By, 1});
G[Cz].push_back({Ax, 1});
G[Ax].push_back({Cz, 1});
G[A1].push_back({Ax, x - 1});
G[Ax].push_back({A1, x - 1});
G[B1].push_back({By, y - 1});
G[By].push_back({B1, y - 1});
G[C1].push_back({Cz, z - 1});
G[Cz].push_back({C1, z - 1});
auto f = [&](char s, int t, int b) {
if(s == 'A' && t == 1) return A1;
if(s == 'A' && t == x) return Ax;
if(s == 'B' && t == 1) return B1;
if(s == 'B' && t == y) return By;
if(s == 'C' && t == 1) return C1;
if(s == 'C' && t == z) return Cz;
int ID = 6 + b;
if(s == 'A') {
G[ID].push_back({A1, t - 1});
G[A1].push_back({ID, t - 1});
G[ID].push_back({Ax, x - t});
G[Ax].push_back({ID, x - t});
}
if(s == 'B') {
G[ID].push_back({B1, t - 1});
G[B1].push_back({ID, t - 1});
G[ID].push_back({By, y - t});
G[By].push_back({ID, y - t});
}
if(s == 'C') {
G[ID].push_back({C1, t - 1});
G[C1].push_back({ID, t - 1});
G[ID].push_back({Cz, z - t});
G[Cz].push_back({ID, z - t});
}
return ID;
};
int S = f(s0, t0, 0);
int T = f(s1, t1, 1);
if(s0 == s1) {
G[S].push_back({T, abs(t0 - t1)});
G[T].push_back({S, abs(t0 - t1)});
}
cout << dijkstra(G, S)[T] << endl;
}