結果

問題 No.1301 Strange Graph Shortest Path
ユーザー snow39snow39
提出日時 2020-11-27 22:42:24
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
CE  
(最新)
AC  
(最初)
実行時間 -
コード長 2,749 bytes
コンパイル時間 755 ms
コンパイル使用メモリ 95,308 KB
最終ジャッジ日時 2024-11-14 23:55:24
合計ジャッジ時間 2,209 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
コンパイルエラー時のメッセージ・ソースコードは、提出者また管理者しか表示できないようにしております。(リジャッジ後のコンパイルエラーは公開されます)
ただし、clay言語の場合は開発者のデバッグのため、公開されます。

コンパイルメッセージ
main.cpp: In constructor 'MinCostFlow<flow_t, cost_t>::MinCostFlow(int)':
main.cpp:36:37: error: 'numeric_limits' was not declared in this scope
   36 |   MinCostFlow(int V) : graph(V),INF(numeric_limits< cost_t >::max()) {}
      |                                     ^~~~~~~~~~~~~~
main.cpp:36:60: error: expected primary-expression before '>' token
   36 |   MinCostFlow(int V) : graph(V),INF(numeric_limits< cost_t >::max()) {}
      |                                                            ^
main.cpp:36:66: error: no matching function for call to 'max()'
   36 |   MinCostFlow(int V) : graph(V),INF(numeric_limits< cost_t >::max()) {}
      |                                                             ~~~~~^~
In file included from /home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/string:50,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/bits/locale_classes.h:40,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/bits/ios_base.h:41,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/ios:42,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/ostream:38,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/iostream:39,
                 from main.cpp:1:
/home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/bits/stl_algobase.h:254:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)'
  254 |     max(const _Tp& __a, const _Tp& __b)
      |     ^~~
/home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/bits/stl_algobase.h:254:5: note:   template argument deduction/substitution failed:
main.cpp:36:66: note:   candidate expects 2 arguments, 0 provided
   36 |   MinCostFlow(int V) : graph(V),INF(numeric_limits< cost_t >::max()) {}
      |                                                             ~~~~~^~
/home/linuxbrew/.linuxbrew/Ce

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cmath>
#include <map>
#include <queue>
#include <iomanip>
#include <set>
#include <tuple>
#define mkp make_pair
#define mkt make_tuple
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define all(v) v.begin(),v.end()
using namespace std;
typedef long long ll;
const ll MOD=1e9+7;
template<class T> void chmin(T &a,const T &b){if(a>b) a=b;}
template<class T> void chmax(T &a,const T &b){if(a<b) a=b;}

template< typename flow_t, typename cost_t >
struct MinCostFlow {
  const cost_t INF;

  struct edge {
    int to;
    flow_t cap;
    cost_t cost;
    int rev;
    bool isrev;
  };
  vector<vector<edge>> graph;
  vector<cost_t> potential,min_cost;
  vector<int> prevv,preve;

  MinCostFlow(int V) : graph(V),INF(numeric_limits< cost_t >::max()) {}

  void add_edge(int from,int to,flow_t cap,cost_t cost){
    graph[from].emplace_back((edge){to,cap,cost,(int)graph[to].size(),false});
    graph[to].emplace_back((edge){from,0,-cost,(int)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>> PQ;
    potential.assign(V,0);
    preve.assign(V,-1);
    prevv.assign(V,-1);

    while(f>0){
      min_cost.assign(V,INF);
      PQ.emplace(0,s);
      min_cost[s]=0;
      while(!PQ.empty()){
        cost_t d;
        int now;
        tie(d,now)=PQ.top();
        PQ.pop();
        if(min_cost[now]<d) continue;
        
        for(int i=0;i<graph[now].size();i++){
          edge &e=graph[now][i];
          cost_t nextCost=min_cost[now]+(e.cost+potential[now]-potential[e.to]);
          if(e.cap>0&&min_cost[e.to]>nextCost){
            min_cost[e.to]=nextCost;
            prevv[e.to]=now;
            preve[e.to]=i;
            PQ.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;
      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(){
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N,M;
  cin>>N>>M;
  MinCostFlow<int,ll> mcf(N);
  rep(i,M){
    int a,b,c,d;
    cin>>a>>b>>c>>d;
    a--;b--;
    mcf.add_edge(a,b,1,c);
    mcf.add_edge(b,a,1,c);
    mcf.add_edge(a,b,1,d);
    mcf.add_edge(b,a,1,d);
  }

  ll ans=mcf.min_cost_flow(0,N-1,2);
  cout<<ans<<endl;


  return 0;
}
0