Submission #221420

# Submission time Handle Problem Language Result Execution time Memory
221420 2020-04-10T02:59:41 Z rama_pang Wild Boar (JOI18_wild_boar) C++14
62 / 100
15036 ms 409988 KB
#include <bits/stdc++.h>
using namespace std;
using lint = long long;

const int MAXN = 2005;
const int MAXM = 4005;
const int MAXL = 100005;
const lint INF = 1e18;

struct edge_t {
  int u, v, w; // u = from, v = to, w = weight
  edge_t() {}
  edge_t(int u, int v, int w) : u(u), v(v), w(w) {}
};

int N, M, T, L;

namespace ShortestPath {

vector<edge_t> edges;
vector<int> adj[MAXN];

// Distance[X][Y][Use Y?] = Shortest Path from edges[X].u to edges[Y].v given we must use X and we must use/not use Y
lint Distance[MAXM][MAXM][2];
int LastEdgeDistance[MAXM][MAXM][2]; // last edge used in Distance[][][]

// ShortestPathEdge[X][edges[Y].v] = Y that has the shortest distance given that we have to use edge X to reach edges[Y].v
int ShortestPathEdge[MAXM][MAXN]; 

void Read() {
  M = M * 2;
  for (int i = 0; i < (M / 2); i++) {
    int A, B, C;
    cin >> A >> B >> C;
    A--, B--;

    adj[A].emplace_back(edges.size());
    edges.emplace_back(A, B, C);

    adj[B].emplace_back(edges.size());
    edges.emplace_back(B, A, C);
  }
}

void Dijkstra(int X) { // Finds all Distance[X][][1][1]
  priority_queue<pair<lint, int>, vector<pair<lint, int>>, greater<pair<lint, int>>> pq;
  pq.emplace(edges[X].w, X);
  Distance[X][X][1] = edges[X].w; // from edges[X].u to edges[X].v

  // Run Dijkstra to find Distance[X][][1][1].
  // Make sure we do not do a U-turn.

  while (!pq.empty()) {
    int cur = pq.top().second;
    lint cur_dist = pq.top().first; 
    pq.pop();

    if (cur_dist != Distance[X][cur][1]) continue;
    int u = edges[cur].v;

    for (auto nxt : adj[u]) {
      if (cur == (nxt ^ 1)) continue; // We cannot U-turn back
      lint &nxt_dist = Distance[X][nxt][1];

      if (nxt_dist == -1 || nxt_dist > cur_dist + edges[nxt].w) {
        nxt_dist = cur_dist + edges[nxt].w;
        pq.emplace(nxt_dist, nxt);
      }
    }
  }
}

void Solve() { // Computes Distance[][][][] and ShortestPathEdge[][][]
  memset(Distance, -1, sizeof(Distance));
  memset(LastEdgeDistance, -1, sizeof(LastEdgeDistance));
  memset(ShortestPathEdge, -1, sizeof(ShortestPathEdge));

  // Computes Distance[][][1] and ShortestPathEdge[][]
  for (int X = 0; X < M; X++) {
    Dijkstra(X);
    for (int Y = 0; Y < M; Y++) {
      if (Distance[X][Y][1] == -1) continue;
      LastEdgeDistance[X][Y][1] = Y;
      int V = edges[Y].v;
      int &SPE = ShortestPathEdge[X][V];
      if (SPE == -1 || Distance[X][SPE][1] > Distance[X][Y][1]) {
        SPE = Y;
      }
    }
  }

  // Computes Distance[][][0]
  for (int X = 0; X < M; X++) {
    vector<multiset<pair<lint, int>>> VertexDist(N);
    for (int Y = 0; Y < M; Y++) {
      if (Distance[X][Y][1] == -1) continue;
      VertexDist[edges[Y].v].emplace(Distance[X][Y][1], LastEdgeDistance[X][Y][1]);
    }
    for (int Y = 0; Y < M; Y++) {
      if (Distance[X][Y][1] == -1) continue;
      int V = edges[Y].v;
      auto it = VertexDist[V].find({Distance[X][Y][1], LastEdgeDistance[X][Y][1]});
      VertexDist[V].erase(it);
      if (!VertexDist[V].empty()) {
        Distance[X][Y][0] = begin(VertexDist[V])->first;
        LastEdgeDistance[X][Y][0] = begin(VertexDist[V])->second;
      }
      VertexDist[V].emplace(Distance[X][Y][1], LastEdgeDistance[X][Y][1]);
    }
  }
}

}

namespace DynamicProgramming {

int X[MAXL];

void Read() {
  for (int i = 0; i < L; i++) {
    cin >> X[i];
    X[i]--;
  }

  // T = 1
  int P, Q;
  cin >> P >> Q;
  X[--P] = --Q;
}

lint Solve() {
  vector<lint> dp(M, INF), next_dp(M, INF);

  // Special Case for pos = 0
  for (auto i : ShortestPath::adj[X[0]]) {
    for (int j = 0; j < M; j++) {
      if (ShortestPath::edges[j].v == X[1] && ShortestPath::Distance[i][j][1] != -1) {
        dp[j] = min(dp[j], ShortestPath::Distance[i][j][1]);
      }
    }
  }

  for (int pos = 1; pos < L - 1; pos++) {
    for (int last_edge = 0; last_edge < M; last_edge++) {
      if (ShortestPath::edges[last_edge].v != X[pos]) continue;

      int nxt_edge = ShortestPath::ShortestPathEdge[last_edge][X[pos + 1]];
      if (nxt_edge == -1) continue;

      next_dp[nxt_edge] = min(next_dp[nxt_edge], 
        dp[last_edge] + ShortestPath::Distance[last_edge][nxt_edge][1] - ShortestPath::edges[last_edge].w);
      
      int exclude_nxt_edge = ShortestPath::LastEdgeDistance[last_edge][nxt_edge][0];
      if (exclude_nxt_edge == -1) continue;
      next_dp[exclude_nxt_edge] = min(next_dp[exclude_nxt_edge], 
        dp[last_edge] + ShortestPath::Distance[last_edge][exclude_nxt_edge][1] - ShortestPath::edges[last_edge].w);
    }

    dp = next_dp;
    next_dp.assign(M, INF);
  }

  lint res = *min_element(begin(dp), end(dp));
  if (res == INF) res = -1;
  return res;
}

}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);

  cin >> N >> M >> T >> L;

  ShortestPath::Read();
  ShortestPath::Solve();
  
  DynamicProgramming::Read();
  cout << DynamicProgramming::Solve() << "\n";

  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 216 ms 408568 KB Output is correct
2 Correct 208 ms 408440 KB Output is correct
3 Correct 208 ms 408440 KB Output is correct
4 Correct 215 ms 408568 KB Output is correct
5 Correct 218 ms 408444 KB Output is correct
6 Correct 209 ms 408532 KB Output is correct
7 Correct 210 ms 408620 KB Output is correct
8 Correct 208 ms 408440 KB Output is correct
9 Correct 206 ms 408440 KB Output is correct
10 Correct 219 ms 408568 KB Output is correct
11 Correct 209 ms 408440 KB Output is correct
12 Correct 212 ms 408440 KB Output is correct
13 Correct 216 ms 408568 KB Output is correct
14 Correct 205 ms 408444 KB Output is correct
15 Correct 211 ms 408440 KB Output is correct
16 Correct 205 ms 408568 KB Output is correct
17 Correct 210 ms 408440 KB Output is correct
18 Correct 225 ms 408520 KB Output is correct
19 Correct 209 ms 408568 KB Output is correct
20 Correct 207 ms 408440 KB Output is correct
21 Correct 208 ms 408440 KB Output is correct
22 Correct 216 ms 408440 KB Output is correct
23 Correct 208 ms 408440 KB Output is correct
24 Correct 208 ms 408440 KB Output is correct
25 Correct 205 ms 408440 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 216 ms 408568 KB Output is correct
2 Correct 208 ms 408440 KB Output is correct
3 Correct 208 ms 408440 KB Output is correct
4 Correct 215 ms 408568 KB Output is correct
5 Correct 218 ms 408444 KB Output is correct
6 Correct 209 ms 408532 KB Output is correct
7 Correct 210 ms 408620 KB Output is correct
8 Correct 208 ms 408440 KB Output is correct
9 Correct 206 ms 408440 KB Output is correct
10 Correct 219 ms 408568 KB Output is correct
11 Correct 209 ms 408440 KB Output is correct
12 Correct 212 ms 408440 KB Output is correct
13 Correct 216 ms 408568 KB Output is correct
14 Correct 205 ms 408444 KB Output is correct
15 Correct 211 ms 408440 KB Output is correct
16 Correct 205 ms 408568 KB Output is correct
17 Correct 210 ms 408440 KB Output is correct
18 Correct 225 ms 408520 KB Output is correct
19 Correct 209 ms 408568 KB Output is correct
20 Correct 207 ms 408440 KB Output is correct
21 Correct 208 ms 408440 KB Output is correct
22 Correct 216 ms 408440 KB Output is correct
23 Correct 208 ms 408440 KB Output is correct
24 Correct 208 ms 408440 KB Output is correct
25 Correct 205 ms 408440 KB Output is correct
26 Correct 211 ms 408440 KB Output is correct
27 Correct 280 ms 408824 KB Output is correct
28 Correct 270 ms 409080 KB Output is correct
29 Correct 532 ms 408952 KB Output is correct
30 Correct 541 ms 409000 KB Output is correct
31 Correct 619 ms 409080 KB Output is correct
32 Correct 650 ms 408952 KB Output is correct
33 Correct 534 ms 409004 KB Output is correct
34 Correct 512 ms 409080 KB Output is correct
35 Correct 630 ms 408952 KB Output is correct
36 Correct 583 ms 408952 KB Output is correct
37 Correct 533 ms 409080 KB Output is correct
38 Correct 501 ms 408952 KB Output is correct
39 Correct 519 ms 408952 KB Output is correct
40 Correct 509 ms 409080 KB Output is correct
41 Correct 500 ms 408952 KB Output is correct
42 Correct 549 ms 408952 KB Output is correct
43 Correct 490 ms 408952 KB Output is correct
44 Correct 480 ms 409080 KB Output is correct
45 Correct 441 ms 409080 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 216 ms 408568 KB Output is correct
2 Correct 208 ms 408440 KB Output is correct
3 Correct 208 ms 408440 KB Output is correct
4 Correct 215 ms 408568 KB Output is correct
5 Correct 218 ms 408444 KB Output is correct
6 Correct 209 ms 408532 KB Output is correct
7 Correct 210 ms 408620 KB Output is correct
8 Correct 208 ms 408440 KB Output is correct
9 Correct 206 ms 408440 KB Output is correct
10 Correct 219 ms 408568 KB Output is correct
11 Correct 209 ms 408440 KB Output is correct
12 Correct 212 ms 408440 KB Output is correct
13 Correct 216 ms 408568 KB Output is correct
14 Correct 205 ms 408444 KB Output is correct
15 Correct 211 ms 408440 KB Output is correct
16 Correct 205 ms 408568 KB Output is correct
17 Correct 210 ms 408440 KB Output is correct
18 Correct 225 ms 408520 KB Output is correct
19 Correct 209 ms 408568 KB Output is correct
20 Correct 207 ms 408440 KB Output is correct
21 Correct 208 ms 408440 KB Output is correct
22 Correct 216 ms 408440 KB Output is correct
23 Correct 208 ms 408440 KB Output is correct
24 Correct 208 ms 408440 KB Output is correct
25 Correct 205 ms 408440 KB Output is correct
26 Correct 211 ms 408440 KB Output is correct
27 Correct 280 ms 408824 KB Output is correct
28 Correct 270 ms 409080 KB Output is correct
29 Correct 532 ms 408952 KB Output is correct
30 Correct 541 ms 409000 KB Output is correct
31 Correct 619 ms 409080 KB Output is correct
32 Correct 650 ms 408952 KB Output is correct
33 Correct 534 ms 409004 KB Output is correct
34 Correct 512 ms 409080 KB Output is correct
35 Correct 630 ms 408952 KB Output is correct
36 Correct 583 ms 408952 KB Output is correct
37 Correct 533 ms 409080 KB Output is correct
38 Correct 501 ms 408952 KB Output is correct
39 Correct 519 ms 408952 KB Output is correct
40 Correct 509 ms 409080 KB Output is correct
41 Correct 500 ms 408952 KB Output is correct
42 Correct 549 ms 408952 KB Output is correct
43 Correct 490 ms 408952 KB Output is correct
44 Correct 480 ms 409080 KB Output is correct
45 Correct 441 ms 409080 KB Output is correct
46 Correct 577 ms 408568 KB Output is correct
47 Correct 15036 ms 409296 KB Output is correct
48 Correct 12744 ms 409472 KB Output is correct
49 Correct 10159 ms 409592 KB Output is correct
50 Correct 11032 ms 409572 KB Output is correct
51 Correct 12492 ms 409576 KB Output is correct
52 Correct 6714 ms 409796 KB Output is correct
53 Correct 6727 ms 409820 KB Output is correct
54 Correct 6757 ms 409668 KB Output is correct
55 Correct 6728 ms 409796 KB Output is correct
56 Correct 6633 ms 409756 KB Output is correct
57 Correct 6489 ms 409712 KB Output is correct
58 Correct 6312 ms 409720 KB Output is correct
59 Correct 6509 ms 409628 KB Output is correct
60 Correct 6309 ms 409944 KB Output is correct
61 Correct 6164 ms 409800 KB Output is correct
62 Correct 5963 ms 409988 KB Output is correct
63 Correct 5728 ms 409908 KB Output is correct
64 Correct 2689 ms 409772 KB Output is correct
65 Correct 2747 ms 409752 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 216 ms 408568 KB Output is correct
2 Correct 208 ms 408440 KB Output is correct
3 Correct 208 ms 408440 KB Output is correct
4 Correct 215 ms 408568 KB Output is correct
5 Correct 218 ms 408444 KB Output is correct
6 Correct 209 ms 408532 KB Output is correct
7 Correct 210 ms 408620 KB Output is correct
8 Correct 208 ms 408440 KB Output is correct
9 Correct 206 ms 408440 KB Output is correct
10 Correct 219 ms 408568 KB Output is correct
11 Correct 209 ms 408440 KB Output is correct
12 Correct 212 ms 408440 KB Output is correct
13 Correct 216 ms 408568 KB Output is correct
14 Correct 205 ms 408444 KB Output is correct
15 Correct 211 ms 408440 KB Output is correct
16 Correct 205 ms 408568 KB Output is correct
17 Correct 210 ms 408440 KB Output is correct
18 Correct 225 ms 408520 KB Output is correct
19 Correct 209 ms 408568 KB Output is correct
20 Correct 207 ms 408440 KB Output is correct
21 Correct 208 ms 408440 KB Output is correct
22 Correct 216 ms 408440 KB Output is correct
23 Correct 208 ms 408440 KB Output is correct
24 Correct 208 ms 408440 KB Output is correct
25 Correct 205 ms 408440 KB Output is correct
26 Correct 211 ms 408440 KB Output is correct
27 Correct 280 ms 408824 KB Output is correct
28 Correct 270 ms 409080 KB Output is correct
29 Correct 532 ms 408952 KB Output is correct
30 Correct 541 ms 409000 KB Output is correct
31 Correct 619 ms 409080 KB Output is correct
32 Correct 650 ms 408952 KB Output is correct
33 Correct 534 ms 409004 KB Output is correct
34 Correct 512 ms 409080 KB Output is correct
35 Correct 630 ms 408952 KB Output is correct
36 Correct 583 ms 408952 KB Output is correct
37 Correct 533 ms 409080 KB Output is correct
38 Correct 501 ms 408952 KB Output is correct
39 Correct 519 ms 408952 KB Output is correct
40 Correct 509 ms 409080 KB Output is correct
41 Correct 500 ms 408952 KB Output is correct
42 Correct 549 ms 408952 KB Output is correct
43 Correct 490 ms 408952 KB Output is correct
44 Correct 480 ms 409080 KB Output is correct
45 Correct 441 ms 409080 KB Output is correct
46 Correct 577 ms 408568 KB Output is correct
47 Correct 15036 ms 409296 KB Output is correct
48 Correct 12744 ms 409472 KB Output is correct
49 Correct 10159 ms 409592 KB Output is correct
50 Correct 11032 ms 409572 KB Output is correct
51 Correct 12492 ms 409576 KB Output is correct
52 Correct 6714 ms 409796 KB Output is correct
53 Correct 6727 ms 409820 KB Output is correct
54 Correct 6757 ms 409668 KB Output is correct
55 Correct 6728 ms 409796 KB Output is correct
56 Correct 6633 ms 409756 KB Output is correct
57 Correct 6489 ms 409712 KB Output is correct
58 Correct 6312 ms 409720 KB Output is correct
59 Correct 6509 ms 409628 KB Output is correct
60 Correct 6309 ms 409944 KB Output is correct
61 Correct 6164 ms 409800 KB Output is correct
62 Correct 5963 ms 409988 KB Output is correct
63 Correct 5728 ms 409908 KB Output is correct
64 Correct 2689 ms 409772 KB Output is correct
65 Correct 2747 ms 409752 KB Output is correct
66 Incorrect 245 ms 409080 KB Output isn't correct
67 Halted 0 ms 0 KB -