結果

問題 No.1301 Strange Graph Shortest Path
ユーザー IKyoproIKyopro
提出日時 2020-12-02 22:15:37
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 218 ms / 3,000 ms
コード長 2,754 bytes
コンパイル時間 2,303 ms
コンパイル使用メモリ 213,580 KB
実行使用メモリ 44,348 KB
最終ジャッジ日時 2023-10-11 12:01:27
合計ジャッジ時間 10,615 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 161 ms
41,596 KB
testcase_03 AC 133 ms
37,304 KB
testcase_04 AC 215 ms
39,420 KB
testcase_05 AC 136 ms
41,352 KB
testcase_06 AC 189 ms
36,676 KB
testcase_07 AC 174 ms
38,952 KB
testcase_08 AC 137 ms
37,744 KB
testcase_09 AC 179 ms
34,712 KB
testcase_10 AC 138 ms
37,156 KB
testcase_11 AC 191 ms
37,932 KB
testcase_12 AC 193 ms
37,896 KB
testcase_13 AC 169 ms
41,916 KB
testcase_14 AC 180 ms
34,976 KB
testcase_15 AC 175 ms
36,560 KB
testcase_16 AC 218 ms
39,220 KB
testcase_17 AC 183 ms
42,008 KB
testcase_18 AC 160 ms
38,020 KB
testcase_19 AC 200 ms
36,828 KB
testcase_20 AC 207 ms
35,632 KB
testcase_21 AC 175 ms
40,332 KB
testcase_22 AC 203 ms
36,540 KB
testcase_23 AC 175 ms
41,568 KB
testcase_24 AC 208 ms
36,328 KB
testcase_25 AC 215 ms
39,320 KB
testcase_26 AC 186 ms
38,036 KB
testcase_27 AC 197 ms
38,040 KB
testcase_28 AC 148 ms
41,160 KB
testcase_29 AC 217 ms
38,488 KB
testcase_30 AC 208 ms
39,308 KB
testcase_31 AC 217 ms
38,836 KB
testcase_32 AC 2 ms
4,352 KB
testcase_33 AC 88 ms
32,140 KB
testcase_34 AC 212 ms
44,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <class T> using vec = vector<T>;
template <class T> using vvec = vector<vec<T>>;
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;}
#define rep(i,n) for(int i=0;i<(n);i++)
#define drep(i,n) for(int i=(n)-1;i>=0;i--)
#define all(x) (x).begin(),(x).end()
#define debug(x)  cerr << #x << " = " << (x) << endl;

typedef pair<ll,ll> P;
const ll inf = 1e17;

class min_cost_flow{
private:
    int N;
    struct edge{int to; ll cap,cost; int rev; bool is_rev;};
    vector<vector<edge>> G;
    vector<ll> h,dist,prevv,preve;
public:
    min_cost_flow(int n){
        N = n;
        G = vector<vector<edge>>(N);
        h = dist = prevv = preve = vector<ll>(N,0);
    }
    void add_edge(int from,int to,ll cap,ll cost){
        G[from].push_back((edge){to,cap,cost,(int) G[to].size(),false});
        G[to].push_back((edge){from,0,-cost,(int) G[from].size()-1,true});
    }
    ll answer(int s,int t,ll f){
        ll res = 0;
        fill(h.begin(),h.end(),0);
        while(f>0){
            priority_queue<P,vector<P>,greater<P>> Q;
            fill(dist.begin(),dist.end(),inf);
            dist[s] = 0;
            Q.push(P(0,s));
            while(!Q.empty()){
                P p = Q.top(); Q.pop();
                int v = p.second;
                if(dist[v]<p.first) continue;
                for(int i=0;i<G[v].size();i++){
                    edge &e = G[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;
                        Q.push(P(dist[e.to],e.to));
                    }
                }
            }
            if(dist[t]==inf) return -1;
            for(int v=0;v<N;v++) h[v] += dist[v];
            ll d = f;
            for(int v=t;v!=s; v=prevv[v]){
                d = min(d,G[prevv[v]][preve[v]].cap);
            }
            f -= d;
            res += d*h[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(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N,M;
    cin >> N >> M;
    min_cost_flow flow(N);
    rep(i,M){
        int a,b,c,d;
        cin >> a >> b >> c >> d;
        a--,b--;
        flow.add_edge(a,b,1,c);
        flow.add_edge(a,b,1,d);
        flow.add_edge(b,a,1,c);
        flow.add_edge(b,a,1,d);
    }
    cout << flow.answer(0,N-1,2) << "\n";
}
0