結果

問題 No.1065 電柱 / Pole (Easy)
ユーザー HaarHaar
提出日時 2020-05-29 21:51:22
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 370 ms / 2,000 ms
コード長 3,761 bytes
コンパイル時間 2,785 ms
コンパイル使用メモリ 216,548 KB
実行使用メモリ 24,024 KB
最終ジャッジ日時 2024-04-23 21:30:27
合計ジャッジ時間 10,510 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 171 ms
13,952 KB
testcase_03 AC 249 ms
23,868 KB
testcase_04 AC 247 ms
22,732 KB
testcase_05 AC 206 ms
22,412 KB
testcase_06 AC 209 ms
22,580 KB
testcase_07 AC 74 ms
8,704 KB
testcase_08 AC 283 ms
23,392 KB
testcase_09 AC 25 ms
6,940 KB
testcase_10 AC 123 ms
12,416 KB
testcase_11 AC 83 ms
9,728 KB
testcase_12 AC 46 ms
7,296 KB
testcase_13 AC 166 ms
14,992 KB
testcase_14 AC 194 ms
17,732 KB
testcase_15 AC 227 ms
19,596 KB
testcase_16 AC 106 ms
11,736 KB
testcase_17 AC 237 ms
20,828 KB
testcase_18 AC 73 ms
9,216 KB
testcase_19 AC 254 ms
20,136 KB
testcase_20 AC 63 ms
8,064 KB
testcase_21 AC 90 ms
10,880 KB
testcase_22 AC 220 ms
18,624 KB
testcase_23 AC 3 ms
6,944 KB
testcase_24 AC 4 ms
6,944 KB
testcase_25 AC 40 ms
7,040 KB
testcase_26 AC 116 ms
13,088 KB
testcase_27 AC 128 ms
12,852 KB
testcase_28 AC 212 ms
18,796 KB
testcase_29 AC 25 ms
6,940 KB
testcase_30 AC 227 ms
20,624 KB
testcase_31 AC 158 ms
16,792 KB
testcase_32 AC 109 ms
11,568 KB
testcase_33 AC 245 ms
22,008 KB
testcase_34 AC 84 ms
9,600 KB
testcase_35 AC 230 ms
20,832 KB
testcase_36 AC 3 ms
6,940 KB
testcase_37 AC 4 ms
6,940 KB
testcase_38 AC 3 ms
6,944 KB
testcase_39 AC 4 ms
6,944 KB
testcase_40 AC 2 ms
6,940 KB
testcase_41 AC 370 ms
24,024 KB
testcase_42 AC 100 ms
9,856 KB
testcase_43 AC 186 ms
14,464 KB
testcase_44 AC 62 ms
7,552 KB
testcase_45 AC 172 ms
14,068 KB
testcase_46 AC 2 ms
6,944 KB
testcase_47 AC 2 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

/**
 * @title グラフ用テンプレート
 * @docs graph_template.md
 */
template <typename Cost = int> class Edge{
public:
  int from,to;
  Cost cost;
  Edge() {}
  Edge(int to, Cost cost): to(to), cost(cost){}
  Edge(int from, int to, Cost cost): from(from), to(to), cost(cost){}
};

template <typename T> using Graph = std::vector<std::vector<Edge<T>>>;
template <typename T> using Tree = std::vector<std::vector<Edge<T>>>;

template <typename T, typename C> void add_edge(C &g, int from, int to, T w = 1){
  g[from].emplace_back(from, to, w);
}

template <typename T, typename C> void add_undirected(C &g, int a, int b, T w = 1){
  add_edge<T, C>(g, a, b, w);
  add_edge<T, C>(g, b, a, w);
}

/**
 * @docs input_graph.md
 */
template <typename T, size_t I, bool WEIGHTED>
std::vector<Edge<T>> input_edges(int M){
  std::vector<Edge<T>> ret;
  
  for(int i = 0; i < M; ++i){
    int s, t; std::cin >> s >> t;
    s -= I;
    t -= I;
    T w = 1; if(WEIGHTED) std::cin >> w;
    ret.emplace_back(s, t, w);
  }
  
  return ret;  
}

template <typename T, bool DIRECTED>
Graph<T> convert_to_graph(int N, const std::vector<Edge<T>> &edges){
  Graph<T> g(N);

  for(const auto &e : edges){
    add_edge(g, e.from, e.to, e.cost);
    if(not DIRECTED) add_edge(g, e.to, e.from, e.cost);
  }
  
  return g;
}


/**
 * @docs input_tuple_vector.md
 */
template <typename T, size_t ... I>
void input_tuple_vector_init(T &val, int N, std::index_sequence<I...>){
  (void)std::initializer_list<int>{
    (void(std::get<I>(val).resize(N)), 0)...
      };
}

template <typename T, size_t ... I>
void input_tuple_vector_helper(T &val, int i, std::index_sequence<I...>){
  (void)std::initializer_list<int>{
    (void(std::cin >> std::get<I>(val)[i]), 0)...
      };
}

template <typename ... Args>
auto input_tuple_vector(int N){
  std::tuple<std::vector<Args>...> ret;

  input_tuple_vector_init(ret, N, std::make_index_sequence<sizeof...(Args)>());
  for(int i = 0; i < N; ++i){
    input_tuple_vector_helper(ret, i, std::make_index_sequence<sizeof...(Args)>());
  }

  return ret;
}


/**
 * @title Dijkstra法
 * @docs dijkstra.md
 */
template <typename T>
class Dijkstra{
public:
  std::vector<std::optional<T>> dist;

private:
  void run(const Graph<T> &graph, std::vector<int> src){
    const int n = graph.size();
    dist.assign(n, std::nullopt);
    
    std::vector<bool> check(n, false);
    std::priority_queue<std::pair<T,int>, std::vector<std::pair<T,int>>, std::greater<std::pair<T,int>>> pq;

    for(auto s : src){
      dist[s] = 0;
      pq.emplace(0, s);
    }

    while(not pq.empty()){
      const auto [d,i] = pq.top(); pq.pop();

      if(check[i]) continue;
      check[i] = true;

      for(auto &e : graph[i]){
        if(not dist[e.to]){
          dist[e.to] = d + e.cost;
          pq.emplace(*dist[e.to], e.to);
        }else{
          if(*dist[e.to] > d + e.cost){
            dist[e.to] = d + e.cost;
            if(not check[e.to]) pq.emplace(*dist[e.to], e.to);
          }
        }
      }
    }
  }

public:
  Dijkstra(const Graph<T> &graph, int src){run(graph, {src});}
  Dijkstra(const Graph<T> &graph, const std::vector<int> &src){run(graph, src);}
};




int main(){
  int N, M, X, Y;

  while(std::cin >> N >> M >> X >> Y){
    --X, --Y;
    
    auto [p, q] = input_tuple_vector<double, double>(N);

    Graph<double> g(N);
    for(int i = 0; i < M; ++i){
      int P, Q; std::cin >> P >> Q;
      --P, --Q;

      double dx = p[P] - p[Q];
      double dy = q[P] - q[Q];

      double L = std::sqrt(dx * dx + dy * dy);

      add_undirected(g, P, Q, L);
    }

    auto ans = Dijkstra(g, X).dist[Y].value();
    
    std::cout << std::fixed << std::setprecision(12) << ans << "\n";
  }

  return 0;
}
0