Submission #705699

# Submission time Handle Problem Language Result Execution time Memory
705699 2023-03-05T01:39:06 Z cig32 Olympic Bus (JOI20_ho_t4) C++17
0 / 100
761 ms 10008 KB
#include <bits/stdc++.h>
#define int long long
using namespace std;
struct edge {
  int u, v, c, d;
};
int n, m;
int dist1[232][232];
int distn[232][232];
int rev1[232][232];
int revn[232][232];
int refer;
bool cmp5(pair<pair<int,int>,int> x, pair<pair<int,int>,int> y) {
  return revn[refer][x.first.first] + x.first.second < revn[refer][y.first.first] + y.first.second;
}
bool cmp6(pair<pair<int,int>,int> x, pair<pair<int,int>,int> y) {
  return rev1[refer][x.first.first] + x.first.second < rev1[refer][y.first.first] + y.first.second;
}

mt19937_64 rng((int) std::chrono::steady_clock::now().time_since_epoch().count());
int rnd(int x, int y) {
  return uniform_int_distribution<int>(x, y)(rng);
}

int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  edge e[m+1];
  for(int i=1; i<=m; i++) {
    cin >> e[i].u >> e[i].v >> e[i].c >> e[i].d;
  }
  for(int i=0; i<=n; i++) {
    for(int j=0; j<=n; j++) {
      dist1[i][j] = distn[i][j] = rev1[i][j] = revn[i][j] = 1e9 + 4e8;
    }
    dist1[i][1] = 0;
    distn[i][n] = 0;
    rev1[i][1] = 0;
    revn[i][n] = 0;
  }
  for(int b=0; b<=n; b++) {
    vector<pair<int, int> > adj[n+1], rev[n+1];
    bool vis[n+1];
    for(int i=1; i<=m; i++) {
      if(e[i].u != b && e[i].v != b) {
        adj[e[i].u].push_back({e[i].v, e[i].c});
        rev[e[i].v].push_back({e[i].u, e[i].c});
      }
    }
    priority_queue<pair<int,int>, vector<pair<int,int> >, greater<pair<int,int> > > pq;
    for(int i=1; i<=n; i++) pq.push({dist1[b][i], i});
    for(int i=1; i<=n; i++) vis[i] = 0;
    while(pq.size()) {
      pair<int,int> t = pq.top(); pq.pop();
      if(!vis[t.second]) {
        for(pair<int, int> x: adj[t.second]) {
          if(!vis[x.first] && dist1[b][x.first] > dist1[b][t.second]+x.second) {
            dist1[b][x.first] = dist1[b][t.second]+x.second;
            pq.push({dist1[b][x.first], x.first});
          }
        }
      }
    }
    for(int i=1; i<=n; i++) pq.push({distn[b][i], i});
    for(int i=1; i<=n; i++) vis[i] = 0;
    while(pq.size()) {
      pair<int,int> t = pq.top(); pq.pop();
      if(!vis[t.second]) {
        for(pair<int, int> x: adj[t.second]) {
          if(!vis[x.first] && distn[b][x.first] > distn[b][t.second]+x.second) {
            distn[b][x.first] = distn[b][t.second]+x.second;
            pq.push({distn[b][x.first], x.first});
          }
        }
      }
    }
    for(int i=1; i<=n; i++) pq.push({rev1[b][i], i});
    for(int i=1; i<=n; i++) vis[i] = 0;
    while(pq.size()) {
      pair<int,int> t = pq.top(); pq.pop();
      if(!vis[t.second]) {
        for(pair<int, int> x: rev[t.second]) {
          if(!vis[x.first] && rev1[b][x.first] > rev1[b][t.second]+x.second) {
            rev1[b][x.first] = rev1[b][t.second]+x.second;
            pq.push({rev1[b][x.first], x.first});
          }
        }
      }
    }
    for(int i=1; i<=n; i++) pq.push({revn[b][i], i});
    for(int i=1; i<=n; i++) vis[i] = 0;
    while(pq.size()) {
      pair<int,int> t = pq.top(); pq.pop();
      if(!vis[t.second]) {
        for(pair<int, int> x: rev[t.second]) {
          if(!vis[x.first] && revn[b][x.first] > revn[b][t.second]+x.second) {
            revn[b][x.first] = revn[b][t.second]+x.second;
            pq.push({revn[b][x.first], x.first});
          }
        }
      }
    }
  }
  int ans = dist1[0][n] + distn[0][1];
  vector<pair<int,int> > adj[n+1], rev[n+1];
  vector<int> adj_id[n+1];
  for(int i=1; i<=m; i++) {
    adj_id[e[i].u].push_back(i);
    adj[e[i].u].push_back({e[i].v, e[i].c});
    rev[e[i].v].push_back({e[i].u, e[i].c});
  }
  int cnt[n+1][n+1];
  for(int i=0; i<=n; i++) {
    for(int j=0; j<=n; j++) {
      cnt[i][j] = 0;
    }
  }
  for(int i=1; i<=m; i++) {
    cnt[e[i].u][e[i].v]++;
  }
  int opt1[n+1], opt2[n+1], opt3[n+1], opt4[n+1];
  for(int i=1; i<=n; i++) {
    opt1[i] = opt2[i] = opt3[i] = opt4[i] = 1e9 + 4e8; 
    for(pair<int, int> x: adj[i]) {
      opt1[i] = min(opt1[i], x.second + revn[i][x.first]);
      opt3[i] = min(opt3[i], x.second + rev1[i][x.first]);
    }
    for(pair<int, int> x: rev[i]) {
      opt2[i] = min(opt2[i], dist1[i][x.first] + x.second);
      opt4[i] = min(opt4[i], distn[i][x.first] + x.second);
    }
  }
  vector<pair<pair<int,int>, int> > opt5[n+1], opt6[n+1];
  for(int i=1; i<=n; i++) {
    for(int j=0; j<adj[i].size(); j++) {
      opt5[i].push_back({adj[i][j], adj_id[i][j]});
    }
    refer = i;
    sort(opt5[i].begin(), opt5[i].end(), cmp5);
    for(int j=0; j<adj[i].size(); j++) {
      opt6[i].push_back({adj[i][j], adj_id[i][j]});
    }
    sort(opt6[i].begin(), opt6[i].end(), cmp6);
  }
  for(int i=1; i<=m; i++) {
    int forward, backward;
    forward = dist1[e[i].u][e[i].v] + e[i].c;
    int mi = (e[i].u == n ? 0 : 1e9 + 4e8);
    mi = min(mi, opt1[e[i].u]);
    forward += mi;
    forward = min(forward, dist1[e[i].u][n]);
    
    int pt1 = (e[i].u == 1 ? 0 : 1e9 + 4e8);
    int pt2 = (e[i].u == n ? 0 : 1e9 + 4e8);
    pt1 = min(pt1, opt2[e[i].u]);
    for(pair<pair<int,int>, int> x: opt5[e[i].u]) {
      if(x.second == i) {
        continue;
      }
      pt2 = min(pt2, revn[e[i].u][x.first.first] + x.first.second);
      break;
    }
    forward = min(forward, pt1 + pt2);
    backward = distn[e[i].u][e[i].v] + e[i].c;
    mi = (e[i].u == 1 ? 0 : 1e9 + 4e8);
    mi = min(mi, opt3[e[i].u]);
    backward += mi;
    backward = min(backward, distn[e[i].u][1]);
    pt1 = (e[i].u == n ? 0 : 1e9 + 4e8);
    pt2 = (e[i].u == 1 ? 0 : 1e9 + 4e8);
    pt1 = min(pt1, opt4[e[i].u]);
    for(pair<pair<int,int>, int> x: opt6[e[i].u]) {
      if(x.second == i) {
        continue;
      }
      pt2 = min(pt2, rev1[e[i].u][x.first.first] + x.first.second);
      break;
    }
    backward = min(backward, pt1 + pt2);
    ans = min(ans, forward + backward + e[i].d);
    //cout << "flip edge " << i << ": " << forward << " + " << backward << " + " << e[i].d << " = " << forward + backward + e[i].d << "\n";
  }
  cout << (ans <= 1e12 ? ans : -1) << "\n";
}
/*
5 6
1 2 1 10000
2 5 1 10000
5 3 1 10000
2 3 10 1
2 4 1 10000
4 1 1 10000

2 2
1 2 3 4
1 2 3 9

3 4
1 2 3 4
2 3 3 9
2 1 3 10
2 3 3 11

2 7
1 2 1 1
1 2 1 7
1 2 1 3
1 2 1 4
1 2 1 5
1 2 1 6
1 2 1 9
*/

Compilation message

ho_t4.cpp: In function 'int32_t main()':
ho_t4.cpp:137:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  137 |     for(int j=0; j<adj[i].size(); j++) {
      |                  ~^~~~~~~~~~~~~~
ho_t4.cpp:142:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  142 |     for(int j=0; j<adj[i].size(); j++) {
      |                  ~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 46 ms 2284 KB Output is correct
2 Incorrect 16 ms 2088 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 761 ms 9996 KB Output is correct
2 Correct 727 ms 9836 KB Output is correct
3 Correct 727 ms 10008 KB Output is correct
4 Correct 60 ms 2360 KB Output is correct
5 Correct 42 ms 2284 KB Output is correct
6 Incorrect 22 ms 2116 KB Output isn't correct
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 49 ms 2164 KB Output is correct
2 Incorrect 18 ms 2092 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 46 ms 2284 KB Output is correct
2 Incorrect 16 ms 2088 KB Output isn't correct
3 Halted 0 ms 0 KB -