Submission #1158774

#TimeUsernameProblemLanguageResultExecution timeMemory
1158774Zero_OPOlympic Bus (JOI20_ho_t4)C++20
0 / 100
1098 ms119880 KiB
#include <bits/stdc++.h>

using namespace std;

#define FOR(i, l, r) for(int i = (l); i < (r); ++i)
#define ROF(i, r, l) for(int i = (r) - 1; i >= (l); --i)

#define all(v) begin(v), end(v)
#define rall(v) rbegin(v), rend(v)
#define sz(v) (int)v.size()
#define pb push_back
#define eb emplace_back
#define compact(v) v.erase(unique(all(v)), end(v))

#define mp make_pair
#define mt make_tuple
#define ff first
#define ss second

#define dbg(x) "[" #x " = " << (x) << "]"
#define readFile(task) freopen(task, "r", stdin)
#define writeFile(task) freopen(task, "w", stdout)

template<typename T>
      bool minimize(T& a, const T& b){
            if(a > b) return a = b, true;
            return false;
      }

template<typename T>
      bool maximize(T& a, const T& b){
            if(a < b) return a = b, true;
            return false;
      }

using ll = long long;
using db = double;
using ld = long double;
using ull = unsigned long long;

using pi = pair<int, int>;
using pl = pair<ll, ll>;
using pd = pair<db, db>;

using vi = vector<int>;
using vb = vector<bool>;
using vstr = vector<string>;
using vl = vector<ll>;
using vd = vector<double>;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

void setIO(){
      ios_base::sync_with_stdio(0); cin.tie(0);
#ifdef LOCAL
      readFile("task.inp");
      writeFile("task.out");
#endif //LOCAL
}

int main(){
      setIO();

      int N, M;
      cin >> N >> M;
      vector<vi> adj(N), adj_rev(N);
      vi U(M), V(M), C(M), D(M);
      FOR(i, 0, M){
            int u, v, c, d;
            cin >> u >> v >> c >> d;
            --u, --v;

            tie(U[i], V[i], C[i], D[i]) = mt(u, v, c, d);
            adj[u].pb(i);
            adj_rev[v].pb(i);
      }

      const int inf = 2e9;
      vector<vector<vi>> dp(2, vector<vi>(N, vi(M+1, inf)));

      //d[t][u][idx] = minimum cost to travel from 0 to to and inverted edge idx, if visited N-1, t = 1, otherwise t = 0

      using node = tuple<int, int, int, int>;
      priority_queue<node, vector<node>, greater<node>> pq;

      dp[0][0][M] = 0;
      pq.push(mt(0, 0, 0, M));

      int dist, t, u, idx;
      while(!pq.empty()){
            tie(dist, t, u, idx) = pq.top(); pq.pop();
            if(dp[t][u][idx] != dist) continue;

            if(u == 0 && t == 1){
                  cout << dist << '\n';
                  return 0;
            }

            for(auto id : adj[u]) if(id != idx){
                  int v = V[id], w = C[id], nt = t | (v == N-1);
                  if(dp[nt][v][idx] > dist + w){
                        dp[nt][v][idx] = dist + w;
                        pq.push(mt(dp[nt][v][idx], nt, v, idx));
                  }
            }

            if(idx == M){
                  for(auto id : adj_rev[u]){
                        int v = U[id], w = C[id] + D[id], nt = t | (v == N-1);
                        if(dp[nt][v][id] > dist + w){
                              dp[nt][v][id] = dist + w;
                              pq.push(mt(dp[nt][v][id], nt, v, id));
                        }
                  }
            } else{
                  if(u == V[idx]){
                        //(V[idx] -> U[idx]) with cost C[idx]
                        int v = U[idx], w = C[idx];
                        int nt = t | (v == N-1);
                        if(dp[nt][v][idx] > dist + w){
                              dp[nt][v][idx] = dist + w;
                              pq.push(mt(dp[nt][v][idx], nt, v, idx));
                        }
                  }
            }
      }


      cout << -1 << '\n';

      return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...