Skip to main content Link Menu Expand (external link) Document Search Copy Copied

01504번 특정한 최단 경로

Problem link: https://www.acmicpc.net/problem/1504

Keywords: Dijkstra, A-star

이 문제 자체에 별다른 의미는 없다. 단지 이 포스팅에서 A-star를 설명하면서, Dijkstra vs. A-star의 수행시간 차이를 보이기 위해 풀었다. 그런데 풀고 보니 이 문제는 heuristic을 정의할 수 없기 때문에…쓸모 없는 일이었다.

#include <algorithm>
#include <cstdint>
#include <iostream>
#include <limits>
#include <queue>
#include <vector>

using namespace std;

int N, E;
vector<pair<int, int>> ADJ[800 + 1];
int V1, V2;

// Shortest path using Dijkstra algorithm.
int64_t Sp1(int src, int dst) {
  vector<int> dist(N + 1, numeric_limits<int>::max());
  priority_queue<pair<int, int>> pq;
  dist[src] = 0;
  pq.push({-dist[src], src});
  while (!pq.empty()) {
    auto cost_so_far = -pq.top().first;
    auto here = pq.top().second;
    pq.pop();
    if (dist[here] < cost_so_far) {
      continue;
    }
    dist[here] = cost_so_far;
    if (here == dst) {
      return dist[here];
    }
    for (const auto neighbor : ADJ[here]) {
      auto there = neighbor.first;
      auto cost = neighbor.second;
      if (dist[there] > dist[here] + cost) {
        dist[there] = dist[here] + cost;
        pq.push({-dist[there], there});
      }
    }
  }
  return numeric_limits<int>::max();
}

int main() {
  // For faster IO.
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);

  // Read inputs.
  cin >> N >> E;
  for (int i = 0; i < E; ++i) {
    int a, b, c;
    cin >> a >> b >> c;
    ADJ[a].push_back({b, c});
    ADJ[b].push_back({a, c});
  }
  cin >> V1 >> V2;

  // Solve the problem.
  auto v1_to_v2 = Sp1(V1, V2);
  auto ans = min(Sp1(1, V1) + v1_to_v2 + Sp1(V2, N),
                 Sp1(1, V2) + v1_to_v2 + Sp1(V1, N));
  if (ans >= numeric_limits<int>::max()) {
    cout << -1 << '\n';
  } else {
    cout << ans << '\n';
  }

  return 0;
}