結果
問題 | No.1301 Strange Graph Shortest Path |
ユーザー |
|
提出日時 | 2020-11-27 23:47:13 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 186 ms / 3,000 ms |
コード長 | 13,693 bytes |
コンパイル時間 | 2,713 ms |
コンパイル使用メモリ | 214,944 KB |
最終ジャッジ日時 | 2025-01-16 08:42:40 |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 33 |
ソースコード
#include <bits/stdc++.h>#define REP(i, n) for(int i = 0;i < n;i++)#define ll long longusing namespace std;typedef pair<int, int> P;typedef pair<ll,ll> LP;const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};const int INF = 1000000000;const ll LINF = 1000000000000000000;//1e18const double PI = acos(-1.0);const double EPS = 1e-10;template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }/* ----------------------- DEBUG FUNCTION ---------------------------- */#define DUMPOUT cerrvoid dump_function() { DUMPOUT << ' '; }void dump_function(bool a) { DUMPOUT << a; }void dump_function(int a) { DUMPOUT << a; }void dump_function(long long a) { DUMPOUT << a; }void dump_function(char a) { DUMPOUT << a; }void dump_function(string &a) { DUMPOUT << a; }void dump_function(double a) { DUMPOUT << a; }template <class T> void dump_function(const vector<T> &);template <class T, size_t size> void dump_function(const array<T, size> &);template <class T, class L> void dump_function(const pair<T, L> &p);template <class T, size_t size> void dump_function(const T (&)[size]);template <class T> void dump_function(const vector<T> &a) {if(a.empty()) return;dump_function(a[0]);for(auto i = a.begin(); ++i != a.end();) {DUMPOUT << " ";dump_function(*i);}DUMPOUT << endl;}template <class T> void dump_function(const deque<T> &a) {if(a.empty()) return;dump_function(a[0]);for(auto i = a.begin(); ++i != a.end();) {DUMPOUT << " ";dump_function(*i);}}template <class T, size_t size> void dump_function(const array<T, size> &a) {dump_function(a[0]);for(auto i = a.begin(); ++i != a.end();) {DUMPOUT << " ";dump_function(*i);}}template <class T, class L> void dump_function(const pair<T, L> &p) {DUMPOUT << '(';dump_function(p.first);DUMPOUT << ",";dump_function(p.second);DUMPOUT << ')';}template <class T> void dump_function(set<T> &x) {for(auto e : x) dump_function(e), DUMPOUT << " ";DUMPOUT << endl;}template <class T> void dump_function(multiset<T> &x) {for(auto e : x) dump_function(e), DUMPOUT << " ";DUMPOUT << endl;}template <class T, size_t size> void dump_function(const T (&a)[size]) {dump_function(a[0]);for(auto i = a; ++i != end(a);) {DUMPOUT << " ";dump_function(*i);}}template <class T> void dump_function(const T &a) { DUMPOUT << a; }int dump_out() {DUMPOUT << '\n';return 0;}template <class T> int dump_out(const T &t) {dump_function(t);DUMPOUT << '\n';return 0;}template <class Head, class... Tail> int dump_out(const Head &head, const Tail &... tail) {dump_function(head);DUMPOUT << ' ';dump_out(tail...);return 0;}#ifdef DEBUG_#define dump(x)\DUMPOUT << #x << ": ";\dump_function(x);\DUMPOUT << endl;void dumps() {}template <class T> void dumps(const T &t) {dump_function(t);DUMPOUT << " ";}template <class Head, class... Tail> void dumps(const Head &head, const Tail &... tail) {dump_function(head);DUMPOUT << ' ';dump_out(tail...);}#else#define dump(x)template <class... T> void dumps(const T &...) {}#endif/* ----------------------- DEBUG FUNCTION ---------------------------- */template<int MOD> struct Fp {long long val;constexpr Fp(long long v = 0) noexcept : val(v % MOD) {if (val < 0) val += MOD;}constexpr int getmod() const { return MOD; }constexpr Fp operator - () const noexcept {return val ? MOD - val : 0;}constexpr Fp operator + (const Fp& r) const noexcept { return Fp(*this) += r; }constexpr Fp operator - (const Fp& r) const noexcept { return Fp(*this) -= r; }constexpr Fp operator * (const Fp& r) const noexcept { return Fp(*this) *= r; }constexpr Fp operator / (const Fp& r) const noexcept { return Fp(*this) /= r; }constexpr Fp& operator += (const Fp& r) noexcept {val += r.val;if (val >= MOD) val -= MOD;return *this;}constexpr Fp& operator -= (const Fp& r) noexcept {val -= r.val;if (val < 0) val += MOD;return *this;}constexpr Fp& operator *= (const Fp& r) noexcept {val = val * r.val % MOD;return *this;}constexpr Fp& operator /= (const Fp& r) noexcept {long long a = r.val, b = MOD, u = 1, v = 0;while (b) {long long t = a / b;a -= t * b, swap(a, b);u -= t * v, swap(u, v);}val = val * u % MOD;if (val < 0) val += MOD;return *this;}constexpr bool operator == (const Fp& r) const noexcept {return this->val == r.val;}constexpr bool operator != (const Fp& r) const noexcept {return this->val != r.val;}friend constexpr istream& operator >> (istream& is, Fp<MOD>& x) noexcept {is >> x.val;x.val %= MOD;if (x.val < 0) x.val += MOD;return is;}friend constexpr ostream& operator << (ostream& os, const Fp<MOD>& x) noexcept {return os << x.val;}friend constexpr Fp<MOD> modpow(const Fp<MOD>& r, long long n) noexcept {if (n == 0) return 1;auto t = modpow(r, n / 2);t = t * t;if (n & 1) t = t * r;return t;}friend constexpr Fp<MOD> modinv(const Fp<MOD>& r) noexcept {long long a = r.val, b = MOD, u = 1, v = 0;while (b) {long long t = a / b;a -= t * b, swap(a, b);u -= t * v, swap(u, v);}return Fp<MOD>(u);}};template<class T> struct BiCoef {vector<T> fact_, inv_, finv_;constexpr BiCoef() {}constexpr BiCoef(int n) noexcept : fact_(n, 1), inv_(n, 1), finv_(n, 1) {init(n);}constexpr void init(int n) noexcept {fact_.assign(n, 1), inv_.assign(n, 1), finv_.assign(n, 1);int MOD = fact_[0].getmod();for(int i = 2; i < n; i++){fact_[i] = fact_[i-1] * i;inv_[i] = -inv_[MOD%i] * (MOD/i);finv_[i] = finv_[i-1] * inv_[i];}}constexpr T com(int n, int k) const noexcept {if (n < k || n < 0 || k < 0) return 0;return fact_[n] * finv_[k] * finv_[n-k];}constexpr T fact(int n) const noexcept {if (n < 0) return 0;return fact_[n];}constexpr T inv(int n) const noexcept {if (n < 0) return 0;return inv_[n];}constexpr T finv(int n) const noexcept {if (n < 0) return 0;return finv_[n];}constexpr T perm(int n, int k) const noexcept {if (n < k || n < 0 || k < 0) return 0;return fact_[n] * finv_[n-k];}};/* ----------------------------- MOD ----------------------------------- */const int MOD = 1000000007;const int MOD2 = 998244353;using mint = Fp<MOD2>;BiCoef<mint> bc;// using vec = vector<mint>;// using mat = vector<vec>;/* ----------------------------- MOD ----------------------------------- *//* ----------------------- AtCoder Library ---------------------------- */// #include <atcoder/all>// using namespace atcoder;/* ----------------------- AtCoder Library ---------------------------- */template <class Cap, class Cost> struct mcf_graph {public:mcf_graph() {}mcf_graph(int n) : _n(n), g(n) {}int add_edge(int from, int to, Cap cap, Cost cost) {assert(0 <= from && from < _n);assert(0 <= to && to < _n);assert(0 <= cap);assert(0 <= cost);int m = int(pos.size());pos.push_back({from, int(g[from].size())});int from_id = int(g[from].size());int to_id = int(g[to].size());if (from == to) to_id++;g[from].push_back(_edge{to, to_id, cap, cost});g[to].push_back(_edge{from, from_id, 0, -cost});return m;}struct edge {int from, to;Cap cap, flow;Cost cost;};edge get_edge(int i) {int m = int(pos.size());assert(0 <= i && i < m);auto _e = g[pos[i].first][pos[i].second];auto _re = g[_e.to][_e.rev];return edge{pos[i].first, _e.to, _e.cap + _re.cap, _re.cap, _e.cost,};}std::vector<edge> edges() {int m = int(pos.size());std::vector<edge> result(m);for (int i = 0; i < m; i++) {result[i] = get_edge(i);}return result;}std::pair<Cap, Cost> flow(int s, int t) {return flow(s, t, std::numeric_limits<Cap>::max());}std::pair<Cap, Cost> flow(int s, int t, Cap flow_limit) {return slope(s, t, flow_limit).back();}std::vector<std::pair<Cap, Cost>> slope(int s, int t) {return slope(s, t, std::numeric_limits<Cap>::max());}std::vector<std::pair<Cap, Cost>> slope(int s, int t, Cap flow_limit) {assert(0 <= s && s < _n);assert(0 <= t && t < _n);assert(s != t);// variants (C = maxcost):// -(n-1)C <= dual[s] <= dual[i] <= dual[t] = 0// reduced cost (= e.cost + dual[e.from] - dual[e.to]) >= 0 for all edgestd::vector<Cost> dual(_n, 0), dist(_n);std::vector<int> pv(_n), pe(_n);std::vector<bool> vis(_n);struct Q {Cost key;int to;bool operator<(Q r) const { return key > r.key; }};std::vector<Q> que;auto dual_ref = [&]() {std::fill(dist.begin(), dist.end(),std::numeric_limits<Cost>::max());std::fill(vis.begin(), vis.end(), false);que.clear();dist[s] = 0;que.push_back(Q{0, s});std::push_heap(que.begin(), que.end());while (!que.empty()) {int v = que.front().to;std::pop_heap(que.begin(), que.end());que.pop_back();if (vis[v]) continue;vis[v] = true;if (v == t) break;// dist[v] = shortest(s, v) + dual[s] - dual[v]// dist[v] >= 0 (all reduced cost are positive)// dist[v] <= (n-1)Cfor (int i = 0; i < int(g[v].size()); i++) {auto e = g[v][i];if (vis[e.to] || !e.cap) continue;// |-dual[e.to] + dual[v]| <= (n-1)C// cost <= C - -(n-1)C + 0 = nCCost cost = e.cost - dual[e.to] + dual[v];if (dist[e.to] - dist[v] > cost) {dist[e.to] = dist[v] + cost;pv[e.to] = v;pe[e.to] = i;que.push_back(Q{dist[e.to], e.to});std::push_heap(que.begin(), que.end());}}}if (!vis[t]) {return false;}for (int v = 0; v < _n; v++) {if (!vis[v]) continue;// dual[v] = dual[v] - dist[t] + dist[v]// = dual[v] - (shortest(s, t) + dual[s] - dual[t]) + (shortest(s, v) + dual[s] - dual[v])// = - shortest(s, t) + dual[t] + shortest(s, v)// = shortest(s, v) - shortest(s, t) >= 0 - (n-1)Cdual[v] -= dist[t] - dist[v];}return true;};Cap flow = 0;Cost cost = 0, prev_cost_per_flow = -1;std::vector<std::pair<Cap, Cost>> result;result.push_back({flow, cost});while (flow < flow_limit) {if (!dual_ref()) break;Cap c = flow_limit - flow;for (int v = t; v != s; v = pv[v]) {c = std::min(c, g[pv[v]][pe[v]].cap);}for (int v = t; v != s; v = pv[v]) {auto& e = g[pv[v]][pe[v]];e.cap -= c;g[v][e.rev].cap += c;}Cost d = -dual[s];flow += c;cost += c * d;if (prev_cost_per_flow == d) {result.pop_back();}result.push_back({flow, cost});prev_cost_per_flow = d;}return result;}private:int _n;struct _edge {int to, rev;Cap cap;Cost cost;};std::vector<std::pair<int, int>> pos;std::vector<std::vector<_edge>> g;};void solve(){int N, M;cin >> N >> M;mcf_graph<ll, ll> g(N);REP(i,M){int u, v, c, d;cin >> u >> v >> c >> d;--u, --v;g.add_edge(u, v, 1, c);g.add_edge(u, v, 1, d);g.add_edge(v, u, 1, c);g.add_edge(v, u, 1, d);}cout << g.flow(0, N-1, 2).second << endl;}int main(){cin.tie(0);ios::sync_with_stdio(false);solve();// int T; cin >> T; REP(t,T) solve();}