제출 #337161

#제출 시각아이디문제언어결과실행 시간메모리
337161VROOM_VARUNOlympic Bus (JOI20_ho_t4)C++14
5 / 100
1095 ms3692 KiB
/*
ID: varunra2
LANG: C++
TASK: olympic
*/

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

#ifdef DEBUG
#include "lib/debug.h"
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#define debug_arr(...) \
  cerr << "[" << #__VA_ARGS__ << "]:", debug_arr(__VA_ARGS__)
#pragma GCC diagnostic ignored "-Wsign-compare"
//#pragma GCC diagnostic ignored "-Wunused-parameter"
//#pragma GCC diagnostic ignored "-Wunused-variable"
#else
#define debug(...) 42
#endif

#define int long long

#define EPS 1e-9
#define IN(A, B, C) assert(B <= A && A <= C)
#define INF (int)1e15
#define MEM(a, b) memset(a, (b), sizeof(a))
#define MOD 1000000007
#define MP make_pair
#define PB push_back
#define all(cont) cont.begin(), cont.end()
#define rall(cont) cont.end(), cont.begin()
#define x first
#define y second

const double PI = acos(-1.0);
typedef long long ll;
typedef long double ld;
typedef pair<int, int> PII;
typedef map<int, int> MPII;
typedef multiset<int> MSETI;
typedef set<int> SETI;
typedef set<string> SETS;
typedef vector<int> VI;
typedef vector<PII> VII;
typedef vector<VI> VVI;
typedef vector<string> VS;

#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define trav(a, x) for (auto& a : x)
#define sz(x) (int)(x).size()
typedef pair<int, int> pii;
typedef vector<int> vi;
#pragma GCC diagnostic ignored "-Wsign-compare"
// util functions

int n, m;
vector<vector<pair<int, PII>>> adj;
int mn = INF;
VVI dists;

void init() {
  adj.resize(n);
  dists.resize(n);
}

VI dijk(int src, VI ign = {-1, -1, -1, -1}) {
  VI dist(n, INF);
  vector<bool> vis(n, false);
  priority_queue<PII, VII, greater<PII>> pq;

  dist[src] = 0;
  pq.push(MP(0, src));

  while (!pq.empty()) {
    auto xx = pq.top();
    pq.pop();
    int u = xx.y;
    if (vis[u]) continue;
    vis[u] = true;
    for (auto& x : adj[u]) {
      if (vis[x.x]) continue;
      if (u == ign[0] and x.x == ign[1] and x.y.x == ign[2] and x.y.y == ign[3])
        continue;
      if (dist[x.x] > dist[u] + x.y.x) {
        dist[x.x] = dist[u] + x.y.x;
        pq.push(MP(dist[x.x], x.x));
      }
    }
  }
  return dist;
}

void ckmin(int& a, int b) { a = min(a, b); }

int32_t main() {
  // #ifndef ONLINE_JUDGE
  // freopen("olympic.in", "r", stdin);
  // freopen("olympic.out", "w", stdout);
  // #endif
  cin.sync_with_stdio(0);
  cin.tie(0);

  cin >> n >> m;

  init();

  for (int i = 0; i < m; i++) {
    int u, v, c, d;
    cin >> u >> v >> c >> d;
    u--, v--;
    adj[u].PB(MP(v, MP(c, d)));
    // if (u == 1 and v == 3 and c == 2 and d == 5) debug("here reeeee");
  }

  for (int i = 0; i < n; i++) {
    dists[i] = dijk(i);
  }

  int ret = INF;

  ckmin(ret, dists[0][n - 1] + dists[n - 1][0]);

  VVI goodedgs;

  rep(u, 0, n) for (int j = 0; j < sz(adj[u]); j++) {
    auto x = adj[u][j];
    int v = x.x;
    int cost = x.y.x;
    int swip = x.y.y;
    bool ok = false;
    if (dists[0][v] + dists[u][n - 1] + cost < dists[0][n - 1]) ok = true;
    if (dists[n - 1][v] + dists[u][0] + cost < dists[n - 1][0]) ok = true;
    if (ok) {
      VI cur = {u, v, cost, swip};
      // debug(cur);
      // goodedgs.PB(cur);
      adj[v].PB(MP(u, MP(cost, swip)));
      int d1 = dijk(0, cur)[n - 1];
      int d2 = dijk(n - 1, cur)[0];
      int nw = d1 + d2 + swip;
      // debug(nw, d1, d2);
      // debug(dijk(0, cur));
      // trav(x, adj) debug(x);
      ckmin(ret, nw);
      adj[v].pop_back();
    }
  }

  if (ret >= INF) ret = -1;

  cout << ret << '\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...