結果

問題 No.17 2つの地点に泊まりたい
ユーザー not_522not_522
提出日時 2015-07-16 21:09:48
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 4,145 bytes
コンパイル時間 1,610 ms
コンパイル使用メモリ 171,724 KB
実行使用メモリ 5,248 KB
最終ジャッジ日時 2024-10-15 01:24:41
合計ジャッジ時間 2,313 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 1 ms
5,248 KB
testcase_02 AC 2 ms
5,248 KB
testcase_03 AC 2 ms
5,248 KB
testcase_04 AC 2 ms
5,248 KB
testcase_05 AC 2 ms
5,248 KB
testcase_06 AC 2 ms
5,248 KB
testcase_07 AC 2 ms
5,248 KB
testcase_08 AC 2 ms
5,248 KB
testcase_09 AC 2 ms
5,248 KB
testcase_10 AC 2 ms
5,248 KB
testcase_11 AC 2 ms
5,248 KB
testcase_12 AC 2 ms
5,248 KB
testcase_13 AC 2 ms
5,248 KB
testcase_14 AC 1 ms
5,248 KB
testcase_15 AC 1 ms
5,248 KB
testcase_16 AC 1 ms
5,248 KB
testcase_17 AC 2 ms
5,248 KB
testcase_18 AC 2 ms
5,248 KB
testcase_19 AC 2 ms
5,248 KB
testcase_20 AC 2 ms
5,248 KB
testcase_21 AC 1 ms
5,248 KB
testcase_22 AC 2 ms
5,248 KB
testcase_23 AC 2 ms
5,248 KB
testcase_24 AC 2 ms
5,248 KB
testcase_25 AC 1 ms
5,248 KB
testcase_26 AC 2 ms
5,248 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

struct Edge {
  typedef int CostType;
  const static int cost = 1;
  int from, to;
  Edge(int from, int to) : from(from), to(to) {};
};

template<typename Cost> struct WeightedEdge : public Edge {
  typedef Cost CostType;
  Cost cost;
  WeightedEdge(int from, int to, Cost cost = 0) : Edge(from, to), cost(cost) {}
};

template<typename Capacity> struct ResidualEdge : public Edge {
  typedef Capacity CapacityType;
  Capacity cap;
  int rev;
  ResidualEdge(int from, int to, Capacity cap) : Edge(from, to), cap(cap) {}
  ResidualEdge reverse() const {return ResidualEdge(to, from, 0);}
};

template<typename Capacity, typename Cost> struct WeightedResidualEdge : public ResidualEdge<Capacity> {
  Cost cost;
  WeightedResidualEdge(int from, int to, Capacity cap, Cost cost) : ResidualEdge<Capacity>(from, to, cap), cost(cost) {}
  WeightedResidualEdge reverse() const {return WeightedResidualEdge(this->to, this->from, 0, -cost);}
};

template<typename Edge> class Graph {
public:
  typedef Edge EdgeType;
  virtual int size() const = 0;
  template<typename... Args> void addEdge(Args...) {}
  template<typename... Args> void addUndirectedEdge(Args...) {}
  virtual vector<Edge> getEdges() const = 0;
  virtual vector<Edge> getEdges(int from) const = 0;
  virtual vector<Edge> getEdges(int from, int to) const = 0;
  virtual int getDegree(int v) const = 0;
};

template<typename Edge> class AdjacencyList : public Graph<Edge> {
protected:
  vector<vector<Edge>> graph;

public:
  AdjacencyList(int n) : graph(n) {}

  int size() const {
    return graph.size();
  }
  
  template<typename... Args> void addEdge(Args... args) {
    Edge edge(args...);
    graph[edge.from].emplace_back(edge);
  }

  template<typename... Args> void addUndirectedEdge(Args... args) {
    Edge edge(args...);
    addEdge(edge);
    swap(edge.from, edge.to);
    addEdge(edge);
  }

  vector<Edge> getEdges() const {
    vector<Edge> res;
    for (const auto& edges : graph) {
      res.insert(res.end(), edges.begin(), edges.end());
    }
    return res;
  }

  vector<Edge> getEdges(int from) const {
    return graph[from];
  }

  vector<Edge> getEdges(int from, int to) const {
    vector<Edge> res;
    for (const auto& edge : graph[from]) {
      if (edge.to == to) res.emplace_back(edge);
    }
    return res;
  }

  int getDegree(int v) const {
    return graph[v].size();
  }

  vector<Edge>& operator[](int v) {
    return graph[v];
  }
};

template<typename Graph> class WarshallFloyd {
private:
  typedef typename Graph::EdgeType::CostType Cost;

  const Graph& graph;

public:
  const static Cost INF = numeric_limits<Cost>::max() / 2 - 1;

  vector<vector<Cost>> dis;
  
  WarshallFloyd(const Graph& graph) : graph(graph), dis(graph.size(), vector<Cost>(graph.size(), INF)) {
    for (int i = 0; i < graph.size(); ++i) dis[i][i] = 0;
  }
  
  void solve() {
    for (const auto& edge : graph.getEdges()) {
      dis[edge.from][edge.to] = min(dis[edge.from][edge.to], edge.cost);
    }
    for (int k = 0; k < graph.size(); ++k) {
      for (int i = 0; i < graph.size(); ++i) {
        for (int j = 0; j < graph.size(); ++j) {
          dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);
        }
      }
    }
  }
};

template<typename Graph> inline WarshallFloyd<Graph> shortestPath(Graph& graph) {
  WarshallFloyd<Graph> warshallFloyd(graph);
  warshallFloyd.solve();
  return warshallFloyd;
}

int main() {
  typedef AdjacencyList<WeightedEdge<int>> Graph;
  int n;
  cin >> n;
  vector<int> s(n);
  for (int& i : s) cin >> i;
  Graph graph(n);
  int m;
  cin >> m;
  for (int i = 0; i < m; ++i) {
    int a, b, c;
    cin >> a >> b >> c;
    graph.addUndirectedEdge(a, b, c);
  }
  auto dis = shortestPath(graph).dis;
  int res = numeric_limits<int>::max();
  const int INF = WarshallFloyd<Graph>::INF;
  for (int i = 1; i < n - 1; ++i) {
    for (int j = 1; j < n - 1; ++j) {
      if (i == j) continue;
      if (dis[0][i] == INF || dis[i][j] == INF || dis[j][n - 1] == INF) continue;
      res = min(res, dis[0][i] + dis[i][j] + dis[j][n - 1] + s[i] + s[j]);
    }
  }
  cout << res << endl;
}
0