結果
問題 | No.1301 Strange Graph Shortest Path |
ユーザー |
![]() |
提出日時 | 2022-01-01 13:59:20 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 215 ms / 3,000 ms |
コード長 | 6,287 bytes |
コンパイル時間 | 2,090 ms |
コンパイル使用メモリ | 182,188 KB |
実行使用メモリ | 42,784 KB |
最終ジャッジ日時 | 2024-10-10 03:13:32 |
合計ジャッジ時間 | 9,940 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 33 |
ソースコード
#include <bits/stdc++.h>using namespace std;#define ll long long#define rep(i,n) for(int i=0;i<n;i++)#define rrep(i,n) for(int i=n-1;i>=0;i--)#define rrep2(i,n,k) for(int i=n-1;i>=n-k;i--)#define vll(n,i) vector<long long>(n,i)#define v2ll(n,m,i) vector<vector<long long>>(n,vll(m,i))#define v3ll(n,m,k,i) vector<vector<vector<long long>>>(n,v2ll(m,k,i))#define v4ll(n,m,k,l,i) vector<vector<vector<vector<long long>>>>(n,v3ll(m,k,l,i))#define all(v) v.begin(),v.end()#define chmin(k,m) k = min(k,m)#define chmax(k,m) k = max(k,m)#define Pr pair<ll,ll>#define Tp tuple<ll,ll,ll>#define riano_ std::ios::sync_with_stdio(false);std::cin.tie(nullptr)using Graph = vector<vector<Tp>>;const ll mod = 998244353;template<uint64_t mod>struct modint{uint64_t val;constexpr modint(const int64_t val_=0) noexcept:val((val_%int64_t(mod)+int64_t(mod))%int64_t(mod)){}constexpr modint operator-() const noexcept{return modint(*this)=mod-val;}constexpr modint operator+(const modint rhs) const noexcept{return modint(*this)+=rhs;}constexpr modint operator-(const modint rhs) const noexcept{return modint(*this)-=rhs;}constexpr modint operator*(const modint rhs) const noexcept{return modint(*this)*=rhs;}constexpr modint operator/(const modint rhs) const noexcept{return modint(*this)/=rhs;}constexpr modint &operator+=(const modint rhs) noexcept{val+=rhs.val;val-=((val>=mod)?mod:0);return (*this);}constexpr modint &operator-=(const modint rhs) noexcept{val+=((val<rhs.val)?mod:0);val-=rhs.val;return (*this);}constexpr modint &operator*=(const modint rhs) noexcept{val=val*rhs.val%mod;return (*this);}constexpr modint &operator/=(modint rhs) noexcept{uint64_t ex=mod-2;modint now=1;while(ex){now*=((ex&1)?rhs:1);rhs*=rhs,ex>>=1;}return (*this)*=now;}modint & operator++(){val++;if (val == mod) val = 0;return *this;}modint operator++(int){modint<mod> res = *this;++*this;return res;}constexpr bool operator==(const modint rhs) noexcept{return val==rhs.val;}constexpr bool operator!=(const modint rhs) noexcept{return val!=rhs.val;}friend constexpr ostream &operator<<(ostream& os,const modint x) noexcept{return os<<(x.val);}friend constexpr istream &operator>>(istream& is,modint& x) noexcept{uint64_t t;is>>t,x=t;return is;}};typedef modint<mod> mint;mint pw(long long a,long long b,long long m = mod){if(a%m==0) return mint(0);if(b==0) return mint(1);else if(b%2==0){long long x = pw(a,b/2,m).val;return mint(x*x);}else{long long x = pw(a,b-1,m).val;return mint(a*x);}}mint modinv(long long a, long long m = mod) {long long b = m, u = 1, v = 0;while (b) {long long t = a / b;a -= t * b; swap(a, b);u -= t * v; swap(u, v);}u %= m;return mint(u);}#define vm(n,i) vector<mint>(n,i)#define v2m(n,m,i) vector<vector<mint>>(n,vm(m,i))#define v3m(n,m,k,i) vector<vector<vector<mint>>>(n,v2m(m,k,i))#define v4m(n,m,k,l,i) vector<vector<vector<vector<mint>>>>(n,v3m(m,k,l,i))//MinCostFlowtemplate< typename flow_t, typename cost_t >struct PrimalDual {const cost_t INF;struct edge {int to;flow_t cap;cost_t cost;int rev;bool isrev;};vector< vector< edge > > graph;vector< cost_t > potential, min_cost;vector< int > prevv, preve;PrimalDual(int V) : graph(V), INF(numeric_limits< cost_t >::max()) {}void add_edge(int from, int to, flow_t cap, cost_t cost) {graph[from].emplace_back((edge) {to, cap, cost, (int) graph[to].size(), false});graph[to].emplace_back((edge) {from, 0, -cost, (int) graph[from].size() - 1, true});}cost_t min_cost_flow(int s, int t, flow_t f) {int V = (int) graph.size();cost_t ret = 0;using Pi = pair< cost_t, int >;priority_queue< Pi, vector< Pi >, greater< Pi > > que;potential.assign(V, 0);preve.assign(V, -1);prevv.assign(V, -1);while(f > 0) {min_cost.assign(V, INF);que.emplace(0, s);min_cost[s] = 0;while(!que.empty()) {Pi p = que.top();que.pop();if(min_cost[p.second] < p.first) continue;for(int i = 0; i < graph[p.second].size(); i++) {edge &e = graph[p.second][i];cost_t nextCost = min_cost[p.second] + e.cost + potential[p.second] - potential[e.to];if(e.cap > 0 && min_cost[e.to] > nextCost) {min_cost[e.to] = nextCost;prevv[e.to] = p.second, preve[e.to] = i;que.emplace(min_cost[e.to], e.to);}}}if(min_cost[t] == INF) return -1;for(int v = 0; v < V; v++) potential[v] += min_cost[v];flow_t addflow = f;for(int v = t; v != s; v = prevv[v]) {addflow = min(addflow, graph[prevv[v]][preve[v]].cap);}f -= addflow;ret += addflow * potential[t];for(int v = t; v != s; v = prevv[v]) {edge &e = graph[prevv[v]][preve[v]];e.cap -= addflow;graph[v][e.rev].cap += addflow;}}return ret;}void output() {for(int i = 0; i < graph.size(); i++) {for(auto &e : graph[i]) {if(e.isrev) continue;auto &rev_e = graph[e.to][e.rev];cout << i << "->" << e.to << " (flow: " << rev_e.cap << "/" << rev_e.cap + e.cap << ")" << endl;}}}};int main() {riano_; ll ans = 0;ll N,M; cin >> N >> M;//main関数内PrimalDual<long long,long long> pd(N+2);// pd.add_edge(from,to,cap,cost);// pd.min_cost_flow(from,to,flow);rep(i,M){ll a,b,c,d; cin >> a >> b >> c >> d;pd.add_edge(a,b,1,c);pd.add_edge(a,b,1,d);pd.add_edge(b,a,1,c);pd.add_edge(b,a,1,d);}//rep(i,2) cout << pd.min_cost_flow(1,N,i+1) << endl;ans = pd.min_cost_flow(1,N,2);cout << ans << endl;}