답안 #882947

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
882947 2023-12-04T08:25:01 Z MilosMilutinovic Wild Boar (JOI18_wild_boar) C++14
62 / 100
4678 ms 963884 KB
#include <bits/stdc++.h>

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m, t, l;
  cin >> n >> m >> t >> l;
  vector<vector<pair<int, int>>> g(n);
  for (int i = 0; i < m; i++) {
    int x, y, z;
    cin >> x >> y >> z;
    --x; --y;
    g[x].emplace_back(y, z);
    g[y].emplace_back(x, z);
  }
  vector<int> seq(l);
  for (int i = 0; i < l; i++) {
    cin >> seq[i];
    --seq[i];
  }
  {
    int p, x;
    cin >> p >> x;
    --p; --x;
    seq[p] = x;
  }
  vector<int> deg(n);
  for (int i = 0; i < n; i++) {
    deg[i] = (int) g[i].size();
  }
  vector<vector<int>> idx(n, vector<int>(n));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < deg[i]; j++) {
      idx[i][g[i][j].first] = j;
    }
  }
  const long long inf = (long long) 1e18;
  vector<vector<vector<vector<long long>>>> dist(n, vector<vector<vector<long long>>>(n));
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      if (i == j) {
        continue;
      }
      dist[i][j].resize(deg[i]);
      for (int k = 0; k < deg[i]; k++) {
        dist[i][j][k] = vector<long long>(deg[j], inf);
      }
    }
  }
  vector<vector<long long>> d(n);
  for (int i = 0; i < n; i++) {
    d[i].resize(deg[i]);
  }
  vector<int> ia(n), ib(n);
  set<tuple<long long, int, int>> st;
  for (int ver = 0; ver < n; ver++) {
    for (int e = 0; e < deg[ver]; e++) {
      for (int i = 0; i < n; i++) {
        for (int j = 0; j < deg[i]; j++) {
          d[i][j] = inf;
        }
        ia[i] = -1;
        ib[i] = -1;
      }
      int to = g[ver][e].first;
      int w = g[ver][e].second;
      d[to][idx[to][ver]] = w;
      st.clear();
      st.emplace(d[to][idx[to][ver]], to, idx[to][ver]);
      ia[to] = idx[to][ver];
      auto Update = [&](int x, int y, long long z) {
        if (ia[x] == y || ib[x] == y) {
          st.erase(st.find({d[x][y], x, y}));
        }
        d[x][y] = z;
        if (ia[x] == y) {
          return;
        }
        if (ib[x] == y) {
          if (d[x][y] < d[x][ia[x]]) {
            swap(ia[x], ib[x]);
          }
          return;
        }
        if (ia[x] == -1) {
          ia[x] = y;
          st.emplace(d[x][y], x, y);
          return;
        }
        if (ib[x] == -1) {
          if (d[x][y] < d[x][ia[x]]) {
            swap(ia[x], ib[x]);
            ia[x] = y;
          } else {
            ib[x] = y;
          }
          st.emplace(d[x][y], x, y);
          return;
        }
        if (d[x][y] >= d[x][ia[x]] && d[x][y] < d[x][ib[x]]) {
          st.erase(st.find({d[x][ib[x]], x, ib[x]}));
          ib[x] = y;
          st.emplace(d[x][y], x, y);
          return;
        }
        if (d[x][y] < d[x][ia[x]]) {
          st.erase(st.find({d[x][ib[x]], x, ib[x]}));
          swap(ia[x], ib[x]);
          ia[x] = y;
          st.emplace(d[x][y], x, y);
        }
      };
      while (!st.empty()) {
        auto it = st.begin();
        int v = get<1>(*it);
        int lst = get<2>(*it);
        st.erase(it);
        for (int i = 0; i < deg[v]; i++) {
          if (i == lst) {
            continue;
          }
          int to = g[v][i].first;
          int w = g[v][i].second;
          if (d[to][idx[to][v]] > d[v][lst] + w) {
            Update(to, idx[to][v], d[v][lst] + w);
            assert((int) st.size() <= 2 * n);
          }
        }
      }
      for (int i = ver + 1; i < n; i++) {
        if (i == ver) {
          continue;
        }
        for (int j = 0; j < deg[i]; j++) {
          dist[ver][i][e][j] = d[i][j];
        }
      }
    }
  }
  vector<vector<vector<vector<int>>>> good(n, vector<vector<vector<int>>>(n, vector<vector<int>>(2)));
  vector<vector<int>> cc(n, vector<int>(2));
  for (int from = 0; from < n; from++) {
    for (int to = 0; to < n; to++) {
      if (from == to) {
        continue;
      }
      vector<pair<int, int>> order;
      for (int i = 0; i < deg[from]; i++) {
        for (int j = 0; j < deg[to]; j++) {
          order.emplace_back(i, j);
        }
      }
      if (from <= to) {
        sort(order.begin(), order.end(), [&](pair<int, int> a, pair<int, int> b) {
          return dist[from][to][a.first][a.second] < dist[from][to][b.first][b.second];
        });
      } else {
        sort(order.begin(), order.end(), [&](pair<int, int> a, pair<int, int> b) {
          return dist[to][from][a.second][a.first] < dist[to][from][b.second][b.first];
        });
      }
      int total = 0;
      for (auto& p : order) {
        if (cc[p.first][0] >= 2 || cc[p.second][1] >= 2) {
          continue;
        }
        good[from][to][0].push_back(p.first);
        good[from][to][1].push_back(p.second);
        cc[p.first][0] += 1;
        cc[p.second][1] += 1;
        total += 1;
        if (total >= 5) {
          break;
        }
      }
      for (int i = 0; i < deg[from]; i++) {
        cc[i][0] = 0;
      }
      for (int i = 0; i < deg[to]; i++) {
        cc[i][1] = 0;
      }
    }
  }
  vector<vector<int>> f(l);
  for (int i = 0; i < l; i++) {
    if (i > 0) {
      for (int j : good[seq[i - 1]][seq[i]][1]) {
        f[i].push_back(j);
      }
    }
    if (i + 1 < l) {
      for (int j : good[seq[i]][seq[i + 1]][0]) {
        f[i].push_back(j);
      }
    }
    sort(f[i].begin(), f[i].end());
    f[i].erase(unique(f[i].begin(), f[i].end()), f[i].end());
  }
  vector<long long> dp(n, inf);
  for (int i = 0; i < f[0].size(); i++) {
    for (int j = 0; j < f[1].size(); j++) {
      if (seq[0] <= seq[1]) {
        dp[j] = min(dp[j], dist[seq[0]][seq[1]][f[0][i]][f[1][j]]);
      } else {
        dp[j] = min(dp[j], dist[seq[1]][seq[0]][f[1][j]][f[0][i]]);
      }
    }
  }
  vector<long long> new_dp(n);
  vector<long long> val(n);
  for (int p = 2; p < l; p++) {
    for (int i = 0; i < f[p].size(); i++) {
      new_dp[i] = inf;
    }
    for (int i = 0; i < f[p - 1].size(); i++) {
      val[i] = inf;
    }
    {
      long long mn = inf;
      for (int i = 0; i < f[p - 1].size(); i++) {
        val[i] = min(val[i], mn);
        mn = min(mn, dp[i]);
      }
    }
    {
      long long mn = inf;
      for (int i = f[p - 1].size() - 1; i >= 0; i--) {
        val[i] = min(val[i], mn);
        mn = min(mn, dp[i]);
      }
    }
    for (int i = 0; i < f[p - 1].size(); i++) {
      if (val[i] != inf) {
        for (int j = 0; j < f[p].size(); j++) {
          if (seq[p - 1] <= seq[p]) {
            new_dp[j] = min(new_dp[j], val[i] + dist[seq[p - 1]][seq[p]][f[p - 1][i]][f[p][j]]);
          } else {
            new_dp[j] = min(new_dp[j], val[i] + dist[seq[p]][seq[p - 1]][f[p][j]][f[p - 1][i]]);
          }
        }
      }
    }
    for (int i = 0; i < f[p].size(); i++) {
      dp[i] = new_dp[i];
    }
  }
  long long res = inf;
  for (int i = 0; i < f[l - 1].size(); i++) {
    res = min(res, dp[i]);
  }
  cout << (res == inf ? -1 : res) << '\n';
  return 0;
}

Compilation message

wild_boar.cpp: In function 'int main()':
wild_boar.cpp:202:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  202 |   for (int i = 0; i < f[0].size(); i++) {
      |                   ~~^~~~~~~~~~~~~
wild_boar.cpp:203:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  203 |     for (int j = 0; j < f[1].size(); j++) {
      |                     ~~^~~~~~~~~~~~~
wild_boar.cpp:214:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  214 |     for (int i = 0; i < f[p].size(); i++) {
      |                     ~~^~~~~~~~~~~~~
wild_boar.cpp:217:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  217 |     for (int i = 0; i < f[p - 1].size(); i++) {
      |                     ~~^~~~~~~~~~~~~~~~~
wild_boar.cpp:222:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  222 |       for (int i = 0; i < f[p - 1].size(); i++) {
      |                       ~~^~~~~~~~~~~~~~~~~
wild_boar.cpp:234:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  234 |     for (int i = 0; i < f[p - 1].size(); i++) {
      |                     ~~^~~~~~~~~~~~~~~~~
wild_boar.cpp:236:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  236 |         for (int j = 0; j < f[p].size(); j++) {
      |                         ~~^~~~~~~~~~~~~
wild_boar.cpp:245:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  245 |     for (int i = 0; i < f[p].size(); i++) {
      |                     ~~^~~~~~~~~~~~~
wild_boar.cpp:250:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  250 |   for (int i = 0; i < f[l - 1].size(); i++) {
      |                   ~~^~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 456 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 468 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 344 KB Output is correct
15 Correct 1 ms 344 KB Output is correct
16 Correct 1 ms 352 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 0 ms 460 KB Output is correct
22 Correct 1 ms 344 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 456 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 468 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 344 KB Output is correct
15 Correct 1 ms 344 KB Output is correct
16 Correct 1 ms 352 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 0 ms 460 KB Output is correct
22 Correct 1 ms 344 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 2 ms 600 KB Output is correct
27 Correct 37 ms 9812 KB Output is correct
28 Correct 47 ms 9908 KB Output is correct
29 Correct 118 ms 15736 KB Output is correct
30 Correct 96 ms 16076 KB Output is correct
31 Correct 94 ms 15904 KB Output is correct
32 Correct 87 ms 15952 KB Output is correct
33 Correct 107 ms 18324 KB Output is correct
34 Correct 110 ms 18256 KB Output is correct
35 Correct 102 ms 18988 KB Output is correct
36 Correct 113 ms 19012 KB Output is correct
37 Correct 110 ms 18468 KB Output is correct
38 Correct 113 ms 20988 KB Output is correct
39 Correct 135 ms 22712 KB Output is correct
40 Correct 124 ms 21076 KB Output is correct
41 Correct 170 ms 20668 KB Output is correct
42 Correct 109 ms 25168 KB Output is correct
43 Correct 194 ms 24644 KB Output is correct
44 Correct 149 ms 24548 KB Output is correct
45 Correct 129 ms 29728 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 456 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 468 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 344 KB Output is correct
15 Correct 1 ms 344 KB Output is correct
16 Correct 1 ms 352 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 0 ms 460 KB Output is correct
22 Correct 1 ms 344 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 2 ms 600 KB Output is correct
27 Correct 37 ms 9812 KB Output is correct
28 Correct 47 ms 9908 KB Output is correct
29 Correct 118 ms 15736 KB Output is correct
30 Correct 96 ms 16076 KB Output is correct
31 Correct 94 ms 15904 KB Output is correct
32 Correct 87 ms 15952 KB Output is correct
33 Correct 107 ms 18324 KB Output is correct
34 Correct 110 ms 18256 KB Output is correct
35 Correct 102 ms 18988 KB Output is correct
36 Correct 113 ms 19012 KB Output is correct
37 Correct 110 ms 18468 KB Output is correct
38 Correct 113 ms 20988 KB Output is correct
39 Correct 135 ms 22712 KB Output is correct
40 Correct 124 ms 21076 KB Output is correct
41 Correct 170 ms 20668 KB Output is correct
42 Correct 109 ms 25168 KB Output is correct
43 Correct 194 ms 24644 KB Output is correct
44 Correct 149 ms 24548 KB Output is correct
45 Correct 129 ms 29728 KB Output is correct
46 Correct 175 ms 16176 KB Output is correct
47 Correct 2900 ms 172744 KB Output is correct
48 Correct 3099 ms 274032 KB Output is correct
49 Correct 3427 ms 358032 KB Output is correct
50 Correct 3242 ms 358816 KB Output is correct
51 Correct 3313 ms 358704 KB Output is correct
52 Correct 3590 ms 349572 KB Output is correct
53 Correct 3521 ms 349828 KB Output is correct
54 Correct 3865 ms 350196 KB Output is correct
55 Correct 3518 ms 348832 KB Output is correct
56 Correct 3609 ms 395108 KB Output is correct
57 Correct 3919 ms 445616 KB Output is correct
58 Correct 4276 ms 495616 KB Output is correct
59 Correct 4437 ms 550876 KB Output is correct
60 Correct 4272 ms 607728 KB Output is correct
61 Correct 4552 ms 669200 KB Output is correct
62 Correct 4666 ms 735784 KB Output is correct
63 Correct 4678 ms 805432 KB Output is correct
64 Correct 3141 ms 963884 KB Output is correct
65 Correct 3013 ms 963588 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 456 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 468 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 344 KB Output is correct
15 Correct 1 ms 344 KB Output is correct
16 Correct 1 ms 352 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 0 ms 460 KB Output is correct
22 Correct 1 ms 344 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 2 ms 600 KB Output is correct
27 Correct 37 ms 9812 KB Output is correct
28 Correct 47 ms 9908 KB Output is correct
29 Correct 118 ms 15736 KB Output is correct
30 Correct 96 ms 16076 KB Output is correct
31 Correct 94 ms 15904 KB Output is correct
32 Correct 87 ms 15952 KB Output is correct
33 Correct 107 ms 18324 KB Output is correct
34 Correct 110 ms 18256 KB Output is correct
35 Correct 102 ms 18988 KB Output is correct
36 Correct 113 ms 19012 KB Output is correct
37 Correct 110 ms 18468 KB Output is correct
38 Correct 113 ms 20988 KB Output is correct
39 Correct 135 ms 22712 KB Output is correct
40 Correct 124 ms 21076 KB Output is correct
41 Correct 170 ms 20668 KB Output is correct
42 Correct 109 ms 25168 KB Output is correct
43 Correct 194 ms 24644 KB Output is correct
44 Correct 149 ms 24548 KB Output is correct
45 Correct 129 ms 29728 KB Output is correct
46 Correct 175 ms 16176 KB Output is correct
47 Correct 2900 ms 172744 KB Output is correct
48 Correct 3099 ms 274032 KB Output is correct
49 Correct 3427 ms 358032 KB Output is correct
50 Correct 3242 ms 358816 KB Output is correct
51 Correct 3313 ms 358704 KB Output is correct
52 Correct 3590 ms 349572 KB Output is correct
53 Correct 3521 ms 349828 KB Output is correct
54 Correct 3865 ms 350196 KB Output is correct
55 Correct 3518 ms 348832 KB Output is correct
56 Correct 3609 ms 395108 KB Output is correct
57 Correct 3919 ms 445616 KB Output is correct
58 Correct 4276 ms 495616 KB Output is correct
59 Correct 4437 ms 550876 KB Output is correct
60 Correct 4272 ms 607728 KB Output is correct
61 Correct 4552 ms 669200 KB Output is correct
62 Correct 4666 ms 735784 KB Output is correct
63 Correct 4678 ms 805432 KB Output is correct
64 Correct 3141 ms 963884 KB Output is correct
65 Correct 3013 ms 963588 KB Output is correct
66 Incorrect 47 ms 10316 KB Output isn't correct
67 Halted 0 ms 0 KB -