////////////////////////////// // Check before you submit. // #define _ATCODER_LIBRARY const long long MOD = 1e9+7; // const long long MOD = 998244353; ///////////////////////////// #include using namespace std; #include using namespace boost::multiprecision; #ifdef _ATCODER_LIBRARY #include using namespace atcoder; #endif // _ATCODER_LIBRARY const long long INF = 1LL << 60; const double PI = acos(-1); using ll = long long; using P = array; #define FOR(i,a,b) for (ll i=(a);i<(ll)(b);++i) #define REP(i,n) FOR(i,0,n) #define ALL(v) (v).begin(),(v).end() #define SUM(v) accumulate(ALL(v),0ll) templateistream& operator>>(istream&i,vector&v){REP(j,v.size())i>>v[j];return i;} templatestring join(vector&v){stringstream s;REP(i,v.size())s<<' '<ostream& operator<<(ostream&o,vector&v){if(v.size())o<string join(vector>&vv){string s="\n";REP(i,vv.size())s+=join(vv[i])+"\n";return s;} templateostream& operator<<(ostream&o,vector>&vv){if(vv.size())o<istream& operator>>(istream&i,pair&v){return i>>v.first>>v.second;} templateostream& operator<<(ostream&o,pair&v){return o<<"("<istream& operator>>(istream&i,array&v){i>>v[0]>>v[1]; return i;} templateostream& operator<<(ostream&o,array&v){return o<<"("<T up(T a, T b){assert(b);return (a+b-1)/b;} templatebool eq(A const&... a){auto t={a...};assert(t.size());auto tar=*t.begin();for(const auto&e:t)if(tar!=e)return false;return true;} templatebool chmin(T &a, T b){if(a>b){a=b;return true;}return false;} templatebool chmax(T &a, T b){if(abool chmax(T &a, initializer_listl){return chmax(a,max(l));} templatebool chmin(T &a, initializer_listl){return chmin(a,min(l));} ////////////////////////////////////////////////////////////////// // My Library ////////////////////////////////////////////////////////////////// template class Dinic { private: int V; vector level,iter; void bfs(int s) { fill(level.begin(),level.end(),-1); queue que; level[s] = 0; que.push(s); while(!que.empty()){ int v = que.front(); que.pop(); for(auto& e : G[v]){ if(e.cap > 0 && level[e.to] < 0){ level[e.to] = level[v] + 1; que.push(e.to); } } } } T dfs(int v,int t,T f) { if(v==t){ return f; } for(int& i = iter[v]; i < (int)G[v].size(); i++){ edge& e = G[v][i]; if(e.cap > 0 && level[v] < level[e.to]){ T d = dfs(e.to,t,min(f,e.cap)); if(d > 0){ e.cap -= d; G[e.to][e.rev].cap += d; return d; } } } return 0; } public: struct edge{ int to; T cap; int rev; }; vector > G; Dinic(int node_size) : V(node_size), level(V), iter(V), G(V){} // Adding edge on a node. void add_edge(int from,int to,T cap) { G[from].push_back((edge){to,cap,(int)G[to].size()}); G[to].push_back((edge){from,(T)0,(int)G[from].size()-1}); } // Calculate the maximum flow. T solve(int s,int t) { T flow = 0; for(;;){ bfs(s); if(level[t] < 0) return flow; fill(iter.begin(),iter.end(),0); T f; while((f=dfs(s,t,numeric_limits::max())) > 0){ flow += f; } } } }; template< typename T > struct Compress { vector< T > xs; Compress() = default; Compress(const vector< T > &vs) { add(vs); } Compress(const initializer_list< vector< T > > &vs) { for(auto &p : vs) add(p); } void add(const vector< T > &vs) { copy(begin(vs), end(vs), back_inserter(xs)); } void add(const T &x) { xs.emplace_back(x); } void build() { sort(begin(xs), end(xs)); xs.erase(unique(begin(xs), end(xs)), end(xs)); } int size() { return (int)xs.size(); } vector< int > get(const vector< T > &vs) const { vector< int > ret; transform(begin(vs), end(vs), back_inserter(ret), [&](const T &x) { return lower_bound(begin(xs), end(xs), x) - begin(xs); }); return ret; } int get(const T &x) const { return lower_bound(begin(xs), end(xs), x) - begin(xs); } const T &operator[](int k) const { return xs[k]; } }; ////////////////////////////////////////////////////////////////// // Contest Code ////////////////////////////////////////////////////////////////// int main(int argc, char **argv) { init_init_init(); ll N, M, d; cin >> N >> M >> d; using T = tuple; vector> uni_pre(N); vector info; REP(i, M) { ll u, v, p, q, w; cin >> u>>v>>p>>q>>w; --u, --v; if (v != N-1) q += d; info.push_back({u, v, p, q, w}); uni_pre[u].push_back(p); uni_pre[v].push_back(q); } vector> comp(N); REP(i, N) { comp[i] = Compress(uni_pre[i]); comp[i].build(); } Dinic max_flow(N*M*2); vector add_constant(N); set se; ll sz{0}; // infの変をはる REP(i, N) { add_constant[i] = sz; for (ll j = sz; j < sz+comp[i].size()-1; ++j) { DEBUG(j); max_flow.add_edge(j, j+1, INF); } DEBUG(comp[i].size()); DEBUG("----"); sz += comp[i].size(); } REP(i, M) { auto [u, v, p, q, w] = info[i]; ll id_p = comp[u].get(p) + add_constant[u]; ll id_q = comp[v].get(q) + add_constant[v]; max_flow.add_edge(id_p, id_q, w); } std::cout << max_flow.solve(0, sz-1) << std::endl; }