1 条题解

  • 0
    @ 2023-9-30 14:07:13
    #include <bits/stdc++.h>
    using namespace std;
    #define DePrint(A...) { fprintf(stderr, A); }
    typedef long long LL;
    const int kMaxN = 2000 + 10;
    const int kMaxV = 300 + 10;
    const LL kInf = 100000000;
    LL floyd[kMaxV][kMaxV];
    int n, m, v_cnt, e_cnt;
    int room[kMaxN][2];
    double p[kMaxN], q[kMaxN], dp[kMaxN][kMaxN][2];
    inline LL Dis(int i, int a, int b) {
      return floyd[room[i - 1][a]][room[i][b]];
    }
    int main() {
      scanf("%d %d %d %d", &n, &m, &v_cnt, &e_cnt);
      for (int i = 1; i <= n; i++)
        scanf("%d", &room[i][0]);
      for (int i = 1; i <= n; i++)
        scanf("%d", &room[i][1]);
      for (int i = 1; i <= n; i++)
        scanf("%lf", &p[i]);
      for (int i = 1; i <= n; i++)
        q[i] = 1 - p[i];
      memset(floyd, 0x3F, sizeof(floyd));
      for (int i = 1; i <= e_cnt; i++) {
        int u, v, w;
        scanf("%d %d %d", &u, &v, &w);
        floyd[u][v] = floyd[v][u] = min(floyd[u][v], (LL) w);
      }
      for (int i = 1; i <= v_cnt; i++)
        floyd[i][i] = 0;
      for (int k = 1; k <= v_cnt; k++)
        for (int i = 1; i <= v_cnt; i++)
          for (int j = 1; j <= v_cnt; j++)
            floyd[i][j] = min(floyd[i][j], floyd[i][k] + floyd[k][j]);
      for (int i = 1; i <= n; i++)
        for (int j = 0; j <= m; j++)
          dp[i][j][0] = dp[i][j][1] = kInf;
      dp[1][0][0] = dp[1][1][1] = 0;
      for (int i = 2; i <= n; i++) {
        dp[i][0][0] = dp[i - 1][0][0] + Dis(i, 0, 0);
        for (int j = 1; j <= min(i, m); j++) {
          dp[i][j][0] = min(dp[i - 1][j][0] + Dis(i, 0, 0),
                            dp[i - 1][j][1] + Dis(i, 0, 0) * q[i - 1]
                                            + Dis(i, 1, 0) * p[i - 1]);
          dp[i][j][1] = min(dp[i - 1][j - 1][0] + Dis(i, 0, 0) * q[i]
                                                + Dis(i, 0, 1) * p[i],
                            dp[i - 1][j - 1][1] + Dis(i, 0, 0) * q[i - 1] * q[i]
                                                + Dis(i, 0, 1) * q[i - 1] * p[i]
                                                + Dis(i, 1, 0) * p[i - 1] * q[i]
                                                + Dis(i, 1, 1) * p[i - 1] * p[i]);
        }
      }
      double ans = kInf;
      for (int i = 0; i <= m; i++) {
        ans = min(ans, dp[n][i][0]);
        ans = min(ans, dp[n][i][1]);
      }
      printf("%.2lf\n", ans);
      return 0;
    }
    
    • 1

    信息

    ID
    144
    时间
    1000ms
    内存
    128MiB
    难度
    9
    标签
    递交数
    8
    已通过
    7
    上传者