#include #define FASTIO using namespace std; using ll = long long; using Vi = std::vector; using Vl = std::vector; using Pii = std::pair; using Pll = std::pair; constexpr int I_INF = std::numeric_limits::max(); constexpr ll L_INF = std::numeric_limits::max(); template inline bool chmin(T1& a, const T2& b) { if (a > b) { a = b; return true; } return false; } template inline bool chmax(T1& a, const T2& b) { if (a < b) { a = b; return true; } return false; } template class Prints { private: class __Prints { public: __Prints(const char* sep, const char* term) : sep(sep), term(term) {} template auto operator()(const Args&... args) const -> decltype((os << ... << std::declval()), void()) { print(args...); } template auto pvec(const T& vec, size_t sz) const -> decltype(os << std::declval()[0])>(), void()) { for (size_t i = 0; i < sz; i++) os << vec[i] << (i == sz - 1 ? term : sep); } template auto pmat(const T& mat, size_t h, size_t w) -> decltype(os << std::declval()[0][0])>(), void()) { for (size_t i = 0; i < h; i++) for (size_t j = 0; j < w; j++) os << mat[i][j] << (j == w - 1 ? term : sep); } private: const char *sep, *term; void print() const { os << term; } void print_rest() const { os << term; } template void print(const T& head, const Tail&... tail) const { os << head, print_rest(tail...); } template void print_rest(const T& head, const Tail&... tail) const { os << sep << head, print_rest(tail...); } }; public: Prints() {} __Prints operator()(const char* sep = " ", const char* term = "\n") const { return __Prints(sep, term); } }; Prints<> prints; Prints prints_err; //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% template class MinCostFlowGraph { private: struct _Edge { int to, rev; Cap cap; Cost cost; _Edge() noexcept {} _Edge(int to, Cap cap, Cost cost, int rev) noexcept : to(to), rev(rev), cap(cap), cost(cost) {} }; std::vector> g; std::vector> pos; public: MinCostFlowGraph(int n) noexcept : g(n) {} int add_edge(int from, int to, Cap cap, Cost cost) noexcept { const int m = static_cast(pos.size()); const int from_id = static_cast(g[from].size()); int to_id = static_cast(g[to].size()); pos.emplace_back(from, static_cast(g[from].size())); if (from == to) ++to_id; g[from].emplace_back(to, cap, cost, to_id); g[to].emplace_back(from, 0, -cost, from_id); return m; } struct Edge { int from, to; Cap cap, flow; Cost cost; Edge() noexcept {} Edge(int from, int to, Cap cap, Cap flow, Cost cost) noexcept : from(from), to(to), cap(cap), flow(flow), cost(cost) {} }; Edge get_edge(int i) const noexcept { const auto e = g[pos[i].first][pos[i].second]; const auto re = g[e.to][e.rev]; return Edge(pos[i].first, e.to, e.cap + re.cap, re.cap, e.cost); } std::vector edges() const noexcept { const int m = static_cast(pos.size()); std::vector res; res.reserve(m); for (int i = 0; i < m; i++) res.emplace_back(get_edge(i)); return res; } std::pair flow(int s, int t) noexcept { return flow(s, t, std::numeric_limits::max()); } std::pair flow(int s, int t, Cap flow_limit) noexcept { return slope(s, t, flow_limit).back(); } std::vector> slope(int s, int t) noexcept { return slope(s, t, std::numeric_limits::max()); } std::vector> slope(int s, int t, Cap flow_limit) noexcept { constexpr Cost C_INF = std::numeric_limits::max(); const int n = static_cast(g.size()); std::vector h(n), dist(n); std::vector prevv(n), preve(n); std::vector> res; struct Q { Cost key; int to; Q(Cost key, int to) noexcept : key(key), to(to) {} bool operator>(const Q& r) const noexcept { return key > r.key; } }; Cap flow = 0; Cost cost = 0, prev_cost = -1; res.emplace_back(flow, cost); while (flow < flow_limit) { std::priority_queue, std::greater> que; dist.assign(n, C_INF); dist[s] = 0; que.emplace(0, s); while (!que.empty()) { auto [d, v] = que.top(); que.pop(); if (dist[v] < d) continue; const int m = static_cast(g[v].size()); for (int i = 0; i < m; i++) { const _Edge& e = g[v][i]; Cost cn = dist[v] + e.cost + h[v] - h[e.to]; if (e.cap > 0 && dist[e.to] > cn) { dist[e.to] = cn; prevv[e.to] = v; preve[e.to] = i; que.emplace(cn, e.to); } } } if (dist[t] == C_INF) break; for (int i = 0; i < n; i++) h[i] += dist[i]; Cap f = flow_limit - flow; for (int i = t; i != s; i = prevv[i]) { f = std::min(f, g[prevv[i]][preve[i]].cap); } for (int i = t; i != s; i = prevv[i]) { _Edge& e = g[prevv[i]][preve[i]]; e.cap -= f; g[i][e.rev].cap += f; } Cost d = h[t]; flow += f; cost += f * d; if (prev_cost == d) res.pop_back(); res.emplace_back(flow, cost); prev_cost = d; } return res; } }; void solve() { int N, M; cin >> N >> M; MinCostFlowGraph mcf(N); for (ll i = 0; i < M; i++) { int a, b, c, d; cin >> a >> b >> c >> d; --a, --b; mcf.add_edge(a, b, 1, c); mcf.add_edge(a, b, 1, d); mcf.add_edge(b, a, 1, c); mcf.add_edge(b, a, 1, d); } auto flw = mcf.flow(0, N - 1, 2); prints()(flw.second); } //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% int main() { #ifdef FASTIO std::cin.tie(nullptr), std::cout.tie(nullptr); std::ios::sync_with_stdio(false); #endif #ifdef FILEINPUT std::ifstream ifs("./in_out/input.txt"); std::cin.rdbuf(ifs.rdbuf()); #endif #ifdef FILEOUTPUT std::ofstream ofs("./in_out/output.txt"); std::cout.rdbuf(ofs.rdbuf()); #endif std::cout << std::setprecision(18) << std::fixed; solve(); std::cout << std::flush; return 0; }