Submission #569526

#TimeUsernameProblemLanguageResultExecution timeMemory
569526codebuster_10Robot (JOI21_ho_t4)C++17
34 / 100
3066 ms44656 KiB
#include <bits/stdc++.h>
#define int int64_t //be careful about this 

using namespace std;

#define vt vector 
#define ar array 
#define pr pair 

#define f first 
#define s second

#define pb push_back
#define eb emplace_back

#define fr(i,a,b) for(int i = a; i < b; ++i)
#define rf(i,a,b) for(int i = b-1; i >= a; --i)
#define all(x) x.begin(),x.end()
#define mem(a,b) memset(a,b,sizeof(a))

namespace IN{
  template<class T> void re(vector<T> &A);
  template<class S,class T> void re(pair<S,T> &A);
  template<class T,size_t N> void re(array<T,N> &A);

  template<class T> void re(T& x){ 
    cin >> x;}
  template<class H, class... T> void re(H& h, T&... t){ 
    re(h); re(t...);}

  template<class T> void re(vector<T> &A){ 
    for(auto& x : A) re(x);}
  template<class S,class T> void re(pair<S,T> &A){ 
      re(A.first); re(A.second);}
  template<class T,size_t N> void re(array<T,N> &A){
    for(int i = 0; i < N; ++i)  re(A[i]);}
}

namespace OUT{
  template<class T>
  void __p(const T& a){ cout<<a; }
  template<class T, class F>
  void __p(const pair<T, F>& a){ cout<<"{"; __p(a.first); cout<<","; __p(a.second); cout<<"}\n"; }
  template<class T, size_t N>
  void __p(const array<T,N>& a){ cout<<"{"; for(int i=0;i<N;++i)__p(a[i]),cout<<",}\n"[i+1==N]; }
  template<class T>
  void __p(const vector<T>& a){
    cout<<"{";for(auto it=a.begin();it<a.end();it++)__p(*it),cout<<",}\n"[it+1==a.end()]; }
  template<class T, class ...Arg>
  void __p(T a1, Arg ...a){__p(a1); __p(a...); }
  template<class Arg1>
  void __f(const char *s, Arg1 &&arg1){ cout<<s<<" : "; __p(arg1); cout<<endl; }
  template<class Arg1, class ... Args>
  void __f(const char *ss, Arg1 &&arg1, Args &&... args){
    int b=0,i=0; do{ if(ss[i]=='(') b++; if(ss[i]==')') b--; i++;}while(!(ss[i]==','&&b==0));
    const char *comma=ss+i; cout.write(ss,comma-ss)<<" : ";__p(arg1);cout<<" | ";__f(comma+1,args...);}
  #define trace(...) cout<<"Line:"<<__LINE__<<"  ", __f(#__VA_ARGS__, __VA_ARGS__)
}


namespace FUN{
  void IO(string s = ""){
    ios_base::sync_with_stdio(NULL); 
    cin.tie(nullptr); 
    cout.precision(20); 
    cout << fixed;
    if(!s.empty()){
      freopen((s+".in").c_str(),"r",stdin);
      freopen((s+".out").c_str(),"w",stdout);
    }
  }

  const auto start_time = chrono::high_resolution_clock::now();
  void output_run_time(){
    // will work for ac,cc&&cf.
#ifndef ONLINE_JUDGE
    auto end_time = chrono::high_resolution_clock::now();
    chrono::duration<double> diff = end_time-start_time;
      cout << "\n\n\nTime Taken : " << diff.count();
#endif
  }

  template<class T> bool ckmin(T& a, const T& b){ 
    return b < a ? a = b, true : false; }
    
  template<class T> bool ckmax(T& a, const T& b){ 
    return a < b ? a = b, true : false; }

  mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
  int my_rand(int L, int R){ 
    return uniform_int_distribution<int>(L,R)(rng); }
  
  template<class T> int sz(const T& x){ 
    return int(x.size()); }

  template<class T> int lb(const vector<T>& vec,const T& val){
    return int(lower_bound(vec.begin(), vec.end(),val) - vec.begin()); }

  template<class T> int ub(const vector<T>& vec,const T& val){
    return int(upper_bound(vec.begin(), vec.end(),val) - vec.begin()); }

  constexpr int  dx[4] = {1,0,-1,0};
  constexpr int  dy[4] = {0,1,0,-1};
  constexpr char dr[4] = {'D','R','U','L'};

  constexpr long long INFLL1 = 1e16, INFLL2 = 9e18;
  constexpr int INF = 2e9;

  template<class T>
  vector<T> V(int n,T val){
    return vector<T> (n,val);
  }

  template<class T>
  vector<vector<T>> V(int n,int m,T val){
    return vector<vector<T>> (n,vector<T> (m,val));
  }

  template<class T>
  vector<vector<vector<T>>> V(int n,int m,int k,T val){
    return vector<vector<vector<T>>> (n,vector<vector<T>> (m,vector<T> (k,val)));
  }
}


using namespace IN;
using namespace OUT;
using namespace FUN;




















signed main(){
  IO();

  int n,m;
  re(n,m);

  
  vt<int> u(2*m), v(2*m), c(2*m), p(2*m);

  vt<vt<int>> g(n);


  fr(e,0,m){
    int u_,v_,c_,p_;
    re(u_,v_,c_,p_);
    --u_,--v_,--c_;
    if(u_ < v_)
      swap(u_,v_);
    u[2*e] = v_, v[2*e] = u_;
    u[2*e+1] = u_, v[2*e+1] = v_;
    c[2*e] = c[2*e+1] = c_;
    p[2*e] = p[2*e+1] = p_;
    g[u_].pb(2*e+1);
    g[v_].pb(2*e);
  }





  priority_queue<ar<int,3>> pq;
  
  vt<ar<int,2>> dis(2*m,{INFLL1,INFLL1});

  vt<int> tot(m,0);

  auto upd = [&](int i) -> void {
    for(auto j : g[i])
      tot[c[j]] = 0;
    for(auto j : g[i])
      tot[c[j]] += p[j];
  };

  upd(0);

  for(auto id : g[0]){
    // change current color.
    if(ckmin(dis[id][1],p[id]))
      pq.push({-dis[id][1],id,1});
    //do change current color.
    if(ckmin(dis[id][0],tot[c[id]] - p[id]))
      pq.push({-dis[id][0],id,0});
  }

  int ans = INFLL1;

  while(!pq.empty()){
    auto [d,i,f] = pq.top(); d *= -1; pq.pop();

    if(d != dis[i][f])
      continue;

    if(v[i] == n-1)
      ckmin(ans,d);

    upd(v[i]);

    for(auto j : g[v[i]]){

      if(f == 0){
        // change current color.
        if(ckmin(dis[j][1],dis[i][f] + p[j]))
          pq.push({-dis[j][1],j,1});
        // do not change current color.
        if(ckmin(dis[j][0],dis[i][f] + tot[c[j]] - p[j]))
          pq.push({-dis[j][0],j,0});
      }else{
        if(c[i] == c[j]){
          // change current color.
          if(ckmin(dis[j][1],dis[i][f] + p[j]))
            pq.push({-dis[j][1],j,1});
          // do not change current color.
          if(ckmin(dis[j][0],dis[i][f] + tot[c[j]] - p[j] - p[i]))
            pq.push({-dis[j][0],j,0});
        }else{
          // change current color.
          if(ckmin(dis[j][1],dis[i][f] + p[j]))
            pq.push({-dis[j][1],j,1});
          // do not change current color.
          if(ckmin(dis[j][0],dis[i][f] + tot[c[j]] - p[j]))
            pq.push({-dis[j][0],j,0});
        }
      }
    }
  }

  cout << (ans == INFLL1 ? int(-1) : ans);




  // output_run_time();
  return 0;
  
}





















Compilation message (stderr)

Main.cpp: In function 'void FUN::IO(std::string)':
Main.cpp:68:14: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   68 |       freopen((s+".in").c_str(),"r",stdin);
      |       ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:69:14: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   69 |       freopen((s+".out").c_str(),"w",stdout);
      |       ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...