#include using namespace std; using ll=long long; using ull=unsigned long long; using P=pair; templateusing minque=priority_queue,greater>; templatebool chmax(T &a,const T &b){return (abool chmin(T &a,const T &b){return (a>b?(a=b,true):false);} templateistream &operator>>(istream &is,pair&p){is>>p.first>>p.second;return is;} templateistream &operator>>(istream &is,tuple&a){is>>std::get<0>(a)>>std::get<1>(a)>>std::get<2>(a);return is;} templateistream &operator>>(istream &is,array&a){for(auto&i:a)is>>i;return is;} templateistream &operator>>(istream &is,vector &a){for(auto &i:a)is>>i;return is;} templatevoid operator++(pair&a,int n){a.first++,a.second++;} templatevoid operator--(pair&a,int n){a.first--,a.second--;} templatevoid operator++(vector&a,int n){for(auto &i:a)i++;} templatevoid operator--(vector&a,int n){for(auto &i:a)i--;} #define overload3(_1,_2,_3,name,...) name #define rep1(i,n) for(int i=0;i<(int)(n);i++) #define rep2(i,l,r) for(int i=(int)(l);i<(int)(r);i++) #define rep(...) overload3(__VA_ARGS__,rep2,rep1)(__VA_ARGS__) #define reps(i,l,r) rep2(i,l,r) #define all(x) x.begin(),x.end() #define pcnt(x) __builtin_popcountll(x) #define fin(x) return cout<<(x)<<'\n',static_cast(0) #define yn(x) cout<<((x)?"Yes\n":"No\n") #define uniq(x) sort(all(x)),x.erase(unique(all(x)),x.end()) template inline int fkey(vector&z,T key){return lower_bound(z.begin(),z.end(),key)-z.begin();} ll myceil(ll a,ll b){return (a+b-1)/b;} template auto vec(const int (&d)[n],const T &init=T()){ if constexpr (id(d,init)); else return init; } #ifdef LOCAL #include #define SWITCH(a,b) (a) #else #define debug(...) static_cast(0) #define debugg(...) static_cast(0) #define SWITCH(a,b) (b) templateostream &operator<<(ostream &os,const pair&p){os<>testcase; for(int i=0;i template struct MinCostFlow{ private: struct Edge{ int to; Cap cap,flow; Cost cost; int idx; }; struct Edge2{ int from,to; Cap lower,upper; Cost cost; }; static constexpr Cap infcap=std::numeric_limits::max(); static constexpr Cost infcost=std::numeric_limits::max(); int n; std::vectorg; std::vectorcnt,ptr; std::vectorb; std::vector>edges; std::vectorpre_csr; std::vectorpre; std::vectorpotential,dst; std::vectorpos,neg; int pos_ptr,neg_ptr; bool dual(Cap delta){ for(int i=0;i-delta;})-neg.begin(); std::priority_queue,std::vector>,std::greater>>que; for(int i=0;i=delta){ if(dst[e.to]>d+potential[x]-potential[e.to]+e.cost){ dst[e.to]=d+potential[x]-potential[e.to]+e.cost; que.push({dst[e.to],e.to}); pre[e.to]=e.idx; } } } } for(int i=0;i0; } void primal(Cap delta){ for(int i=0;iinv.cap-inv.flow)f=inv.cap-inv.flow; now=e.to; } if(f>b[now])f=b[now]; if(fmincost_flow(){ ptr=cnt; ptr.insert(ptr.begin(),0); for(int i=0;iptr2(n,0); g.resize(ptr[n]); for(auto&&[from,to,lower,upper,cost]:pre_csr){ int gfrom=ptr2[from],gto=ptr2[to]; if(from==to)gto++; g[ptr[from]+(ptr2[from]++)]={to,upper,0,cost,gto}; g[ptr[to]+(ptr2[to]++)]={from,-lower,0,-cost,gfrom}; } Cap delta=1; for(int i=0;i=delta&&e.cost+potential[i]-potential[e.to]<0){ e.flow+=c; g[ptr[e.to]+e.idx].flow-=c; b[i]-=c; b[e.to]+=c; } } for(int i=0;i0)pos[pos_ptr++]=i; else if(b[i]<0)neg[neg_ptr++]=i; } while(dual(delta))primal(delta); delta>>=1; } if(pos_ptr+neg_ptr>0)return std::nullopt; Cost ret=0; for(int i=0;iget_potential(){ std::vectorret(n,0); for(int v=0;v0&&ret[e.to]>ret[i]+e.cost)ret[e.to]=ret[i]+e.cost; } } return ret; } std::vectorget_edge(){ std::vectorret(edges.size()); for(int i=0;i<(int)ret.size();i++){ Edge&e=g[ptr[edges[i].first]+edges[i].second]; Edge&inv=g[ptr[e.to]+e.idx]; ret[i]={edges[i].first,e.to,-inv.cap,e.cap,e.flow,e.cost}; } return ret; } }; void SOLVE(){ int n,m,k,l; cin>>n>>m>>k>>l; MinCostFlowmcf(n+m+2); rep(i,l){ int x,y,z; cin>>x>>y>>z; x--,y--; mcf.add_edge(x,y+n,0,1,-(1ll<