結果

問題 No.1301 Strange Graph Shortest Path
ユーザー beetbeet
提出日時 2020-11-27 21:22:11
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 210 ms / 3,000 ms
コード長 2,924 bytes
コンパイル時間 2,409 ms
コンパイル使用メモリ 212,664 KB
実行使用メモリ 43,292 KB
最終ジャッジ日時 2023-10-10 21:38:29
合計ジャッジ時間 9,822 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 164 ms
41,656 KB
testcase_03 AC 133 ms
37,444 KB
testcase_04 AC 207 ms
39,276 KB
testcase_05 AC 137 ms
41,456 KB
testcase_06 AC 182 ms
36,664 KB
testcase_07 AC 169 ms
39,524 KB
testcase_08 AC 132 ms
37,676 KB
testcase_09 AC 170 ms
34,900 KB
testcase_10 AC 133 ms
37,284 KB
testcase_11 AC 181 ms
38,016 KB
testcase_12 AC 187 ms
37,976 KB
testcase_13 AC 159 ms
41,884 KB
testcase_14 AC 161 ms
34,964 KB
testcase_15 AC 158 ms
36,620 KB
testcase_16 AC 205 ms
39,336 KB
testcase_17 AC 176 ms
42,084 KB
testcase_18 AC 160 ms
38,132 KB
testcase_19 AC 185 ms
36,880 KB
testcase_20 AC 183 ms
35,496 KB
testcase_21 AC 169 ms
40,496 KB
testcase_22 AC 184 ms
36,592 KB
testcase_23 AC 164 ms
41,596 KB
testcase_24 AC 183 ms
36,436 KB
testcase_25 AC 194 ms
39,312 KB
testcase_26 AC 170 ms
38,196 KB
testcase_27 AC 200 ms
38,096 KB
testcase_28 AC 143 ms
41,216 KB
testcase_29 AC 210 ms
38,588 KB
testcase_30 AC 190 ms
39,472 KB
testcase_31 AC 197 ms
38,772 KB
testcase_32 AC 2 ms
4,348 KB
testcase_33 AC 96 ms
32,564 KB
testcase_34 AC 194 ms
43,292 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

using Int = long long;
const char newl = '\n';

template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}
template<typename T> void drop(const T &x){cout<<x<<endl;exit(0);}
template<typename T=Int>
vector<T> read(size_t n){
  vector<T> ts(n);
  for(size_t i=0;i<n;i++) cin>>ts[i];
  return ts;
}


// O(F E \log V)
template<typename Flow, typename Cost>
struct PrimalDual{
  struct Edge{
    Int dst;
    Flow cap;
    Cost cost;
    Int rev;
    Edge(Int dst,Flow cap,Cost cost,Int rev):
      dst(dst),cap(cap),cost(cost),rev(rev){}
  };

  vector<vector<Edge>> G;
  vector<Cost> h,dist;
  vector<Int> prevv,preve;

  PrimalDual(Int n):G(n),h(n),dist(n),prevv(n),preve(n){}

  void add_edge(Int u,Int v,Flow cap,Cost cost){
    Int e=G[u].size();
    Int r=(u==v?e+1:G[v].size());
    G[u].emplace_back(v,cap,cost,r);
    G[v].emplace_back(u,0,-cost,e);
  }

  Cost residual_cost(Int src,Edge &e){
    return e.cost+h[src]-h[e.dst];
  }

  void dijkstra(Int s){
    struct P{
      Cost first;
      Int second;
      P(Cost first,Int second):first(first),second(second){}
      bool operator<(const P&a) const{return first>a.first;}
    };
    priority_queue<P> pq;

    dist[s]=0;
    pq.emplace(dist[s],s);
    while(!pq.empty()){
      P p=pq.top();pq.pop();
      Int v=p.second;
      if(dist[v]<p.first) continue;
      for(Int i=0;i<(Int)G[v].size();i++){
        Edge &e=G[v][i];
        if(e.cap==0) continue;
        if(!(dist[v]+residual_cost(v,e)<dist[e.dst])) continue;
        dist[e.dst]=dist[v]+e.cost+h[v]-h[e.dst];
        prevv[e.dst]=v;
        preve[e.dst]=i;
        pq.emplace(dist[e.dst],e.dst);
      }
    }
  }

  Cost res;

  bool build(Int s,Int t,Flow f,
             function<void(decltype(h)&)> init=[](decltype(h) &p){
               fill(p.begin(),p.end(),0);
             }){
    res=0;
    init(h);
    const Cost INF = numeric_limits<Cost>::max();
    while(f>0){
      fill(dist.begin(),dist.end(),INF);
      dijkstra(s);
      if(dist[t]==INF) return false;

      for(Int v=0;v<(Int)h.size();v++)
        if(dist[v]<INF) h[v]=h[v]+dist[v];

      Flow d=f;
      for(Int v=t;v!=s;v=prevv[v])
        d=min(d,G[prevv[v]][preve[v]].cap);

      f-=d;
      res=res+h[t]*d;
      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 true;
  }

  Cost get_cost(){return res;}
};

//INSERT ABOVE HERE
signed main(){
  cin.tie(0);
  ios::sync_with_stdio(0);

  Int n,m;
  cin>>n>>m;
  PrimalDual<Int, Int> G(n);
  for(Int i=0;i<m;i++){
    Int u,v,c,d;
    cin>>u>>v>>c>>d;
    u--;v--;
    G.add_edge(u,v,1,c);
    G.add_edge(v,u,1,c);
    G.add_edge(u,v,1,d);
    G.add_edge(v,u,1,d);
  }

  G.build(0,n-1,2);
  cout<<G.get_cost()<<newl;
  return 0;
}
0