답안 #209012

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
209012 2020-03-13T00:32:47 Z eriksuenderhauf Olympic Bus (JOI20_ho_t4) C++11
100 / 100
59 ms 8236 KB
#include <bits/stdc++.h>
#define pb push_back
#define sz(x) (int)(x).size()
#define trav(x, a) for (const auto& x: a)
#define pii pair<int,int>
#define st first
#define nd second
using namespace std;
typedef long long ll;
const int N = 5e4 + 5;
const ll inf = 0x1f1f1f1f1f1f1f1f;
vector<int> e[2][N], adj[N];
int U[N], V[N], C[N], D[N];
ll dp[2][2][N], dp2[2][205][205];
int n, m, par[2][2][N];
int mrk[2][2][N];
vector<int> path[2][2];

void solve(int s, int t, int i) {
  priority_queue<pii,vector<pii>,greater<pii>> pq;
  pq.push({0, s});
  for (int j = 1; j <= n; j++)
    dp[i][s>1][j] = inf;
  dp[i][s>1][s] = 0;
  while (!pq.empty()) {
    auto c = pq.top(); pq.pop();
    if (dp[i][s>1][c.nd] != c.st)
      continue;
    for (int j: e[i][c.nd]) {
      int v = U[j]^V[j]^c.nd;
      if (dp[i][s>1][v] > c.st + C[j]) {
        dp[i][s>1][v] = c.st + C[j];
        par[i][s>1][v] = j;
        pq.push({dp[i][s>1][v], v});
      }
    }
  }
  if (dp[i][s>1][t] == inf)
    return;
  int u = t;
  while (u != s) {
    int j = par[i][s>1][u];
    mrk[i][s>1][j] = 1;
    path[i][s>1].pb(u);
    u = U[j]^V[j]^u;
  }
  path[i][s>1].pb(u);
  reverse(path[i][s>1].begin(), path[i][s>1].end());
}

ll calc(int j, int i) {
  int mi = n+1;
  for (int l = 0; l+1 < sz(path[0][j]); l++) {
    if (U[i] == path[0][j][l] && path[0][j][l+1] == V[i]) {
      mi = l;
      break;
    }
  }
  ll ret = inf;
  for (int l = 0; l <= mi; l++)
    for (int r = mi+1; r < sz(path[0][j]); r++)
      ret = min(ret, dp[0][j][path[0][j][l]] + dp2[j][path[0][j][l]][path[0][j][r]] + dp[1][j^1][path[0][j][r]]);
  return ret;
}

int main() {
  memset(dp, 0x1f, sizeof dp);
  memset(dp2, 0x1f, sizeof dp2);
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= m; i++) {
    scanf("%d %d %d %d", U+i, V+i, C+i, D+i);
    e[0][U[i]].pb(i);
    e[1][V[i]].pb(i);
  }
  for (int i = 0; i < 2; i++) {
    solve(1, n, i);
    solve(n, 1, i);
  }
  for (int i = 1; i <= m; i++)
    for (int j = 0; j < 2; j++)
      if (!mrk[0][j][i])
        dp2[j][U[i]][V[i]] = min(dp2[j][U[i]][V[i]], 1ll*C[i]);
  for (int i = 1; i <= n; i++)
    for (int j = 0; j < 2; j++)
      dp2[j][i][i] = 0;
  for (int t = 0; t < 2; t++)
    for (int k = 1; k <= n; k++)
      for (int i = 1; i <= n; i++)
        for (int j = 1; j <= n; j++)
          dp2[t][i][j] = min(dp2[t][i][j], dp2[t][i][k] + dp2[t][k][j]);
  ll ans = dp[0][0][n] + dp[0][1][1];
  ll x1 = 0, x2 = 0;
  for (int i = 1; i <= m; i++) {
    bool f1 = mrk[0][0][i], f2 = mrk[0][1][i];
    if (f1 && f2)
      ans = min(ans, calc(0, i) + calc(1, i) + D[i]);
    else if (f1)
      ans = min(ans, calc(0,i) + min(dp[0][1][1], dp[0][1][V[i]] + C[i] + dp[1][0][U[i]]) + D[i]);
    else if (f2)
      ans = min(ans, calc(1,i) + min(dp[0][0][n], dp[0][0][V[i]] + C[i] + dp[1][1][U[i]]) + D[i]);
    else
      ans = min(ans, min(dp[0][0][n], dp[0][0][V[i]] + C[i] + dp[1][1][U[i]]) + min(dp[0][1][1], dp[0][1][V[i]] + C[i] + dp[1][0][U[i]]) + D[i]);
  }
  if (ans >= inf)
    ans = -1;
  printf("%I64d\n", ans);
}

Compilation message

ho_t4.cpp: In function 'int main()':
ho_t4.cpp:106:24: warning: format '%d' expects argument of type 'int', but argument 2 has type 'll {aka long long int}' [-Wformat=]
   printf("%I64d\n", ans);
                        ^
ho_t4.cpp:92:6: warning: unused variable 'x1' [-Wunused-variable]
   ll x1 = 0, x2 = 0;
      ^~
ho_t4.cpp:92:14: warning: unused variable 'x2' [-Wunused-variable]
   ll x1 = 0, x2 = 0;
              ^~
ho_t4.cpp:69:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d", &n, &m);
   ~~~~~^~~~~~~~~~~~~~~~~
ho_t4.cpp:71:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d %d %d %d", U+i, V+i, C+i, D+i);
     ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 28 ms 6136 KB Output is correct
2 Correct 29 ms 6264 KB Output is correct
3 Correct 27 ms 6240 KB Output is correct
4 Correct 28 ms 6264 KB Output is correct
5 Correct 9 ms 6136 KB Output is correct
6 Correct 26 ms 6136 KB Output is correct
7 Correct 8 ms 6136 KB Output is correct
8 Correct 8 ms 6136 KB Output is correct
9 Correct 11 ms 6136 KB Output is correct
10 Correct 34 ms 6136 KB Output is correct
11 Correct 31 ms 6136 KB Output is correct
12 Correct 34 ms 6136 KB Output is correct
13 Correct 27 ms 6136 KB Output is correct
14 Correct 27 ms 6136 KB Output is correct
15 Correct 27 ms 6136 KB Output is correct
16 Correct 27 ms 6136 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 53 ms 7544 KB Output is correct
2 Correct 53 ms 7548 KB Output is correct
3 Correct 56 ms 7544 KB Output is correct
4 Correct 29 ms 6136 KB Output is correct
5 Correct 27 ms 6136 KB Output is correct
6 Correct 26 ms 6136 KB Output is correct
7 Correct 27 ms 6136 KB Output is correct
8 Correct 7 ms 6008 KB Output is correct
9 Correct 54 ms 7544 KB Output is correct
10 Correct 54 ms 7548 KB Output is correct
11 Correct 55 ms 7544 KB Output is correct
12 Correct 55 ms 7544 KB Output is correct
13 Correct 54 ms 7544 KB Output is correct
14 Correct 55 ms 7672 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 26 ms 6136 KB Output is correct
2 Correct 28 ms 6136 KB Output is correct
3 Correct 45 ms 7164 KB Output is correct
4 Correct 26 ms 6136 KB Output is correct
5 Correct 51 ms 7544 KB Output is correct
6 Correct 8 ms 6136 KB Output is correct
7 Correct 9 ms 6136 KB Output is correct
8 Correct 52 ms 7544 KB Output is correct
9 Correct 57 ms 7544 KB Output is correct
10 Correct 50 ms 7544 KB Output is correct
11 Correct 51 ms 7544 KB Output is correct
12 Correct 52 ms 7544 KB Output is correct
13 Correct 8 ms 6136 KB Output is correct
14 Correct 8 ms 6136 KB Output is correct
15 Correct 8 ms 6136 KB Output is correct
16 Correct 8 ms 6136 KB Output is correct
17 Correct 8 ms 6136 KB Output is correct
18 Correct 8 ms 6136 KB Output is correct
19 Correct 51 ms 7544 KB Output is correct
20 Correct 51 ms 7544 KB Output is correct
21 Correct 51 ms 7544 KB Output is correct
22 Correct 51 ms 7544 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 28 ms 6136 KB Output is correct
2 Correct 29 ms 6264 KB Output is correct
3 Correct 27 ms 6240 KB Output is correct
4 Correct 28 ms 6264 KB Output is correct
5 Correct 9 ms 6136 KB Output is correct
6 Correct 26 ms 6136 KB Output is correct
7 Correct 8 ms 6136 KB Output is correct
8 Correct 8 ms 6136 KB Output is correct
9 Correct 11 ms 6136 KB Output is correct
10 Correct 34 ms 6136 KB Output is correct
11 Correct 31 ms 6136 KB Output is correct
12 Correct 34 ms 6136 KB Output is correct
13 Correct 27 ms 6136 KB Output is correct
14 Correct 27 ms 6136 KB Output is correct
15 Correct 27 ms 6136 KB Output is correct
16 Correct 27 ms 6136 KB Output is correct
17 Correct 53 ms 7544 KB Output is correct
18 Correct 53 ms 7548 KB Output is correct
19 Correct 56 ms 7544 KB Output is correct
20 Correct 29 ms 6136 KB Output is correct
21 Correct 27 ms 6136 KB Output is correct
22 Correct 26 ms 6136 KB Output is correct
23 Correct 27 ms 6136 KB Output is correct
24 Correct 7 ms 6008 KB Output is correct
25 Correct 54 ms 7544 KB Output is correct
26 Correct 54 ms 7548 KB Output is correct
27 Correct 55 ms 7544 KB Output is correct
28 Correct 55 ms 7544 KB Output is correct
29 Correct 54 ms 7544 KB Output is correct
30 Correct 55 ms 7672 KB Output is correct
31 Correct 26 ms 6136 KB Output is correct
32 Correct 28 ms 6136 KB Output is correct
33 Correct 45 ms 7164 KB Output is correct
34 Correct 26 ms 6136 KB Output is correct
35 Correct 51 ms 7544 KB Output is correct
36 Correct 8 ms 6136 KB Output is correct
37 Correct 9 ms 6136 KB Output is correct
38 Correct 52 ms 7544 KB Output is correct
39 Correct 57 ms 7544 KB Output is correct
40 Correct 50 ms 7544 KB Output is correct
41 Correct 51 ms 7544 KB Output is correct
42 Correct 52 ms 7544 KB Output is correct
43 Correct 8 ms 6136 KB Output is correct
44 Correct 8 ms 6136 KB Output is correct
45 Correct 8 ms 6136 KB Output is correct
46 Correct 8 ms 6136 KB Output is correct
47 Correct 8 ms 6136 KB Output is correct
48 Correct 8 ms 6136 KB Output is correct
49 Correct 51 ms 7544 KB Output is correct
50 Correct 51 ms 7544 KB Output is correct
51 Correct 51 ms 7544 KB Output is correct
52 Correct 51 ms 7544 KB Output is correct
53 Correct 54 ms 7672 KB Output is correct
54 Correct 55 ms 7544 KB Output is correct
55 Correct 55 ms 7544 KB Output is correct
56 Correct 27 ms 6136 KB Output is correct
57 Correct 26 ms 6136 KB Output is correct
58 Correct 53 ms 7928 KB Output is correct
59 Correct 53 ms 7800 KB Output is correct
60 Correct 54 ms 7800 KB Output is correct
61 Correct 51 ms 7800 KB Output is correct
62 Correct 53 ms 7800 KB Output is correct
63 Correct 54 ms 7800 KB Output is correct
64 Correct 58 ms 8236 KB Output is correct
65 Correct 58 ms 8088 KB Output is correct
66 Correct 57 ms 8108 KB Output is correct
67 Correct 28 ms 7320 KB Output is correct
68 Correct 55 ms 7544 KB Output is correct
69 Correct 57 ms 7676 KB Output is correct
70 Correct 55 ms 7548 KB Output is correct
71 Correct 59 ms 7544 KB Output is correct
72 Correct 55 ms 7544 KB Output is correct
73 Correct 54 ms 7544 KB Output is correct
74 Correct 55 ms 7544 KB Output is correct