Submission #705700

# Submission time Handle Problem Language Result Execution time Memory
705700 2023-03-05T01:41:36 Z cig32 Olympic Bus (JOI20_ho_t4) C++14
37 / 100
1000 ms 10848 KB
#include <bits/stdc++.h>
#define int long long
using namespace std;
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;
}
int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  pair<pair<int,int>,pair<int,int> > e[m+1];
  for(int i=1; i<=m; i++) {
    cin >> e[i].first.first >> e[i].first.second >> e[i].second.first >> e[i].second.second;
  }
  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] = 1e18;
    }
    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].first.first != b && e[i].first.second != b) {
        adj[e[i].first.first].push_back({e[i].first.second, e[i].second.first});
        rev[e[i].first.second].push_back({e[i].first.first, e[i].second.first});
      }
    }
    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].first.first].push_back(i);
    adj[e[i].first.first].push_back({e[i].first.second, e[i].second.first});
    rev[e[i].first.second].push_back({e[i].first.first, e[i].second.first});
  }
  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].first.first][e[i].first.second]++;
  }
  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] = 1e18;
    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].first.first][e[i].first.second] + e[i].second.first;
    int mi = (e[i].first.first == n ? 0 : 1e18);
    mi = min(mi, opt1[e[i].first.first]);
    forward += mi;
    forward = min(forward, dist1[e[i].first.first][n]);
    
    int pt1 = (e[i].first.first == 1 ? 0 : 1e18);
    int pt2 = (e[i].first.first == n ? 0 : 1e18);
    pt1 = min(pt1, opt2[e[i].first.first]);
    for(pair<pair<int,int>, int> x: opt5[e[i].first.first]) {
      if(x.second == i) {
        continue;
      }
      pt2 = min(pt2, revn[e[i].first.first][x.first.first] + x.first.second);
      break;
    }
    forward = min(forward, pt1 + pt2);
    backward = distn[e[i].first.first][e[i].first.second] + e[i].second.first;
    mi = (e[i].first.first == 1 ? 0 : 1e18);
    mi = min(mi, opt3[e[i].first.first]);
    backward += mi;
    backward = min(backward, distn[e[i].first.first][1]);
    pt1 = (e[i].first.first == n ? 0 : 1e18);
    pt2 = (e[i].first.first == 1 ? 0 : 1e18);
    pt1 = min(pt1, opt4[e[i].first.first]);
    for(pair<pair<int,int>, int> x: opt6[e[i].first.first]) {
      if(x.second == i) {
        continue;
      }
      pt2 = min(pt2, rev1[e[i].first.first][x.first.first] + x.first.second);
      break;
    }
    backward = min(backward, pt1 + pt2);
    ans = min(ans, forward + backward + e[i].second.second);
    //cout << "flip pair<pair<int,int>,pair<int,int> > " << i << ": " << forward << " + " << backward << " + " << e[i].second.second << " = " << forward + backward + e[i].second.second << "\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:128: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]
  128 |     for(int j=0; j<adj[i].size(); j++) {
      |                  ~^~~~~~~~~~~~~~
ho_t4.cpp:133: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]
  133 |     for(int j=0; j<adj[i].size(); j++) {
      |                  ~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 45 ms 2252 KB Output is correct
2 Correct 15 ms 2092 KB Output is correct
3 Correct 52 ms 2172 KB Output is correct
4 Correct 53 ms 2168 KB Output is correct
5 Correct 4 ms 724 KB Output is correct
6 Correct 16 ms 2132 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 1 ms 484 KB Output is correct
10 Correct 62 ms 2284 KB Output is correct
11 Correct 62 ms 2280 KB Output is correct
12 Correct 61 ms 2284 KB Output is correct
13 Correct 36 ms 2280 KB Output is correct
14 Correct 47 ms 2168 KB Output is correct
15 Correct 45 ms 2280 KB Output is correct
16 Correct 47 ms 2284 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 747 ms 9856 KB Output is correct
2 Correct 716 ms 9744 KB Output is correct
3 Correct 730 ms 9992 KB Output is correct
4 Correct 60 ms 2380 KB Output is correct
5 Correct 39 ms 2276 KB Output is correct
6 Correct 22 ms 2100 KB Output is correct
7 Correct 15 ms 2108 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 543 ms 10592 KB Output is correct
10 Correct 534 ms 10512 KB Output is correct
11 Correct 659 ms 9920 KB Output is correct
12 Correct 654 ms 9816 KB Output is correct
13 Correct 684 ms 9828 KB Output is correct
14 Correct 668 ms 10848 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 46 ms 2168 KB Output is correct
2 Correct 20 ms 2088 KB Output is correct
3 Correct 407 ms 7892 KB Output is correct
4 Correct 15 ms 2088 KB Output is correct
5 Correct 515 ms 9608 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 488 ms 10500 KB Output is correct
9 Correct 481 ms 10288 KB Output is correct
10 Correct 503 ms 9924 KB Output is correct
11 Correct 498 ms 9936 KB Output is correct
12 Correct 509 ms 10204 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 0 ms 340 KB Output is correct
19 Correct 532 ms 10480 KB Output is correct
20 Correct 501 ms 10092 KB Output is correct
21 Correct 496 ms 9724 KB Output is correct
22 Correct 502 ms 9880 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 45 ms 2252 KB Output is correct
2 Correct 15 ms 2092 KB Output is correct
3 Correct 52 ms 2172 KB Output is correct
4 Correct 53 ms 2168 KB Output is correct
5 Correct 4 ms 724 KB Output is correct
6 Correct 16 ms 2132 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 1 ms 484 KB Output is correct
10 Correct 62 ms 2284 KB Output is correct
11 Correct 62 ms 2280 KB Output is correct
12 Correct 61 ms 2284 KB Output is correct
13 Correct 36 ms 2280 KB Output is correct
14 Correct 47 ms 2168 KB Output is correct
15 Correct 45 ms 2280 KB Output is correct
16 Correct 47 ms 2284 KB Output is correct
17 Correct 747 ms 9856 KB Output is correct
18 Correct 716 ms 9744 KB Output is correct
19 Correct 730 ms 9992 KB Output is correct
20 Correct 60 ms 2380 KB Output is correct
21 Correct 39 ms 2276 KB Output is correct
22 Correct 22 ms 2100 KB Output is correct
23 Correct 15 ms 2108 KB Output is correct
24 Correct 0 ms 340 KB Output is correct
25 Correct 543 ms 10592 KB Output is correct
26 Correct 534 ms 10512 KB Output is correct
27 Correct 659 ms 9920 KB Output is correct
28 Correct 654 ms 9816 KB Output is correct
29 Correct 684 ms 9828 KB Output is correct
30 Correct 668 ms 10848 KB Output is correct
31 Correct 46 ms 2168 KB Output is correct
32 Correct 20 ms 2088 KB Output is correct
33 Correct 407 ms 7892 KB Output is correct
34 Correct 15 ms 2088 KB Output is correct
35 Correct 515 ms 9608 KB Output is correct
36 Correct 0 ms 340 KB Output is correct
37 Correct 0 ms 340 KB Output is correct
38 Correct 488 ms 10500 KB Output is correct
39 Correct 481 ms 10288 KB Output is correct
40 Correct 503 ms 9924 KB Output is correct
41 Correct 498 ms 9936 KB Output is correct
42 Correct 509 ms 10204 KB Output is correct
43 Correct 0 ms 340 KB Output is correct
44 Correct 1 ms 340 KB Output is correct
45 Correct 0 ms 340 KB Output is correct
46 Correct 0 ms 340 KB Output is correct
47 Correct 0 ms 340 KB Output is correct
48 Correct 0 ms 340 KB Output is correct
49 Correct 532 ms 10480 KB Output is correct
50 Correct 501 ms 10092 KB Output is correct
51 Correct 496 ms 9724 KB Output is correct
52 Correct 502 ms 9880 KB Output is correct
53 Correct 771 ms 9552 KB Output is correct
54 Correct 769 ms 9932 KB Output is correct
55 Correct 787 ms 9892 KB Output is correct
56 Correct 53 ms 2252 KB Output is correct
57 Correct 55 ms 2252 KB Output is correct
58 Correct 619 ms 8016 KB Output is correct
59 Correct 662 ms 8272 KB Output is correct
60 Correct 622 ms 8076 KB Output is correct
61 Correct 590 ms 8080 KB Output is correct
62 Correct 649 ms 7992 KB Output is correct
63 Correct 619 ms 8116 KB Output is correct
64 Execution timed out 1081 ms 5640 KB Time limit exceeded
65 Halted 0 ms 0 KB -