結果
問題 | No.1324 Approximate the Matrix |
ユーザー | theory_and_me |
提出日時 | 2020-12-05 17:59:40 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
CE
(最新)
AC
(最初)
|
実行時間 | - |
コード長 | 8,972 bytes |
コンパイル時間 | 728 ms |
コンパイル使用メモリ | 80,116 KB |
最終ジャッジ日時 | 2024-11-14 23:56:31 |
合計ジャッジ時間 | 1,252 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
コンパイルエラー時のメッセージ・ソースコードは、提出者また管理者しか表示できないようにしております。(リジャッジ後のコンパイルエラーは公開されます)
ただし、clay言語の場合は開発者のデバッグのため、公開されます。
ただし、clay言語の場合は開発者のデバッグのため、公開されます。
コンパイルメッセージ
main.cpp:17:14: error: 'function' in namespace 'std' does not name a template type 17 | std::function<Cost(Flow)> F; // must be discrete convex | ^~~~~~~~ main.cpp:9:1: note: 'std::function' is defined in header '<functional>'; did you forget to '#include <functional>'? 8 | #include <iostream> +++ |+#include <functional> 9 | main.cpp:41:36: error: 'std::function' has not been declared 41 | int add_edge(int from, int to, std::function<Cost(Flow)> F, Flow cap, Flow flow){ | ^~~ main.cpp:41:49: error: expected ',' or '...' before '<' token 41 | int add_edge(int from, int to, std::function<Cost(Flow)> F, Flow cap, Flow flow){ | ^ main.cpp: In member function 'int mccf_graph<Flow, Cost>::add_edge(int, int, int)': main.cpp:46:50: error: 'F' was not declared in this scope 46 | E.push_back(edge_with_function{from, to, F, cap, flow}); | ^ main.cpp:46:53: error: 'cap' was not declared in this scope 46 | E.push_back(edge_with_function{from, to, F, cap, flow}); | ^~~ main.cpp:46:58: error: 'flow' was not declared in this scope; did you mean 'Flow'? 46 | E.push_back(edge_with_function{from, to, F, cap, flow}); | ^~~~ | Flow main.cpp: In function 'int main()': main.cpp:289:19: error: no matching function for call to 'mccf_graph<long int, long int>::add_edge(int&, int&, main()::<lambda(int64_t)>&, __gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type&, int)' 289 | G.add_edge(s, i, F, A[i], 0); | ~~~~~~~~~~^~~~~~~~~~~~~~~~~~ main.cpp:41:9: note: candidate: 'int mccf_graph<Flow, Cost>::add_edge(int, int, int) [with Flow = long int; Cos
ソースコード
// 想定解3 O(N^2)本の辺を張り,流すたびに辺のコストを書き換える #include <algorithm> #include <cassert> #include <limits> #include <queue> #include <vector> #include <iostream> template<class Flow, class Cost> struct mccf_graph{ int n; // input edges struct edge_with_function{ int from, to; std::function<Cost(Flow)> F; // must be discrete convex Flow cap, flow; }; // edges on the residual graph struct edge{ int to, rev, id; bool available; Cost cost; bool is_rev; // for debug void print_edge(){ std::cerr << "to: " << to << " cost: " << cost << "\n";} }; std::vector<std::vector<edge>> g; std::vector<edge_with_function> E; std::vector<Cost> dual; mccf_graph() {} mccf_graph(int n) : n(n), g(n){ dual.resize(n, 0); } int add_edge(int from, int to, std::function<Cost(Flow)> F, Flow cap, Flow flow){ assert(0 <= from and from < n); assert(0 <= to and to < n); // assert(cap > 0); int m = int(E.size()); E.push_back(edge_with_function{from, to, F, cap, flow}); return m; } Cost calc_objective(){ Cost obj = 0; for(auto &e: E){ obj += e.F(e.flow); } return obj; } /* for debug void print_g(){ for(int i=0;i<n;i++){ for(edge &e: g[i]){ std::cerr << "to " << e.to; std::cerr << " rev " << e.rev; std::cerr << " id " << e.id; std::cerr << " avail " << (int)e.available; std::cerr << " cost " << e.cost; std::cerr << " r_cost " << e.cost - dual[e.to] + dual[i]; std::cerr << " is_rev " << (int)e.is_rev; std::cerr << "\n"; } } std::cerr << "\n"; } void print_E(){ for(auto &e: E){ std::cerr << "from " << e.from; std::cerr << " to " << e.to; std::cerr << " cap " << e.cap; std::cerr << " flow " << e.flow; std::cerr << "\n"; } std::cerr << "\n"; } for debug */ void solve_SSP(int s, int t){ solve_SSP(s, t, std::numeric_limits<Flow>::max()); } void solve_SSP(int s, int t, Flow flow_limit){ assert(0 <= s and s < n); assert(0 <= t and t < n); assert(s != t); // construct a residual graph int m = E.size(); for(int i=0;i<m;i++){ auto &e = E[i]; if(!e.cap) continue; g[e.from].push_back(edge{e.to, (int)g[e.to].size(), i, true, e.F(1) - e.F(0), false}); g[e.to].push_back(edge{e.from, (int)g[e.from].size()-1, i, false, 0, true}); } // get flow std::vector<Cost> dist(n, 0); std::vector<int> pv(n), pe(n); std::vector<bool> vis(n); auto dual_ref_Dijkstra = [&]()->bool{ fill(dist.begin(), dist.end(), std::numeric_limits<Cost>::max()); fill(pv.begin(), pv.end(), -1); fill(pe.begin(), pe.end(), -1); fill(vis.begin(), vis.end(), false); struct Q{ Cost key; int to; bool operator<(Q r) const {return key > r.key;}; }; std::priority_queue<Q> que; dist[s] = 0; que.push(Q{0, s}); while(!que.empty()){ int v = que.top().to; que.pop(); if(vis[v]) continue; vis[v] = true; if(v == t) break; for(int i=0;i<(int)g[v].size();i++){ auto &e = g[v][i]; if(vis[e.to] or !e.available) continue; Cost cost = e.cost - dual[e.to] + dual[v]; if(dist[e.to] - dist[v] > cost){ dist[e.to] = dist[v] + cost; pv[e.to] = v; pe[e.to] = i; que.push(Q{dist[e.to], e.to}); } } } if(!vis[t]){ return false; } // assert(vis[t]); for(int v=0;v<n;v++){ if(!vis[v]) continue; dual[v] -= dist[t] - dist[v]; } return true; }; auto dual_ref_BF = [&]()->bool{ fill(dist.begin(), dist.end(), std::numeric_limits<Cost>::max()); fill(pv.begin(), pv.end(), -1); fill(pe.begin(), pe.end(), -1); fill(vis.begin(), vis.end(), false); dist[s] = 0; vis[s] = true; for(int itr=0;itr<n-1;itr++){ for(int v=0;v<n;v++){ // if(dist[v] == INF_cost) continue; if(!vis[v]) continue; for(int i=0;i<(int)g[v].size();i++){ auto &e = g[v][i]; if(!e.available) continue; if(dist[e.to] > dist[v] + e.cost){ dist[e.to] = dist[v] + e.cost; pv[e.to] = v; pe[e.to] = i; vis[e.to] = true; } } } } // check feasibility if(!vis[t]){ return false; } // detect negative cycle for(int v=0;v<n;v++){ // if(dist[v] == INF_cost) continue; if(!vis[v]) continue; for(auto &e: g[v]){ if(!e.available) continue; assert(dist[v] + e.cost >= dist[e.to]); } } for(int v=0;v<n;v++){ // assert(dist[v]<INF_cost); if(!vis[v]) continue; dual[v] -= dist[t] - dist[v]; } return true; }; auto update_edge = [&](edge &e){ auto &e_func = E[e.id]; if(e.is_rev){ if(e_func.flow == 0){ e.available = false; e.cost = 0; }else{ e.available = true; e.cost = e_func.F(e_func.flow-1) - e_func.F(e_func.flow); } }else{ if(e_func.flow == e_func.cap){ e.available = false; e.cost = 0; }else{ e.available = true; e.cost = e_func.F(e_func.flow+1) - e_func.F(e_func.flow); } } return; }; Flow flow_cur = 0; while(flow_cur < flow_limit){ if(!flow_cur){ if(!dual_ref_BF()) break; }else{ if(!dual_ref_Dijkstra()) break; } for(int v=t;v!=s;v=pv[v]){ auto& e = g[pv[v]][pe[v]]; auto& e_func = E[e.id]; auto& re = g[v][e.rev]; if(e.is_rev) e_func.flow--; else e_func.flow++; update_edge(e); update_edge(re); } flow_cur++; } } }; int main(){ int N, M; std::cin >> N >> M; std::vector<int> A(N), B(N); for(int i=0;i<N;i++) std::cin >> A[i]; for(int i=0;i<N;i++) std::cin >> B[i]; std::vector<std::vector<int>> T(N, std::vector<int>(N, 0)); for(int i=0;i<N;i++){ for(int j=0;j<N;j++){ std::cin >> T[i][j]; } } // assert int A_sum = 0, B_sum = 0; for(int i=0;i<N;i++){ assert(A[i] >= 0); A_sum += A[i]; } for(int i=0;i<N;i++){ assert(B[i] >= 0); B_sum += B[i]; } assert(A_sum == M); assert(B_sum == M); // graph construction mccf_graph<int64_t, int64_t> G(2*N+2); int s = 2*N; int t = s+1; int INF_cap = 1e9; for(int i=0;i<N;i++){ auto F = [](int64_t x){return 0;}; G.add_edge(s, i, F, A[i], 0); } for(int i=0;i<N;i++){ auto F = [](int64_t x){return 0;}; G.add_edge(N+i, t, F, B[i], 0); } for(int i=0;i<N;i++){ for(int j=0;j<N;j++){ auto F = [&T, i, j](int64_t x){return (x-T[i][j])*(x-T[i][j]);}; G.add_edge(i, N+j, F, INF_cap, 0); } } G.solve_SSP(s, t, M); std::vector<std::vector<int>> res(N, std::vector<int>(N, 0)); for(auto &e: G.E){ if(e.from != s and e.to!= t){ res[e.from][e.to-N] = e.flow; } } // for(int i=0;i<N;i++){ // for(int j=0;j<N;j++){ // std::cout << res[i][j] << " "; // } // std::cout << "\n"; // } std::cout << G.calc_objective() << "\n"; return 0; }