結果

問題 No.1301 Strange Graph Shortest Path
ユーザー yuto1115yuto1115
提出日時 2020-11-27 21:56:41
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 194 ms / 3,000 ms
コード長 4,400 bytes
コンパイル時間 2,499 ms
コンパイル使用メモリ 213,332 KB
実行使用メモリ 34,472 KB
最終ジャッジ日時 2023-10-10 21:40:52
合計ジャッジ時間 9,629 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,352 KB
testcase_01 AC 1 ms
4,352 KB
testcase_02 AC 146 ms
32,872 KB
testcase_03 AC 116 ms
29,272 KB
testcase_04 AC 189 ms
31,236 KB
testcase_05 AC 119 ms
32,212 KB
testcase_06 AC 163 ms
29,260 KB
testcase_07 AC 152 ms
30,700 KB
testcase_08 AC 118 ms
29,680 KB
testcase_09 AC 163 ms
27,676 KB
testcase_10 AC 122 ms
29,316 KB
testcase_11 AC 166 ms
30,060 KB
testcase_12 AC 170 ms
30,064 KB
testcase_13 AC 157 ms
32,972 KB
testcase_14 AC 151 ms
27,908 KB
testcase_15 AC 149 ms
29,024 KB
testcase_16 AC 194 ms
31,360 KB
testcase_17 AC 158 ms
33,128 KB
testcase_18 AC 143 ms
30,172 KB
testcase_19 AC 176 ms
29,256 KB
testcase_20 AC 171 ms
28,440 KB
testcase_21 AC 158 ms
31,784 KB
testcase_22 AC 178 ms
29,432 KB
testcase_23 AC 152 ms
32,620 KB
testcase_24 AC 170 ms
29,040 KB
testcase_25 AC 183 ms
31,272 KB
testcase_26 AC 158 ms
30,132 KB
testcase_27 AC 167 ms
30,084 KB
testcase_28 AC 129 ms
32,132 KB
testcase_29 AC 191 ms
30,840 KB
testcase_30 AC 182 ms
31,304 KB
testcase_31 AC 194 ms
30,800 KB
testcase_32 AC 1 ms
4,352 KB
testcase_33 AC 80 ms
25,404 KB
testcase_34 AC 181 ms
34,472 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//@formatter:off
#include<bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < int(n); ++i)
#define rrep(i,n) for (int i = int(n)-1; i >= 0; i--)
#define rep2(i,s,n) for (int i = int(s); i < int(n); ++i)
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define pb push_back
#define eb emplace_back
#define vi vector<int>
#define vvi vector<vector<int>>
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vd vector<double>
#define vvd vector<vector<double>>
#define vs vector<string>
#define vc vector<char>
#define vvc vector<vector<char>>
#define vb vector<bool>
#define vvb vector<vector<bool>>
#define vp vector<P>
#define vvp vector<vector<P>>
using namespace std;
using ll = long long;
using P = pair<int,int>;
using LP = pair<ll,ll>;
template<class S,class T> istream& operator>>(istream &is,pair<S,T> &p) { return is >> p.first >> p.second; }
template<class S,class T> ostream& operator<<(ostream &os,const pair<S,T> &p) { return os<<'{'<<p.first<<","<<p.second<<'}'; }
template<class T> istream& operator>>(istream &is,vector<T> &v) { for(T &t:v){is>>t;} return is; }
template<class T> ostream& operator<<(ostream &os,const vector<T> &v) { os<<'[';rep(i,v.size())os<<v[i]<<(i==int(v.size()-1)?"":","); return os<<']'; }
void Yes(bool b) { cout << (b ? "Yes" : "No") << '\n'; }
void YES(bool b) { cout << (b ? "YES" : "NO") << '\n'; }
template<class T> void fin(T a) { cout << a << endl; exit(0); }
template<class T> bool chmin(T& a,T b) {if(a > b){a = b; return true;} return false;}
template<class T> bool chmax(T& a,T b) {if(a < b){a = b; return true;} return false;}
const int inf = 1001001001;
const ll linf = 1001001001001001001;
//@formatter:on

template<typename T>
class Mincostflow {
    using TP = pair<T, int>;
    
    struct edge {
        int to, cap, rev;
        T cost;
        
        edge(int to, int cap, T cost, int rev) : to(to), cap(cap), cost(cost), rev(rev) {}
    };
    
    T tinf;
    int n;
    vector<vector<edge>> G;
    vector<T> pot, dist;
    vi prevv, preve;
    
    // potential
    void tinf_init(int x) { tinf = inf; }
    
    void tinf_init(ll x) { tinf = linf; }

public:
    explicit Mincostflow(int n) : n(n), G(n), pot(n), dist(n), prevv(n), preve(n) {
        T x = 0;
        tinf_init(x);
    }
    
    void add_edge(int from, int to, T cap, int cost) {
        G[from].eb(to, cap, cost, G[to].size());
        G[to].eb(from, 0, -cost, G[from].size() - 1);
    }
    
    // return -1 if impossible
    // this must not be called more than once
    T min_cost_flow(int s, int t, int f) {
        T res = 0;
        pot.assign(n, 0);
        while (f > 0) {
            // use dijkstra to update pot
            priority_queue<TP, vector<TP>, greater<TP>> q;
            dist.assign(n, tinf);
            dist[s] = 0;
            q.emplace(0, s);
            while (!q.empty()) {
                T np = q.top().first;
                int v = q.top().second;
                q.pop();
                if (dist[v] < np) continue;
                rep(i, G[v].size()) {
                    edge &e = G[v][i];
                    if (e.cap > 0 && dist[e.to] > dist[v] + e.cost + pot[v] - pot[e.to]) {
                        dist[e.to] = dist[v] + e.cost + pot[v] - pot[e.to];
                        prevv[e.to] = v;
                        preve[e.to] = i;
                        q.emplace(dist[e.to], e.to);
                    }
                }
            }
            if (dist[t] == tinf) return -1;
            rep(i, n) pot[i] += dist[i];
            
            int d = f;
            for (int v = t; v != s; v = prevv[v]) {
                chmin(d, G[prevv[v]][preve[v]].cap);
            }
            f -= d;
            res += d * pot[t];
            for (int v = t; v != s; v = prevv[v]) {
                edge &e = G[prevv[v]][preve[v]];
                e.cap -= d;
                G[v][e.rev].cap += d;
            }
        }
        return res;
    };
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int n, m;
    cin >> n >> m;
    Mincostflow<ll> fl(n);
    rep(i, m) {
        int u, v, c, d;
        cin >> u >> v >> c >> d;
        u--;
        v--;
        fl.add_edge(u, v, 1, c);
        fl.add_edge(v, u, 1, c);
        fl.add_edge(u, v, 1, d);
        fl.add_edge(v, u, 1, d);
    }
    cout << fl.min_cost_flow(0, n - 1, 2) << endl;
}
0