結果

問題 No.1301 Strange Graph Shortest Path
ユーザー umimelumimel
提出日時 2024-12-28 19:24:48
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 404 ms / 3,000 ms
コード長 5,516 bytes
コンパイル時間 2,536 ms
コンパイル使用メモリ 183,972 KB
実行使用メモリ 94,928 KB
最終ジャッジ日時 2024-12-28 19:25:07
合計ジャッジ時間 16,357 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,248 KB
testcase_02 AC 311 ms
92,020 KB
testcase_03 AC 235 ms
81,980 KB
testcase_04 AC 382 ms
91,844 KB
testcase_05 AC 223 ms
92,624 KB
testcase_06 AC 375 ms
85,360 KB
testcase_07 AC 306 ms
87,624 KB
testcase_08 AC 232 ms
83,948 KB
testcase_09 AC 326 ms
79,708 KB
testcase_10 AC 254 ms
83,656 KB
testcase_11 AC 331 ms
87,944 KB
testcase_12 AC 351 ms
87,888 KB
testcase_13 AC 284 ms
92,396 KB
testcase_14 AC 330 ms
81,232 KB
testcase_15 AC 306 ms
82,768 KB
testcase_16 AC 404 ms
91,836 KB
testcase_17 AC 341 ms
93,112 KB
testcase_18 AC 276 ms
84,056 KB
testcase_19 AC 334 ms
84,284 KB
testcase_20 AC 346 ms
82,656 KB
testcase_21 AC 344 ms
90,836 KB
testcase_22 AC 386 ms
85,580 KB
testcase_23 AC 302 ms
92,640 KB
testcase_24 AC 393 ms
83,612 KB
testcase_25 AC 365 ms
90,660 KB
testcase_26 AC 317 ms
87,048 KB
testcase_27 AC 335 ms
87,876 KB
testcase_28 AC 260 ms
89,928 KB
testcase_29 AC 404 ms
89,984 KB
testcase_30 AC 361 ms
91,252 KB
testcase_31 AC 394 ms
89,296 KB
testcase_32 AC 2 ms
5,248 KB
testcase_33 AC 176 ms
75,024 KB
testcase_34 AC 331 ms
94,928 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using ll = long long;
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define fi first
#define se second
mt19937_64 rng(chrono::system_clock::now().time_since_epoch().count());
const ll MOD1000000007 = 1000000007;
const ll MOD998244353 = 998244353;
const ll MOD[3] = {999727999, 1070777777, 1000000007};
const ll LINF = 1LL << 60LL;
const int IINF = (1 << 30) - 2;


template<typename T> 
struct edge{
    int from;
    int to;
    T cost;
    int id;

    edge(){}
    edge(int to, T cost=1) : from(-1), to(to), cost(cost){}
    edge(int from, int to, T cost) : from(from), to(to), cost(cost) {}
    edge(int from, int to, T cost, int id) : from(from), to(to), cost(cost), id(id){}

    void reverse(){swap(from, to);}
};

template<typename T>
struct edges : std::vector<edge<T>>{
    void sort(){
        std::sort(
            (*this).begin(),
            (*this).end(), 
            [](const edge<T>& a, const edge<T>& b){
                return a.cost < b.cost;
            }
        );
    }
};

template<typename T = bool>
struct graph : std::vector<edges<T>>{
private:
    int n = 0;
    int m = 0;
    edges<T> es;
    bool dir;

public:
    graph(int n, bool dir) : n(n), dir(dir){
        (*this).resize(n);
    }

    void add_edge(int from, int to, T cost=1){
        if(dir){
            es.push_back(edge<T>(from, to, cost, m));
            (*this)[from].push_back(edge<T>(from, to, cost, m++));
        }else{
            if(from > to) swap(from, to);
            es.push_back(edge<T>(from, to, cost, m));
            (*this)[from].push_back(edge<T>(from, to, cost, m));
            (*this)[to].push_back(edge<T>(to, from, cost, m++));
        }
    }

    int get_vnum(){
        return n;
    }

    int get_enum(){
        return m;
    }

    bool get_dir(){
        return dir;
    }

    edge<T> get_edge(int i){
        return es[i];
    }

    edges<T> get_edge_set(){
        return es;
    }
};

template<typename T>
struct redge{
    int from, to;
    T cap, cost;
    int rev;
    
    redge(int to, T cap, T cost=(T)(1)) : from(-1), to(to), cap(cap), cost(cost){}
    redge(int to, T cap, T cost, int rev) : from(-1), to(to), cap(cap), cost(cost), rev(rev){}
};

template<typename T> using Edges = vector<edge<T>>;
template<typename T> using weighted_graph = vector<Edges<T>>;
template<typename T> using tree = vector<Edges<T>>;
using unweighted_graph = vector<vector<int>>;
template<typename T> using residual_graph = vector<vector<redge<T>>>;


template<typename T>
struct mincostflow{
    residual_graph<T> G;
    const T TINF = std::numeric_limits<T>::max() / 2;
    int n;

    mincostflow(residual_graph<T> &G_){
        n = (int)G_.size();
        G.resize(n);

        for(int from=0; from<n; from++){
            for(redge<T> e : G_[from]){
                G[from].push_back(redge<T>(e.to, e.cap, e.cost, (int)G[e.to].size()));
                G[e.to].pb(redge<T>(from, 0, -e.cost, (int)G[from].size()-1));
            }
        }
    }

    T flow(int s, int t, T f){
        residual_graph<T> H(n);
        vector<T> h(n, 0); //ポテンシャル
        vector<T> dist(n, 0); //最短距離
        vector<int> prevv(n, 0); // 直前の頂点
        vector<int> preve(n, 0); // 直前の辺

        for(int from=0; from<n; from++){
            for(redge<T> e : G[from]){
                H[from].push_back(e);
            }
        }

        T res = 0;
        while(f > 0){
            //ダイクストラ法を用いてhを更新
            priority_queue<pair<T, int>, vector<pair<T, int>>, greater<pair<T, int>>> PQ;
            for(int i=0; i<n; i++) dist[i] = TINF;
            dist[s] = 0;
            PQ.push({0, s});
            while(!PQ.empty()){
                pair<T, int> p = PQ.top();
                PQ.pop();
                int v = p.se;
                if(dist[v] < p.fi) continue;

                for(int i=0; i<(int)H[v].size(); i++){
                    redge<T> &e = H[v][i];
                    if(e.cap > 0 && dist[e.to] > dist[v] + e.cost + h[v] - h[e.to]){
                        dist[e.to] = dist[v] + e.cost + h[v] - h[e.to];
                        prevv[e.to] = v;
                        preve[e.to] = i;
                        PQ.push({dist[e.to], e.to});
                    }
                }
            }
            if(dist[t] == TINF){
                //これ以上流せない
                return -1;
            }
            for(int v=0; v<n; v++) h[v] += dist[v];

            // s-t間最短経路に沿って目一杯流す
            T d = f;
            for(int v=t; v!=s; v=prevv[v]){
                d = min(d, H[prevv[v]][preve[v]].cap);
            }
            f -= d;
            res += d*h[t];
            for(int v=t; v!=s; v=prevv[v]){
                redge<T> &e = H[prevv[v]][preve[v]];
                e.cap -= d;
                H[v][e.rev].cap += d;
            }
        }

        return res;
    }
};


void solve(){
    int n, m; cin >> n >> m;
    residual_graph<ll> G(n);
    for(int i=0; i<m; i++){
        int u, v; cin >> u >> v;
        ll c, d; cin >> c >> d;
        u--; v--;
        G[u].pb(redge<ll>(v, 1, d));
        G[u].pb(redge<ll>(v, 1, c));
        G[v].pb(redge<ll>(u, 1, d));
        G[v].pb(redge<ll>(u, 1, c));
    }

    mincostflow<ll> mcf(G);
    cout << mcf.flow(0, n-1, 2) << endl;
}

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    
    int T=1;
    //cin >> T;
    while(T--) solve();
}
0