Submission #46984895


Source Code Expand

#include <bits/stdc++.h>
#ifdef LOCAL
#include "debug.h"
#else
#define debug(...)
#endif
using int64 = long long;
using uint = unsigned int;
using uint64 = unsigned long long;
bool ckmin(auto& a, auto b) { return b < a ? a = b, 1 : 0; }
bool ckmax(auto& a, auto b) { return b > a ? a = b, 1 : 0; }
using namespace std;

template <typename Graph, typename F>
auto Dijkstra(Graph& g, int s, F f) {
  assert(g.IsDirected());
  using dist_t = std::invoke_result_t<F, typename Graph::edge_t&>;
  std::vector<dist_t> dist(g.n, std::numeric_limits<dist_t>::max());
  std::vector<int> from(g.n, -1);
  std::vector<bool> visit(g.n);
  dist[s] = 0;
  auto Cmp = [&](auto& p, auto& q) { return p.second > q.second; };
  std::priority_queue<std::pair<int, dist_t>,
                      std::vector<std::pair<int, dist_t>>, decltype(Cmp)>
      q(Cmp);
  q.push({s, 0});
  while (!q.empty()) {
    auto [u, du] = q.top();
    q.pop();
    if (visit[u]) continue;
    visit[u] = true;
    for (auto eid : g.go[u]) {
      auto& e = g.edges[eid];
      int v = e.v;
      if (visit[v]) continue;
      dist_t d = dist[u] + f(e);
      if (d < dist[v]) {
        dist[v] = d;
        from[v] = u;
        q.push({v, d});
      }
    }
  }
  return std::make_tuple(dist, from, visit);
}

struct EdgeBase {
  int u, v;
  operator std::string() {
    return "Edge(" + std::to_string(u) + ", " + std::to_string(v) + ")";
  }
};

template <typename Graph>
struct GraphNeighborsRange {
  struct Iterator {
    Graph* g;
    int u, id;
    bool is_directed;
    Iterator(Graph* g_, int u_, int id_) : g(g_), u(u_), id(id_) {
      is_directed = g->IsDirected();
    }
    Iterator& operator++() {
      id++;
      return *this;
    }
    bool operator!=(const Iterator& other) { return id != other.id; }
    int operator*() {
      assert(id < (int)g->go[u].size());
      auto& e = g->edges[g->go[u][id]];
      if (is_directed) {
        return e.v;
      } else {
        return u ^ e.u ^ e.v;
      }
    }
  };
  Graph* g;
  int u;
  GraphNeighborsRange(Graph* g_, int u_) : g(g_), u(u_) {
    assert(0 <= u && u < g->n);
  }
  Iterator begin() { return Iterator(g, u, 0); }
  Iterator end() { return Iterator(g, u, g->go[u].size()); }
};

template <typename Graph>
struct GraphEdgesRange {
  struct Iterator {
    Graph* g;
    int u, id;
    Iterator(Graph* g_, int u_, int id_) : g(g_), u(u_), id(id_) {}
    Iterator& operator++() {
      id++;
      return *this;
    }
    bool operator!=(const Iterator& other) { return id != other.id; }
    typename Graph::edge_t& operator*() {
      assert(id < (int)g->go[u].size());
      return g->edges[g->go[u][id]];
    }
  };
  Graph* g;
  int u;
  GraphEdgesRange(Graph* g_, int u_) : g(g_), u(u_) {
    assert(0 <= u && u < g->n);
  }
  Iterator begin() { return Iterator(g, u, 0); }
  Iterator end() { return Iterator(g, u, g->go[u].size()); }
};

template <typename Edge>
struct Graph {
  using edge_t = Edge;
  int n;
  std::vector<Edge> edges;
  std::vector<std::vector<int>> go;
  Graph(int n_) : n(n_) { go.resize(n); }
  virtual bool IsDirected() const { assert(0); }
  virtual void AddEdge(Edge e) = 0;
  GraphNeighborsRange<Graph> Neighbors(int u) {
    return GraphNeighborsRange<Graph>(this, u);
  }
  GraphEdgesRange<Graph> Edges(int u) {
    return GraphEdgesRange<Graph>(this, u);
  }
};

template <typename Edge>
struct UndirectedGraph : public Graph<Edge> {
  UndirectedGraph(int n_) : Graph<Edge>(n_) {}
  virtual bool IsDirected() const { return false; }
  virtual void AddEdge(Edge e) {
    assert(e.u >= 0 && e.u < this->n && e.v >= 0 && e.v < this->n);
    this->edges.push_back(e);
    this->go[e.u].push_back(this->edges.size() - 1);
    this->go[e.v].push_back(this->edges.size() - 1);
  }
};

template <typename Edge>
struct DirectedGraph : public Graph<Edge> {
  DirectedGraph(int n_) : Graph<Edge>(n_) {}
  virtual bool IsDirected() const { return true; }
  virtual void AddEdge(Edge e) {
    assert(e.u >= 0 && e.u < this->n && e.v >= 0 && e.v < this->n);
    this->edges.push_back(e);
    this->go[e.u].push_back(this->edges.size() - 1);
  }
};

template <typename Edge>
struct FlowGraph : public DirectedGraph<Edge> {
  using flow_t = decltype(std::declval<Edge>().cap);
  FlowGraph(int n_) : DirectedGraph<Edge>(n_) {}
  static const flow_t eps = (flow_t)1e-7;
  virtual void AddEdge(Edge) { assert(false); }
  void AddFlowEdge(Edge e) {
    DirectedGraph<Edge>::AddEdge(e);
    std::swap(e.u, e.v);
    e.cap = 0;
    DirectedGraph<Edge>::AddEdge(e);
  }
  void AddFlowEdgeWithCost(Edge e) {
    DirectedGraph<Edge>::AddEdge(e);
    std::swap(e.u, e.v);
    e.cap = 0;
    e.cost *= -1;
    DirectedGraph<Edge>::AddEdge(e);
  }
};

struct Edge {
  int u, v;
  int64 w;
};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  int n;
  int64 A, B, C;
  cin >> n >> A >> B >> C;
  vector D(n, vector<int64>(n));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) cin >> D[i][j];
  }

  DirectedGraph<Edge> graph(2 * n);
  for (int i = 0; i < n; i++) {
    graph.AddEdge({i, i + n, 0});
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      graph.AddEdge({i, j, D[i][j] * A});
      graph.AddEdge({i + n, j + n, D[i][j] * B + C});
    }
  }

  auto [dist, from, visit] = Dijkstra(graph, 0, [](auto& e) { return e.w; });
  int64 ans = min(dist[n - 1], dist[n - 1 + n]);
  cout << ans << '\n';

  return 0;
}

Submission Info

Submission Time
Task E - Our clients, please wait a moment
User xindubawukong
Language C++ 20 (gcc 12.2)
Score 450
Code Size 5627 Byte
Status AC
Exec Time 116 ms
Memory 53048 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 450 / 450
Status
AC × 3
AC × 39
Set Name Test Cases
Sample sample00.txt, sample01.txt, sample02.txt
All sample00.txt, sample01.txt, sample02.txt, testcase00.txt, testcase01.txt, testcase02.txt, testcase03.txt, testcase04.txt, testcase05.txt, testcase06.txt, testcase07.txt, testcase08.txt, testcase09.txt, testcase10.txt, testcase11.txt, testcase12.txt, testcase13.txt, testcase14.txt, testcase15.txt, testcase16.txt, testcase17.txt, testcase18.txt, testcase19.txt, testcase20.txt, testcase21.txt, testcase22.txt, testcase23.txt, testcase24.txt, testcase25.txt, testcase26.txt, testcase27.txt, testcase28.txt, testcase29.txt, testcase30.txt, testcase31.txt, testcase32.txt, testcase33.txt, testcase34.txt, testcase35.txt
Case Name Status Exec Time Memory
sample00.txt AC 1 ms 3500 KiB
sample01.txt AC 1 ms 3484 KiB
sample02.txt AC 1 ms 3308 KiB
testcase00.txt AC 92 ms 50408 KiB
testcase01.txt AC 79 ms 46876 KiB
testcase02.txt AC 79 ms 46916 KiB
testcase03.txt AC 91 ms 50452 KiB
testcase04.txt AC 85 ms 47564 KiB
testcase05.txt AC 82 ms 47316 KiB
testcase06.txt AC 90 ms 50576 KiB
testcase07.txt AC 90 ms 50492 KiB
testcase08.txt AC 95 ms 50572 KiB
testcase09.txt AC 96 ms 50448 KiB
testcase10.txt AC 97 ms 50372 KiB
testcase11.txt AC 92 ms 50752 KiB
testcase12.txt AC 89 ms 47668 KiB
testcase13.txt AC 95 ms 50088 KiB
testcase14.txt AC 98 ms 50484 KiB
testcase15.txt AC 93 ms 50732 KiB
testcase16.txt AC 92 ms 47568 KiB
testcase17.txt AC 86 ms 47192 KiB
testcase18.txt AC 97 ms 50752 KiB
testcase19.txt AC 93 ms 50568 KiB
testcase20.txt AC 95 ms 50492 KiB
testcase21.txt AC 82 ms 46960 KiB
testcase22.txt AC 100 ms 50712 KiB
testcase23.txt AC 92 ms 50372 KiB
testcase24.txt AC 93 ms 50340 KiB
testcase25.txt AC 90 ms 47960 KiB
testcase26.txt AC 95 ms 47440 KiB
testcase27.txt AC 80 ms 47136 KiB
testcase28.txt AC 81 ms 46956 KiB
testcase29.txt AC 95 ms 50756 KiB
testcase30.txt AC 96 ms 50752 KiB
testcase31.txt AC 90 ms 50676 KiB
testcase32.txt AC 116 ms 52340 KiB
testcase33.txt AC 115 ms 53048 KiB
testcase34.txt AC 107 ms 52012 KiB
testcase35.txt AC 92 ms 50736 KiB