library

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub beet-aizu/library

:heavy_check_mark: initialize potential
(test/aoj/2290.test.cpp)

Depends on

Code

#define PROBLEM "http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2290"

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

#define call_from_test
#include "../../mincostflow/primaldual.cpp"
#include "../../tools/chminmax.cpp"
#include "../../vector/identity.cpp"
#undef call_from_test

#ifdef SANITIZE
#define IGNORE
#endif

signed main(){
  cin.tie(0);
  ios::sync_with_stdio(0);

  int n,v,xleft,xright;
  cin>>n>>v>>xleft>>xright;

  int S=n+n,T=n+n+1,L=n+n+2,R=n+n+3;
  PrimalDual<int, int> G(n+n+4);

  G.add_edge(S,L,1,0);
  G.add_edge(L,T,1,0);

  G.add_edge(S,R,1,0);
  G.add_edge(R,T,1,0);

  vector<int> xs(n),ts(n),ps(n);
  for(int i=0;i<n;i++){
    cin>>xs[i]>>ts[i]>>ps[i];

    if(abs(xs[i]-xleft)<=ts[i]*v)
      G.add_edge(L,i,1,0);

    if(abs(xs[i]-xright)<=ts[i]*v)
      G.add_edge(R,i,1,0);
  }

  auto init=[&](auto &h)->void{
    fill(h.begin(),h.end(),0);

    auto ord=identity(n);
    sort(ord.begin(),ord.end(),
         [&](int i,int j){return ts[i]<ts[j];});

    auto add_edge=[&](int u,int v,int f,int c){
      G.add_edge(u,v,f,c);
      chmin(h[v],h[u]+c);
    };

    for(int i=0;i<n;i++){
      int x=ord[i];
      add_edge(x,n+x,1,-ps[x]);
      for(int j=i+1;j<n;j++){
        int y=ord[j];
        if(abs(xs[x]-xs[y])<=(ts[y]-ts[x])*v)
          add_edge(n+x,y,1,0);
      }
      add_edge(n+x,T,1,0);
    }
  };
  assert(G.build(S,T,2,init));
  cout<<-G.get_cost()<<endl;
  return 0;
}
#line 1 "test/aoj/2290.test.cpp"
#define PROBLEM "http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2290"

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

#define call_from_test
#line 1 "mincostflow/primaldual.cpp"

#line 3 "mincostflow/primaldual.cpp"
using namespace std;
#endif
//BEGIN CUT HERE
// 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;}
};
//END CUT HERE
#ifndef call_from_test
//INSERT ABOVE HERE
signed main(){
  return 0;
}
#endif
#line 1 "tools/chminmax.cpp"

#line 3 "tools/chminmax.cpp"
using namespace std;
#endif
//BEGIN CUT HERE
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;}
//END CUT HERE
#ifndef call_from_test
signed main(){
  return 0;
}
#endif
#line 1 "vector/identity.cpp"

#line 3 "vector/identity.cpp"
using namespace std;
#endif

//BEGIN CUT HERE
vector<int> identity(int n){
  vector<int> ord(n);
  iota(ord.begin(),ord.end(),0);
  return ord;
}
//END CUT HERE
#ifndef call_from_test
signed main(){
  return 0;
}
#endif
#line 10 "test/aoj/2290.test.cpp"
#undef call_from_test

#ifdef SANITIZE
#define IGNORE
#endif

signed main(){
  cin.tie(0);
  ios::sync_with_stdio(0);

  int n,v,xleft,xright;
  cin>>n>>v>>xleft>>xright;

  int S=n+n,T=n+n+1,L=n+n+2,R=n+n+3;
  PrimalDual<int, int> G(n+n+4);

  G.add_edge(S,L,1,0);
  G.add_edge(L,T,1,0);

  G.add_edge(S,R,1,0);
  G.add_edge(R,T,1,0);

  vector<int> xs(n),ts(n),ps(n);
  for(int i=0;i<n;i++){
    cin>>xs[i]>>ts[i]>>ps[i];

    if(abs(xs[i]-xleft)<=ts[i]*v)
      G.add_edge(L,i,1,0);

    if(abs(xs[i]-xright)<=ts[i]*v)
      G.add_edge(R,i,1,0);
  }

  auto init=[&](auto &h)->void{
    fill(h.begin(),h.end(),0);

    auto ord=identity(n);
    sort(ord.begin(),ord.end(),
         [&](int i,int j){return ts[i]<ts[j];});

    auto add_edge=[&](int u,int v,int f,int c){
      G.add_edge(u,v,f,c);
      chmin(h[v],h[u]+c);
    };

    for(int i=0;i<n;i++){
      int x=ord[i];
      add_edge(x,n+x,1,-ps[x]);
      for(int j=i+1;j<n;j++){
        int y=ord[j];
        if(abs(xs[x]-xs[y])<=(ts[y]-ts[x])*v)
          add_edge(n+x,y,1,0);
      }
      add_edge(n+x,T,1,0);
    }
  };
  assert(G.build(S,T,2,init));
  cout<<-G.get_cost()<<endl;
  return 0;
}
Back to top page