#include // #include // #include using namespace std; // using namespace atcoder; // using bint = boost::multiprecision::cpp_int; using ll = long long; using ull = unsigned long long; using P = pair; using vi = vector; using vvi = vector; using vvvi = vector; #define rep(i,n) for(ll i = 0;i < (ll)n;i++) #define ALL(x) (x).begin(),(x).end() #define sz(c) ((ll)(c).size()) #define LB(A,x) (int)(lower_bound(A.begin(),A.end(),x)-A.begin()) // #define MOD 1000000007 #define MOD 998244353 templateusing min_priority_queue=priority_queue,greater>; templateostream&operator<<(ostream&os,vector&v){for(int i = 0;i < v.size();i++)os<istream&operator>>(istream&is,vector&v){for(T&in:v)is>>in;return is;} templateostream&operator<<(ostream&os,pair&p){os<istream&operator>>(istream&is,pair&p){is>>p.first>>p.second;return is;} // https://ei1333.github.io/luzhiled/snippets/graph/dinic.html template struct Dinic{ /* 復元をするときはfromの頂点から出る辺からのcapを出力. is_e == trueの時その辺は元のグラフにあった"本当の"辺. "本当の"辺eについてfr -> toにv[e.to][e.ref].capだけフローが流れている. 二部マッチングの復元の場合,仮想の始点(終点)から(へ)の辺に注意. */ struct edge{ int to; flow_t cap; int ref;//逆辺の番号 bool is_e;//本物の辺かどうか int idx;//入力で与えられる辺としてのindex }; vector> v; vector mn_cost,iter; const flow_t inf; Dinic(int n):inf(numeric_limits::max()),v(n){} void add_edge(int fr,int to,flow_t cap,int idx = -1){ v[fr].emplace_back((edge){to,cap,(int)v[to].size(),true,idx}); v[to].emplace_back((edge){fr,0,(int)v[fr].size()-1,false,idx}); } bool bfs(int s,int t){ mn_cost.assign(v.size(),-1); queue que; mn_cost[s] = 0; que.push(s); while(!que.empty() && mn_cost[t] == -1){ int ov = que.front();que.pop(); for(auto &e : v[ov]){ if(e.cap > 0 && mn_cost[e.to] == -1){ mn_cost[e.to] = mn_cost[ov] + 1; que.push(e.to); } } } return mn_cost[t] != -1; } flow_t dfs(int ov,const int t,flow_t flow){ if(ov == t)return flow; for(int &i = iter[ov];i < v[ov].size();i++){ edge &e = v[ov][i]; if(e.cap > 0 && mn_cost[ov] < mn_cost[e.to]){ flow_t d = dfs(e.to,t,min(flow,e.cap)); if(d > 0){ e.cap -= d; v[e.to][e.ref].cap += d; return d; } } } return 0; } // O(m n^2)(n:頂点数 m:辺数) flow_t max_flow(int s,int t){ flow_t flow = 0; while(bfs(s,t)){ iter.assign(v.size(),0); flow_t f = 0; while((f = dfs(s,t,inf)) > 0)flow += f; } return flow; } void output(){ for(int i = 0;i < v.size();i++){ for(auto &e : v[i]) { if(!e.is_e) continue; auto &rev_e = v[e.to][e.ref]; cout << i << "->" << e.to << " (flow: " << rev_e.cap << "/" << e.cap + rev_e.cap << ")" << "\n"; } } } }; int main(){ ios_base::sync_with_stdio(0), cin.tie(0); ll n,m,D; cin >> n >> m >> D; const ll inf = 1ll << 50; vvi v(m,vi(5)); rep(i,m){ cin >> v[i]; rep(j,2)v[i][j]--; } vvi air(n); air[0].emplace_back(0); air[n-1].emplace_back(1ll << 32); rep(i,m){ air[v[i][0]].emplace_back(v[i][2]); air[v[i][1]].emplace_back(v[i][3]+D); } vi rui(n+1); rep(i,n){ sort(ALL(air[i])); air[i].erase(unique(ALL(air[i])),air[i].end()); rui[i+1] = rui[i] + sz(air[i]); } Dinic dn(rui.back()); rep(i,m){ dn.add_edge(rui[v[i][0]]+LB(air[v[i][0]],v[i][2]),rui[v[i][1]]+LB(air[v[i][1]],v[i][3]+D),v[i][4]); } rep(i,n){ rep(j,sz(air[i])-1){ dn.add_edge(rui[i]+j,rui[i]+j+1,inf); } } cout << dn.max_flow(0,rui.back()-1) << "\n"; return 0; }