結果

問題 No.1069 電柱 / Pole (Hard)
ユーザー HaarHaar
提出日時 2020-06-05 01:48:18
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 226 ms / 2,000 ms
コード長 5,604 bytes
コンパイル時間 3,585 ms
コンパイル使用メモリ 251,156 KB
実行使用メモリ 11,008 KB
最終ジャッジ日時 2024-07-23 15:08:38
合計ジャッジ時間 5,827 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 226 ms
6,940 KB
testcase_05 AC 182 ms
11,008 KB
testcase_06 AC 7 ms
6,940 KB
testcase_07 AC 8 ms
6,944 KB
testcase_08 AC 7 ms
6,944 KB
testcase_09 AC 8 ms
6,940 KB
testcase_10 AC 6 ms
6,940 KB
testcase_11 AC 2 ms
6,944 KB
testcase_12 AC 3 ms
6,944 KB
testcase_13 AC 6 ms
6,940 KB
testcase_14 AC 6 ms
6,944 KB
testcase_15 AC 5 ms
6,940 KB
testcase_16 AC 5 ms
6,944 KB
testcase_17 AC 3 ms
6,940 KB
testcase_18 AC 6 ms
6,944 KB
testcase_19 AC 8 ms
6,944 KB
testcase_20 AC 3 ms
6,940 KB
testcase_21 AC 7 ms
6,940 KB
testcase_22 AC 4 ms
6,940 KB
testcase_23 AC 11 ms
6,944 KB
testcase_24 AC 8 ms
6,944 KB
testcase_25 AC 8 ms
6,940 KB
testcase_26 AC 7 ms
6,940 KB
testcase_27 AC 7 ms
6,944 KB
testcase_28 AC 7 ms
6,940 KB
testcase_29 AC 3 ms
6,944 KB
testcase_30 AC 2 ms
6,940 KB
testcase_31 AC 2 ms
6,940 KB
testcase_32 AC 2 ms
6,944 KB
testcase_33 AC 2 ms
6,944 KB
testcase_34 AC 1 ms
6,944 KB
testcase_35 AC 2 ms
6,940 KB
testcase_36 AC 2 ms
6,940 KB
testcase_37 AC 3 ms
6,940 KB
testcase_38 AC 3 ms
6,940 KB
testcase_39 AC 2 ms
6,940 KB
testcase_40 AC 3 ms
6,940 KB
testcase_41 AC 4 ms
6,940 KB
testcase_42 AC 3 ms
6,944 KB
testcase_43 AC 4 ms
6,940 KB
testcase_44 AC 6 ms
6,944 KB
testcase_45 AC 6 ms
6,944 KB
testcase_46 AC 6 ms
6,940 KB
testcase_47 AC 7 ms
6,944 KB
testcase_48 AC 6 ms
6,944 KB
testcase_49 AC 6 ms
6,940 KB
testcase_50 AC 3 ms
6,940 KB
testcase_51 AC 7 ms
6,940 KB
testcase_52 AC 3 ms
6,944 KB
testcase_53 AC 7 ms
6,948 KB
testcase_54 AC 2 ms
6,940 KB
testcase_55 AC 2 ms
6,940 KB
testcase_56 AC 2 ms
6,944 KB
testcase_57 AC 2 ms
6,944 KB
testcase_58 AC 2 ms
6,944 KB
testcase_59 AC 2 ms
6,940 KB
testcase_60 AC 1 ms
6,940 KB
testcase_61 AC 2 ms
6,944 KB
testcase_62 AC 2 ms
6,940 KB
testcase_63 AC 2 ms
6,940 KB
testcase_64 AC 2 ms
6,944 KB
testcase_65 AC 2 ms
6,944 KB
testcase_66 AC 2 ms
6,944 KB
testcase_67 AC 2 ms
6,940 KB
testcase_68 AC 2 ms
6,944 KB
testcase_69 AC 4 ms
6,940 KB
testcase_70 AC 4 ms
6,940 KB
testcase_71 AC 4 ms
6,940 KB
testcase_72 AC 3 ms
6,944 KB
testcase_73 AC 3 ms
6,944 KB
testcase_74 AC 5 ms
6,944 KB
testcase_75 AC 4 ms
6,940 KB
testcase_76 AC 9 ms
6,940 KB
testcase_77 AC 9 ms
6,940 KB
testcase_78 AC 2 ms
6,940 KB
testcase_79 AC 7 ms
6,940 KB
testcase_80 AC 7 ms
6,944 KB
testcase_81 AC 7 ms
6,944 KB
testcase_82 AC 5 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#ifdef DEBUG
#include <Mylib/Debug/debug.cpp>
#else
#define dump(...)
#endif

/**
 * @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_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;
}





template <typename T>
auto yen_algorithm(Graph<T> g, int s, int t, int K){
  using Path = std::pair<T, std::vector<int>>;
  using P = std::pair<T, int>;

  const int N = g.size();

  std::vector<std::vector<bool>> valid(N);
  std::vector<std::optional<Path>> result(K);
  std::priority_queue<Path, std::vector<Path>, std::greater<Path>> stock;

  for(int i = 0; i < N; ++i){
    valid[i].assign(g[i].size(), true);
  }
  
  auto shortest_path =
    [&](int from, const std::vector<bool> &usable){
      std::vector<bool> visited(N, false);
      std::vector<std::optional<T>> dist(N);
      std::vector<std::pair<int, int>> restore(N);

      std::priority_queue<P, std::vector<P>, std::greater<P>> pq;

      dist[from] = 0;
      pq.emplace(0, from);

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

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

        for(int k = 0; k < (int)g[i].size(); ++k){
          if(not valid[i][k] or not usable[g[i][k].to]) continue;
          auto &e = g[i][k];

          if(not dist[e.to] or *dist[e.to] > d + e.cost){
            dist[e.to] = d + e.cost;
            restore[e.to] = std::make_pair(i, k);
            if(not visited[e.to]) pq.emplace(*dist[e.to], e.to);
          }
        }
      }

      std::optional<Path> ret;
    
      if(dist[t]){
        std::vector<int> p;

        int cur = t;
        while(cur != from){
          auto [i, j] = restore[cur];
          p.push_back(j);
          cur = i;
        }
      
        std::reverse(p.begin(), p.end());

        ret = std::make_pair(*dist[t], p);
      }

      return ret;
    };

  for(int i = 0; i < K; ++i){
    if(i == 0){
      std::vector<bool> usable(N, true);
      if(auto res = shortest_path(s, usable); res) stock.push(*res);
    }else{
      std::vector<int> prev_path;

      {
        int cur = s;
        for(auto u : result[i-1]->second){
          prev_path.push_back(cur);
          cur = g[cur][u].to;
        }
        prev_path.push_back(t);
      }

      std::vector<bool> check(i, true);
      std::vector<bool> usable(N, true);

      for(int k = 0; k < (int)prev_path.size() - 1; ++k){
        const int u = prev_path[k];
        
        for(int j = 0; j < i; ++j){
          if(check[j]){
            valid[prev_path[k]][result[j]->second[k]] = false;
          }
        }

        if(auto res = shortest_path(u, usable); res){

          auto [c, p] = *res;

          std::vector<int> temp;
          for(int j = 0; j < k; ++j){
            int v = result[i-1]->second[j];

            c += g[prev_path[j]][v].cost;
            temp.push_back(v);
          }

          temp.insert(temp.end(), p.begin(), p.end());
          stock.emplace(c, temp);
        }

        usable[u] = false;

        for(int j = 0; j < i; ++j){
          if(check[j]){
            valid[prev_path[k]][result[j]->second[k]] = true;
          }
        }

        for(int j = 0; j < i; ++j){
          if(check[j]){
            if(prev_path[k+1] != g[prev_path[k]][result[j]->second[k]].to){
              check[j] = false;
            }
          }
        }
      }
    }

    if(stock.empty()) break;

    result[i] = stock.top(); stock.pop();

    while(not stock.empty() and stock.top() == result[i]){
      stock.pop();
    }
  }

  return result;
}







int main(){
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  
  int N, M, K; std::cin >> N >> M >> K;
  int X, Y; std::cin >> X >> Y;
  --X, --Y;

  auto [p, q] = input_tuple_vector<long double, long double>(N);

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

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

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

    add_undirected(g, P, Q, L);
  }
  
  auto res = yen_algorithm(g, X, Y, K);
  
  for(auto x : res){
    if(not x){
      std::cout << -1 << "\n";
    }else{
      std::cout << std::fixed << std::setprecision(12) << x->first << "\n";
    }
  }

  return 0;
}
0