結果

問題 No.1301 Strange Graph Shortest Path
ユーザー chocoruskchocorusk
提出日時 2020-11-27 22:14:24
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 306 ms / 3,000 ms
コード長 2,565 bytes
コンパイル時間 2,894 ms
コンパイル使用メモリ 135,400 KB
実行使用メモリ 35,500 KB
最終ジャッジ日時 2023-10-09 21:18:30
合計ジャッジ時間 13,561 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,712 KB
testcase_01 AC 3 ms
5,736 KB
testcase_02 AC 249 ms
34,208 KB
testcase_03 AC 213 ms
31,120 KB
testcase_04 AC 305 ms
31,720 KB
testcase_05 AC 219 ms
34,272 KB
testcase_06 AC 263 ms
29,828 KB
testcase_07 AC 255 ms
31,888 KB
testcase_08 AC 210 ms
31,372 KB
testcase_09 AC 248 ms
28,608 KB
testcase_10 AC 210 ms
30,916 KB
testcase_11 AC 269 ms
30,812 KB
testcase_12 AC 283 ms
30,644 KB
testcase_13 AC 250 ms
34,456 KB
testcase_14 AC 245 ms
29,024 KB
testcase_15 AC 245 ms
30,068 KB
testcase_16 AC 306 ms
31,532 KB
testcase_17 AC 268 ms
34,680 KB
testcase_18 AC 239 ms
31,584 KB
testcase_19 AC 272 ms
30,040 KB
testcase_20 AC 270 ms
28,716 KB
testcase_21 AC 261 ms
33,036 KB
testcase_22 AC 282 ms
29,468 KB
testcase_23 AC 257 ms
34,140 KB
testcase_24 AC 279 ms
29,692 KB
testcase_25 AC 292 ms
31,960 KB
testcase_26 AC 263 ms
31,088 KB
testcase_27 AC 271 ms
31,076 KB
testcase_28 AC 224 ms
33,964 KB
testcase_29 AC 285 ms
30,804 KB
testcase_30 AC 273 ms
31,856 KB
testcase_31 AC 276 ms
31,492 KB
testcase_32 AC 3 ms
5,796 KB
testcase_33 AC 156 ms
26,812 KB
testcase_34 AC 264 ms
35,500 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <cassert>
#include <fstream>
#include <utility>
#include <functional>
#include <time.h>
#include <stack>
#include <array>
#define popcount __builtin_popcount
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
//最小重み二部マッチング復元 https://judge.yosupo.jp/submission/1155

struct edge{
    int to, cap, rev; ll cost;
    edge(int to, int cap, ll cost, int rev):to(to), cap(cap), cost(cost), rev(rev){}
};

int V;
vector<edge> g[100010];
ll h[100010];
ll dist[100010];
int prevv[100010], preve[100010];

void add_edge(int from, int to, int cap, ll cost){
    edge e=edge(to, cap, cost, g[to].size());
    g[from].push_back(e);
    e=edge(from, 0, -cost, g[from].size()-1);
    g[to].push_back(e);
}

ll min_cost_flow(int s, int t, int f){
    using P=pair<ll, int>;
    const ll INF=1e18;
    ll res=0;
    fill(h, h+V, 0);
    while(f>0){
        priority_queue<P, vector<P>, greater<P>> que;
        fill(dist, dist+V, INF);
        dist[s]=0;
        que.push({0, s});
        while(!que.empty()){
            P p=que.top(); que.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;
                    que.push({dist[e.to], e.to});
                }
            }
        }
        for(int v=0; v<V; v++) h[v]+=dist[v];
        if(dist[t]==INF) return -1;
        int 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()
{
    int n, m; cin>>n>>m;
    V=n;
    for(int i=0; i<m; i++){
        int u, v; cin>>u>>v; u--; v--;
        ll c, d; cin>>c>>d;
        add_edge(u, v, 1, c);
        add_edge(u, v, 1, d);
        add_edge(v, u, 1, c);
        add_edge(v, u, 1, d);
        
    }
    cout<<min_cost_flow(0, n-1, 2)<<endl;
	return 0;
}
0