結果
| 問題 | No.1301 Strange Graph Shortest Path | 
| コンテスト | |
| ユーザー |  beet | 
| 提出日時 | 2020-11-27 21:22:11 | 
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 206 ms / 3,000 ms | 
| コード長 | 2,924 bytes | 
| コンパイル時間 | 2,439 ms | 
| コンパイル使用メモリ | 205,860 KB | 
| 最終ジャッジ日時 | 2025-01-16 06:17:36 | 
| ジャッジサーバーID (参考情報) | judge5 / judge1 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 2 | 
| other | AC * 33 | 
ソースコード
#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;
}
            
            
            
        