結果
問題 | No.1324 Approximate the Matrix |
ユーザー | tko919 |
提出日時 | 2020-12-21 00:47:32 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 100 ms / 2,000 ms |
コード長 | 4,641 bytes |
コンパイル時間 | 2,050 ms |
コンパイル使用メモリ | 191,548 KB |
実行使用メモリ | 6,144 KB |
最終ジャッジ日時 | 2024-09-21 12:36:37 |
合計ジャッジ時間 | 4,081 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 2 ms
5,376 KB |
testcase_02 | AC | 2 ms
5,376 KB |
testcase_03 | AC | 50 ms
5,376 KB |
testcase_04 | AC | 48 ms
5,376 KB |
testcase_05 | AC | 49 ms
5,376 KB |
testcase_06 | AC | 48 ms
5,376 KB |
testcase_07 | AC | 49 ms
5,376 KB |
testcase_08 | AC | 7 ms
5,376 KB |
testcase_09 | AC | 4 ms
5,376 KB |
testcase_10 | AC | 7 ms
5,376 KB |
testcase_11 | AC | 15 ms
5,376 KB |
testcase_12 | AC | 4 ms
5,376 KB |
testcase_13 | AC | 3 ms
5,376 KB |
testcase_14 | AC | 16 ms
5,376 KB |
testcase_15 | AC | 8 ms
5,376 KB |
testcase_16 | AC | 2 ms
5,376 KB |
testcase_17 | AC | 12 ms
5,376 KB |
testcase_18 | AC | 4 ms
5,376 KB |
testcase_19 | AC | 5 ms
5,376 KB |
testcase_20 | AC | 4 ms
5,376 KB |
testcase_21 | AC | 3 ms
5,376 KB |
testcase_22 | AC | 6 ms
5,376 KB |
testcase_23 | AC | 12 ms
5,376 KB |
testcase_24 | AC | 20 ms
5,376 KB |
testcase_25 | AC | 13 ms
5,376 KB |
testcase_26 | AC | 11 ms
5,376 KB |
testcase_27 | AC | 7 ms
5,376 KB |
testcase_28 | AC | 2 ms
5,376 KB |
testcase_29 | AC | 2 ms
5,376 KB |
testcase_30 | AC | 2 ms
5,376 KB |
testcase_31 | AC | 2 ms
5,376 KB |
testcase_32 | AC | 2 ms
5,376 KB |
testcase_33 | AC | 2 ms
5,376 KB |
testcase_34 | AC | 1 ms
5,376 KB |
testcase_35 | AC | 1 ms
5,376 KB |
testcase_36 | AC | 1 ms
5,376 KB |
testcase_37 | AC | 98 ms
6,144 KB |
testcase_38 | AC | 100 ms
6,144 KB |
testcase_39 | AC | 98 ms
6,016 KB |
testcase_40 | AC | 95 ms
6,144 KB |
testcase_41 | AC | 99 ms
6,144 KB |
testcase_42 | AC | 10 ms
5,376 KB |
testcase_43 | AC | 10 ms
5,376 KB |
testcase_44 | AC | 10 ms
5,376 KB |
コンパイルメッセージ
main.cpp: In member function 'bool MinCostFlow<Flow, Cost, type>::dual(const Flow&)': main.cpp:40:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17' [-Wc++17-extensions] 40 | auto [d,u]=pq.top(); pq.pop(); | ^ main.cpp: In function 'int main()': main.cpp:141:9: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17' [-Wc++17-extensions] 141 | auto [tmp,add]=mcf.run<int>(); | ^
ソースコード
#define _USE_MATH_DEFINES #include <bits/stdc++.h> using namespace std; //template #define rep(i,a,b) for(int i=(int)(a);i<(int)(b);i++) #define ALL(v) (v).begin(),(v).end() typedef long long int ll; const int inf = 0x3fffffff; const ll INF = 0x1fffffffffffffff; const double eps=1e-12; template<typename T>inline bool chmax(T& a,T b){if(a<b){a=b;return 1;}return 0;} template<typename T>inline bool chmin(T& a,T b){if(a>b){a=b;return 1;}return 0;} //end template<typename Flow,typename Cost,int type=1>class MinCostFlow{ //Maximize=-1 struct ptr{int v_id,e_id;}; struct edge{ int from,to; Flow flow,cap; Cost weight; int rev; edge(int _f,int _t,Flow _c,Cost _w,int _r) :from(_f),to(_t),flow(0),cap(_c),weight(_w),rev(_r){} Flow residual_cap()const{return cap-flow;} }; int n; vector<vector<edge>> g; vector<Flow> b,pot; vector<ptr> es; Cost farthest; vector<Cost> dist; vector<edge*> par; vector<int> exc,def; void push(edge& e,Flow amount){ e.flow+=amount; g[e.to][e.rev].flow-=amount; } Cost residual_cost(int from,int to,edge& e){ return e.weight+pot[from]-pot[to]; } bool dual(const Flow& delta){ dist.assign(n,numeric_limits<Cost>::max()); par.assign(n,nullptr); exc.erase(remove_if(ALL(exc),[&](int v){return b[v]<delta;}),exc.end()); def.erase(remove_if(ALL(def),[&](int v){return b[v]>-delta;}),def.end()); priority_queue<pair<Cost,int>,vector<pair<Cost,int>>,greater<>> pq; for(auto& v:exc)pq.push({dist[v]=0,v}); farthest=0; int def_cnt=0; while(!pq.empty()){ auto [d,u]=pq.top(); pq.pop(); if(dist[u]<d)continue; farthest=d; if(b[u]<=-delta)def_cnt++; if(def_cnt>=(int)def.size())break; for(auto& e:g[u]){ if(e.residual_cap()<delta)continue; int v=e.to; Cost nd=d+residual_cost(u,v,e); if(nd>=dist[v])continue; pq.push({dist[v]=nd,v}); par[v]=&e; } } rep(v,0,n)pot[v]+=min(dist[v],farthest); return def_cnt>0; } void primal(const Flow& delta){ for(auto& t:def){ if(dist[t]>farthest)continue; Flow f=-b[t]; int v; for(v=t;par[v]!=nullptr;v=par[v]->from){ chmin(f,par[v]->residual_cap()); } chmin(f,b[v]); f-=f%delta; if(f<=0)continue; for(v=t;par[v]!=nullptr;){ auto& e=*par[v]; push(e,f); int u=par[v]->from; if(e.residual_cap()<=0)par[v]=nullptr; v=u; } b[t]+=f; b[v]-=f; } } public: MinCostFlow(int _n):n(_n),g(_n),b(_n),pot(_n){} void add_edge(int from,int to,Flow lb,Flow ub,Cost cost){ int f_id=g[from].size(),t_id=(from==to?f_id+1:g[to].size()); g[from].push_back(edge(from,to,ub,cost*type,t_id)); g[to].push_back(edge(to,from,-lb,-cost*type,f_id)); es.push_back(ptr{from,f_id}); } void add_supply(int v,Flow amount){b[v]+=amount;} void add_demand(int v,Flow amount){b[v]-=amount;} Flow get_pot(int v){return pot[v];} Flow get_flow(int v){return g[es[v].v_id][es[v].e_id].flow;} template<typename T>pair<bool,T> run(const Flow& sf=2){ Flow max_flow=1; for(auto& t:b)chmax(max_flow,abs(t)); for(auto& es:g)for(auto& e:es)chmax(max_flow,abs(e.residual_cap())); Flow delta=1; while(delta<max_flow)delta*=sf; for(;delta;delta/=sf){ for(auto& es:g)for(auto& e:es){ Flow rcap=e.residual_cap(); rcap-=rcap%delta; Cost rcost=residual_cost(e.from,e.to,e); if(rcost<0 or rcap<0){ push(e,rcap); b[e.from]-=rcap; b[e.to]+=rcap; } } rep(v,0,n)if(b[v]!=0){ (b[v]>0?exc:def).push_back(v); } while(dual(delta))primal(delta); } T res=0; for(auto& es:g)for(auto& e:es)res+=T(e.flow)*T(e.weight); res>>=1; if(exc.empty() and def.empty())return {1,res*type}; else return {0,res*type}; } }; int main(){ int n,k; cin>>n>>k; vector<int> a(n),b(n); rep(i,0,n)cin>>a[i]; rep(i,0,n)cin>>b[i]; MinCostFlow<int,int> mcf(n*2); rep(i,0,n){ mcf.add_supply(i,a[i]); mcf.add_demand(i+n,b[i]); } ll res=0; rep(i,0,n)rep(j,0,n){ int x,lim=min(a[i],b[j]); cin>>x; res+=x*x; int pre=x*x; rep(v,1,lim+1){ mcf.add_edge(i,j+n,0,1,(v-x)*(v-x)-pre); pre=(v-x)*(v-x); } } auto [tmp,add]=mcf.run<int>(); assert(tmp); res+=add; cout<<res<<endl; return 0; }