結果

問題 No.1301 Strange Graph Shortest Path
ユーザー outlineoutline
提出日時 2020-11-27 22:50:13
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 179 ms / 3,000 ms
コード長 4,136 bytes
コンパイル時間 1,638 ms
コンパイル使用メモリ 146,216 KB
実行使用メモリ 35,208 KB
最終ジャッジ日時 2023-10-09 21:42:07
合計ジャッジ時間 8,487 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,352 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 140 ms
32,184 KB
testcase_03 AC 107 ms
28,996 KB
testcase_04 AC 170 ms
31,312 KB
testcase_05 AC 114 ms
31,892 KB
testcase_06 AC 155 ms
29,112 KB
testcase_07 AC 152 ms
30,296 KB
testcase_08 AC 112 ms
29,188 KB
testcase_09 AC 141 ms
27,920 KB
testcase_10 AC 115 ms
28,840 KB
testcase_11 AC 153 ms
29,912 KB
testcase_12 AC 152 ms
30,004 KB
testcase_13 AC 136 ms
32,604 KB
testcase_14 AC 146 ms
27,940 KB
testcase_15 AC 150 ms
28,608 KB
testcase_16 AC 179 ms
31,284 KB
testcase_17 AC 148 ms
32,600 KB
testcase_18 AC 125 ms
29,752 KB
testcase_19 AC 160 ms
29,300 KB
testcase_20 AC 145 ms
28,320 KB
testcase_21 AC 137 ms
31,396 KB
testcase_22 AC 147 ms
29,176 KB
testcase_23 AC 140 ms
32,232 KB
testcase_24 AC 160 ms
29,092 KB
testcase_25 AC 168 ms
31,192 KB
testcase_26 AC 144 ms
30,000 KB
testcase_27 AC 150 ms
29,948 KB
testcase_28 AC 119 ms
31,844 KB
testcase_29 AC 165 ms
30,696 KB
testcase_30 AC 166 ms
30,996 KB
testcase_31 AC 168 ms
30,880 KB
testcase_32 AC 2 ms
4,356 KB
testcase_33 AC 70 ms
25,232 KB
testcase_34 AC 157 ms
35,208 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <queue>
#include <string>
#include <map>
#include <set>
#include <stack>
#include <tuple>
#include <deque>
#include <array>
#include <numeric>
#include <bitset>
#include <iomanip>
#include <cassert>
#include <chrono>
#include <random>
#include <limits>
#include <iterator>
#include <functional>
#include <sstream>
#include <fstream>
#include <complex>
#include <cstring>
#include <unordered_map>
using namespace std;

using ll = long long;
using P = pair<int, int>;
constexpr int INF = 1001001001;
// constexpr int mod = 1000000007;
constexpr int mod = 998244353;

template<class T>
inline bool chmax(T& x, T y){
    if(x < y){
        x = y;
        return true;
    }
    return false;
}
template<class T>
inline bool chmin(T& x, T y){
    if(x > y){
        x = y;
        return true;
    }
    return false;
}

template<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;
        edge(int to, flow_t cap, cost_t cost, int rev, bool isrev)
            : to(to), cap(cap), cost(cost), rev(rev), isrev(isrev) {}
    };
    vector<vector<edge>> graph;
    vector<cost_t> potential;   // ポテンシャルテーブル
    vector<cost_t> min_cost;    // 最小コストテーブル
    vector<int> prevv, preve;   // 経路復元用

    PrimalDual(int V, cost_t INF = numeric_limits<cost_t>::max() / 2) : graph(V), INF(INF) {}

    void add_edge(int from, int to, flow_t cap, cost_t cost){
        graph[from].emplace_back(edge(to, cap, cost, graph[to].size(), false));
        graph[to].emplace_back(edge(from, 0, -cost, 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 < (int)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;
            // s-t間最短路の沿って目一杯流す
            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;
    }
};

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int N, M;
    cin >> N >> M;
    PrimalDual<int, ll> pd(N);
    int s = 0, t = N - 1;
    for(int i = 0; i < M; ++i){
        int u, v, c, d;
        cin >> u >> v >> c >> d;
        --u, --v;
        pd.add_edge(u, v, 1, c);
        pd.add_edge(v, u, 1, c);
        pd.add_edge(u, v, 1, d);
        pd.add_edge(v, u, 1, d);
    }
    cout << pd.min_cost_flow(s, t, 2) << endl;
}
0