QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#451391#8759. 小班课kilo_tobo_tarjenCompile Error//C++145.0kb2024-06-23 10:34:182024-06-23 10:34:19

Judging History

你现在查看的是最新测评结果

  • [2024-06-23 10:34:19]
  • 评测
  • [2024-06-23 10:34:18]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
const char el = '\n';
typedef long long ll;
namespace costflow {

typedef long long flow_t;
typedef long long cost_t;

const flow_t inf_flow = 1e18;
const cost_t inf_cost = 1e18;

struct CostEdge {
  int from, to;
  cost_t cost;
  flow_t cap, low = 0, flow = 0;
};

int num_node(const std::vector<CostEdge> &edges) {
  int n = 0;
  for (const auto &e : edges) n = std::max({n, e.from, e.to});
  return n;
}
std::pair<flow_t, cost_t> get_flow(const std::vector<CostEdge> &edges, int s) {
  flow_t flow = 0;
  cost_t cost = 0;
  for (const auto &e : edges) {
    if (e.from == s) flow += e.flow;
    cost += e.flow * e.cost;
  }
  return {flow, cost};
}

struct CostFlow {
  struct Edge {
    int from, to;
    cost_t cost;
    flow_t cap;
  };
  int n;
  std::vector<std::vector<int>> eid;
  std::vector<Edge> edge;
  void build(const std::vector<CostEdge> &edges) {
    n = num_node(edges);
    eid.assign(n + 1, {});
    edge.clear();
    int num_edges = 0;
    for (const auto &e : edges) {
      eid[e.from].push_back(num_edges++);
      edge.push_back({e.from, e.to, e.cost, e.cap - e.flow});
      eid[e.to].push_back(num_edges++);
      edge.push_back({e.to, e.from, -e.cost, e.flow});
    }
  }
  std::vector<cost_t> dis;
  std::vector<int> pre;
  bool spfa(int s, int t) {
    if (s > n || t > n) return false;
    dis.assign(n + 1, inf_cost);
    pre.assign(n + 1, 0);
    std::vector<bool> inque(n + 1);
    std::queue<int> que;
    dis[s] = 0;
    que.push(s);
    inque[s] = true;
    while (que.size()) {
      int u = que.front();
      // cerr << 'u' << ' ' << u << endl;
      que.pop();
      inque[u] = false;
      for (auto i : eid[u]) {
        const auto &e = edge[i];
        if (e.cap && dis[e.to] > dis[u] + e.cost) {
          dis[e.to] = dis[u] + e.cost;
          pre[e.to] = i;
          if (!inque[e.to]) {
            que.push(e.to);
            inque[e.to] = true;
          }
        }
      }
    }
    return dis[t] < inf_cost;
  }
  std::pair<flow_t, cost_t> maxflow(int s, int t) {
    flow_t flow = 0;
    cost_t cost = 0;
    while (spfa(s, t)) {
      flow_t detf = inf_flow;
      cost_t detc = 0;
      for (int u = t, i = pre[u]; u != s; u = edge[i].from, i = pre[u]) {
        detf = std::min(detf, edge[i].cap);
        detc += edge[i].cost;
      }
      for (int u = t, i = pre[u]; u != s; u = edge[i].from, i = pre[u]) {
        edge[i].cap -= detf;
        edge[i ^ 1].cap += detf;
      }
      flow += detf;
      cost += detf * detc;
    }
    return {flow, cost};
  }
  std::vector<CostEdge> to_edge() {
    std::vector<CostEdge> edges;
    for (int i = 0; i < edge.size(); i += 2)
      edges.push_back({
          .from = edge[i].from,
          .to = edge[i].to,
          .cost = edge[i].cost,
          .cap = edge[i].cap + edge[i ^ 1].cap,
          .flow = edge[i ^ 1].cap,
      });
    return edges;
  }
};

}  // namespace costflow
using namespace costflow;

struct graph {
  vector<vector<int>> e;
  graph(int n) : e(n + 1) {}
  void adde(int u, int v) { e[u].push_back(v); }
  optional<vector<int>> toposort() {
    int n = e.size() - 1;
    vector<int> d(n + 1);
    queue<int> que;
    for (int u = 1; u <= n; u++)
      for (auto v : e[u]) d[v]++;
    for (int i = 1; i <= n; i++)
      if (!d[i]) que.push(i);
    vector<int> res;
    while (!que.empty()) {
      auto u = que.front();
      res.push_back(u);
      que.pop();
      for (auto v : e[u]) {
        if (!--d[v]) que.push(v);
      }
    }
    if (res.size() != n) return nullopt;
    return res;
  }
};
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << setprecision(15);
  int tt;
  cin >> tt;
  while (tt--) {
    int n, m;
    cin >> n >> m;
    vector<CostEdge> edge;
    auto id1 = [&](int i) { return i; };
    auto id2 = [&](int i) { return i + n; };
    int s = n + m + 1, t = n + m + 2;
    for (int i = 1; i <= m; i++) {
      int b;
      cin >> b;
      edge.push_back({.from = id2(i), .to = t, .cost = 0, .cap = b});
    }
    for (int i = 1; i <= n; i++) {
      int k;
      cin >> k;
      vector<int> a(k);
      for (auto &v : a) cin >> v;
      edge.push_back({.from = s, .to = id1(i), .cost = 0, .cap = 1});
      for (int j = 0; j < a.size(); j++)
        edge.push_back({.from = id1(i), .to = id2(a[j]), .cost = j, .cap = 1});
    }
    CostFlow cf;
    cf.build(edge);
    auto [f, c] = cf.maxflow(s, t);
    edge = cf.to_edge();
    cout << f << el;
    graph g(n + m);
    vector<int> cost(n + 1);
    for (auto e : edge)
      if (e.from <= n && e.flow) g.adde(e.to, e.from), cost[e.from] = e.cost;
    for (auto e : edge)
      if (e.from <= n && !e.flow && e.cost < cost[e.from]) g.adde(e.from, e.to);
    auto res = *g.toposort();
    reverse(res.begin(), res.end());
    for (auto v : res)
      if (v <= n) cout << v << ' ';
    cout << el;
  }
  return 0;
}

詳細信息

answer.code:125:3: error: ‘optional’ does not name a type
  125 |   optional<vector<int>> toposort() {
      |   ^~~~~~~~
answer.code: In function ‘int main()’:
answer.code:175:10: warning: structured bindings only available with ‘-std=c++17’ or ‘-std=gnu++17’ [-Wc++17-extensions]
  175 |     auto [f, c] = cf.maxflow(s, t);
      |          ^
answer.code:184:19: error: ‘struct graph’ has no member named ‘toposort’
  184 |     auto res = *g.toposort();
      |                   ^~~~~~~~