/*? begin "base.hpp" */ #ifndef __clang__ #pragma GCC optimize ("O3") #endif void solve( /* この関数に問題ごとの処理を書く */ #ifdef GCJ_CASE long long case_id #endif ); #if defined(EBUG) && !defined(ONLINE_JUDGE) #define debug true #else #define NDEBUG #define debug false #endif #include #include #include #include #include #include #include #include #include #include #ifdef __cpp_lib_execution #include #endif #include #include #include #include #include using namespace std; using LL=long long; using ULL=unsigned long long; #define int LL #define CS const #define CX constexpr #define IL inline #define OP operator #define RT return #define TL template #define TN typename #define lambda [&] #define times(n,i) uptil(0,n,i) #define rtimes(n,i) downto((n)-1,0,i) #define upto(f,t,i) for(int rabT##i=(t),i=(f);i<=rabT##i;i++) #define uptil(f,t,i) for(int rabT##i=(t),i=(f);i< rabT##i;i++) #define downto(f,t,i) for(int rabT##i=(t),i=(f);i>=rabT##i;i--) #define downtil(f,t,i) for(int rabT##i=(t),i=(f);i> rabT##i;i--) #define iter(v) begin(v),end(v) #define citer(v) cbegin(v),cend(v) #define riter(v) rbegin(v),rend(v) #define criter(v) crbegin(v),crend(v) #define IF(a,b,c) ((a)?(b):(c)) #define BINOP_ASGN(t,u,op) t operator op(CS u&o)CS{RT t(*this)op##=o;} #if debug #define _GLIBCXX_DEBUG #define _LIBCPP_DEBUG 2 #define _LIBCPP_DEBUG2 2 #define ln <T power(T x,int n){T rt(1);for(;n;n/=2){if(n%2)rt*=x;x*=x;}RT rt;} int pow_mod(int x,int n,int m){int rt=1;for(;n;n/=2){if(n%2)rt=rt*x%m;x=x*x%m;}RT rt;} int128 pow_mod_64(int128 x,int n,int m){int128 rt=1;for(;n;n/=2){if(n%2)rt=rt*x%m;x=x*x%m;}RT rt;} /*? end "power.hpp" */ IL CX int modulo(int a,int m){RT(a%=m,a>=0?a:a+m);} TLclass MInt{ /*! https://ei1333.github.io/luzhiled/snippets/other/mod-int.html */ public: int val; CX MInt():val(0){} explicit CX MInt(int v):val(modulo(v,mod)){} MInt&operator+=(CS MInt&m){val+=m.val;if(val>=mod)val-=mod;RT*this;} MInt&operator-=(CS MInt&m){val-=m.val;if(val<0)val+=mod;RT*this;} MInt&operator*=(CS MInt&m){val=val*m.val%mod;RT*this;} MInt&operator/=(CS MInt&m){val=val*m.inv().val%mod;RT*this;} BINOP_ASGN(MInt,MInt,+) BINOP_ASGN(MInt,MInt,-) BINOP_ASGN(MInt,MInt,*) BINOP_ASGN(MInt,MInt,/) MInt operator-()CS{MInt m;m.val=val?mod-val:0;RT m;} bool operator==(CS MInt&m)CS{RT val==m.val;} bool operator!=(CS MInt&m)CS{RT val!=m.val;} //MInt pow(int n)CS{MInt x(*this),rt(1);while(n){if(n%2)rt*=x;x*=x;n/=2;}RT rt;} MInt pow(int n)CS{RT power(*this,n);} MInt inv()CS{int a=val,b=mod,x=1,y=0,t;while(b){t=a/b;swap(b,a-=t*b);swap(y,x-=t*y);}RT(MInt)x;} friend ostream&operator<<(ostream&o,CS MInt&m){RT o<>(istream&i,MInt&m){int v;i>>v;m=MInt(v);RT i;} }; using mint=MInt<>; constexpr mint operator"" _m(ULL n){RT mint(n);} constexpr MInt<998244353>operator"" _m998244353(ULL n){RT MInt<998244353>(n);} constexpr MInt<1000000007>operator"" _m1e9_7(ULL n){RT MInt<1000000007>(n);} constexpr MInt<1000000009>operator"" _m1e9_9(ULL n){RT MInt<1000000009>(n);} //#pragma rab:gsub \b(\d+)m\b mint(\1) /*? end "mod.hpp" */ /*? begin "typedefs.hpp" */ struct unit{}; using int128=__int128; using LD=long double; TLusing vec=vector; TLusing vvec=vec>; TLusing vvvec=vec>; TLusing vvvvec=vec>; //#pragma rab typedefs.dynamic using WI = vvec; using VI = vec; using VPII = vec>; /*? end "typedefs.hpp" */ /*? begin "alias.hpp" */ #define EB emplace_back #define PB push_back #define foldl accumulate #define scanl partial_sum /*? end "alias.hpp" */ /*? begin "util.hpp" */ TLIL bool amax(T&v,CS T&a){RT vIL bool amin(T&v,CS T&a){RT v>a&&(v=a,true);} #ifndef __cpp_lib_exchange_function #define exchange exchange_RAB TLIL T exchange(T& t, U&& u){T x=move(t);t=forward(u);RT x;} #endif #ifndef __cpp_lib_clamp #define clamp clamp_RAB TLIL CX CS T&clamp(CS T&a,CS T&mn,CS T&mx){RT amx?mx:a;} #endif TLIL int size_RAB(T t){RT t.size();} #define size size_RAB TLIL void uniq_after_sort(V&v){v.erase(unique(iter(v)),v.end());} TLIL void sort_and_uniq(V&v){sort(iter(v));v.erase(unique(iter(v)),v.end());} TLIL auto leftmost_ge(CS V&v,CS K&k){RT v.lower_bound(k);} TLIL auto leftmost_gt(CS V&v,CS K&k){RT v.upper_bound(k);} namespace rab{ TLIL void append(V&v,CS W&w){copy(PARABLE citer(w),back_inserter(v));} TLIL auto flatten(CS V&xss,int reserve_size=0)->TN V::value_type{ decltype(flatten(xss))ret; ret.reserve(reserve_size); for(CS auto&xs:xss)append(ret,xs); ret.shrink_to_fit(); RT move(ret); } TLIL bool is_in(I x,I l,I r){RT l<=x&&xIL T fetch(CS T&d,CS vec&v,int i){RT 0<=i&&iIL T fetch(CS T&d,CS vvec&v,int i,int j){ RT 0<=i&&iIL T fetch(CS T&d,CS vec>&v,int i,I...j){ // RT 0<=i&&istruct Compressed{int size;mapzip;vecunzip;}; TLIL Compressedcompressed(vecv){ sort_and_uniq(v);mapzip;times(size(v),i)zip[v[i]]=i;RT{size(v),zip,move(v)}; } TLstruct CompressedSrc{int size;mapzip;vecunzip;WI src;}; TLIL CompressedSrccompressed_src(CS vec&v){ auto c=compressed(v);VI src(c.size);times(size(v),i)src[c.zip[v[i]]].PB(i);RT{c.size,c.zip,c.unzip,src}; } struct identity{TLU operator()(U&&v)CS{RT v;}}; } /*? end "util.hpp" */ /*? begin "debug.hpp" */ TL IL istream&operator>>(istream&s,vec&v){for(auto&&p:v)s>>p;RT s;} TL IL ostream&operator<<(ostream&s,CS pair&p){RT s<<"("< IL ostream&operator<<(ostream&,CS vec&); TL IL ostream&operator<<(ostream&,CS map&); #define DEFINE_ITER_OUTPUT(s,x,sep){int i=0;for(CS auto&x##0_elem:x){if(i++)s< IL ostream&operator<<(ostream&s,CS vec&v)DEFINE_ITER_OUTPUT(s,v,' ') TL IL ostream&operator<<(ostream&s,CS map&m)DEFINE_ITER_OUTPUT(s,m,' ') TL IL ostream&operator<<(ostream&s,CS vec>&w)DEFINE_ITER_OUTPUT(s,w,'\n') TL IL ostream&operator<<(ostream&s,CS vec>&v)DEFINE_ITER_OUTPUT(s,v,'\n') /*? end "debug.hpp" */ signed main(){ {if(debug)cerr<<"MOD: "<<(MOD)ln;} if(!debug)cin.tie(0),cerr.tie(0),ios::sync_with_stdio(0); cout<>T; times(T,t){cout<<"Case #"<,class Inverser=negate> class UnionFind{ /*! http://noshi91.hatenablog.com/entry/2018/05/30/191943 https://en.wikipedia.org/wiki/Disjoint-set_data_structure https://qiita.com/drken/items/cce6fc5c579051e64fab */ int n,*parents,*sizes;T*pot_diffs;bool to_delete;Adder adder; Inverser inverser; public: explicit UnionFind(int n,bool to_delete=false): n(n),parents(new int[n]),sizes(new int[n]),pot_diffs(new T[n]),to_delete(to_delete) {clear(); } void clear(){ times(n,i)parents[i]=i;/*roots*/ fill(sizes,sizes+n,1);fill(pot_diffs,pot_diffs+n,0); } ~UnionFind(){if(to_delete){delete[]parents;delete[]sizes;delete[]pot_diffs;}} int size(){RT n;} int root(int i){int p=parents[i]; if(p==i)RT i;/*`i`is a root*/ int r=root(p);/*and pot_diffs[p]:=diff from root*/ pot_diffs[i]+=pot_diffs[p];parents[i]=r;RT r; } bool is_same(int i,int j){RT root(i)==root(j);} bool is_all_same(){int r=root(0);uptil(1,n,i)if(root(i)!=r)RT 0;RT 1;} bool merge(int i,int j,T pdiff=0){i=root(i);j=root(j); if(i==j)RT false;/*already merged*/ if(sizes[i]>sizes[j]){swap(i,j);pdiff=inverser(pdiff); } /*now sizes[i]<=sizes[j]*/ parents[i]=j;sizes[j]+=sizes[i];pot_diffs[i]=pdiff;RT true; } T diff(int i,int j){ root(i);/*pot_diffs[i]:=diff from root*/ root(j);/*pot_diffs[j]:=diff from root*/ RT adder(pot_diffs[i],inverser(pot_diffs[j]));}};using unionfind=UnionFind<>; /*? end "uf.hpp" */ TL struct Edge{int from;int to;EdgeVal weight; Edge(int from,int to,EdgeVal weight):from(from),to(to),weight(weight){} IL bool OP==(CS Edge&e)CS{RT weight==e.weight&&from==e.from&&to==e.to;} IL bool OP<(CS Edge&e)CS{RT weight(CS Edge&e)CS{RT e=(CS Edge&e)CS{RT e<=this;}}; TL class Graph{ using VoidWithoutEdgeVal=TN enable_if::value,void>::type; protected: int nv_,nde_;unionfind uf; public: vecvs;vvec>edges; Graph(int nv):nv_(nv),nde_(0),uf(nv),vs(nv),edges(nv){} IL int nv()CS{RT nv_;} IL int nde()CS{RT nde_;} IL int nue()CS{RT nde_/2;} IL void add_dedge(int i,int j,CS EdgeVal&val){ if(debug&&(!rab::is_in(i,0LL,nv_)||!rab::is_in(j,0LL,nv_))){ cerr<<"invalid index:("< class dinic{ public: struct FlowEdge{CS int from,to,nxt;CS F cap;F flow; FlowEdge(int from,int to,int nxt,F cap,F flow): from(from),to(to),nxt(nxt),cap(cap),flow(flow){}};int n,s,t;VI level,prog,que,heads;vecedges; dinic(CS Graph&g,CS CapFn&capfn=rab::identity()): n(g.nv()),s(-1),t(-1),level(n),prog(n),que(n),heads(n) {fill(iter(heads),-1);edges.reserve(g.nde()*2);int edges_i=0; times(n,i){for(auto&e:g.edges[i]){F c=capfn(e.weight); edges.EB(i,e.to,heads[i],c,(F)0);heads[i]=edges_i;++edges_i;edges.EB(e.to,i,heads[e.to],c,c); heads[e.to]=edges_i;++edges_i; }}} F exec(int s,int t){this->s=s;this->t=t;F mf=0,inf=numeric_limits::max()/8; while(update_level(),level[s]){copy(iter(heads),begin(prog)); mf+=find_paths(s,inf); } RT mf; } private: void update_level(){int ql=0,qr=0;fill(iter(level),0);level[t]=n; que[qr++]=t;while(ql!=qr){int v=que[ql++]; if(v==s)RT;for(int i=heads[v];~i;i=edges[i].nxt){CS auto&e=edges[i]; if(!level[e.to]&&e.flow!=0){level[e.to]=level[v]-1;que[qr++]=e.to; }}}} F find_paths(int v,int limit){if(v==t)RT limit;F diff=0; for(int i=prog[v];~i;i=prog[v]=edges[i].nxt){auto&e=edges[i]; if(level[v] */ int N;int M;int D;cin>>N;cin>>M;cin>>D;VI U(M);VI V(M);VI P(M);VI Q(M);VI W(M); times(M,Ri_0){cin>>U[Ri_0];--U[Ri_0];cin>>V[Ri_0];--V[Ri_0];cin>>P[Ri_0];cin>>Q[ Ri_0];cin>>W[Ri_0];} /* */ VPII vs; vs.reserve(M * 2); times(M, i) { vs.PB({U[i], P[i]}); vs.PB({V[i], Q[i] + D}); } auto cp = rab::compressed(vs); Graph g(cp.size); times(M, i) { g.add_dedge(cp.zip[{U[i], P[i]}], cp.zip[{V[i], Q[i] + D}], W[i]); } times(cp.size - 1, i) { if(cp.unzip[i].first == cp.unzip[i + 1].first) { g.add_dedge(i, i + 1, 1ll << 50); } } if(debug) { for(auto &es : g.edges) { for(auto &e : es) { cout << "(" << e.from sp << e.to sp << e.weight << ")" tb; } cout ln; } } dinic d(g); cout << d.exec(0, cp.size - 1) ln; }