[DP] [expectations] $ P1850 $ classroom change

[DP] [expected] \ (P1850 \) to change the classroom

link

Title Description

There \ (2n \) lesson arranged on $ n $ time periods. In the \ (I \) ( I \ Leq n-\) \ (. 1 \ Leq ) over a period of time, the same course two contents at the same time in different locations, wherein, beef previously arranged in the classroom \ (C_i \ ) class, while another lesson in the classroom (d_i \) \ carried out.

In the case of non-submission of any application, students need in order all the time period in order to complete the \ (n \) festival scheduled classes. If a student wants to change the first \ (i \) classroom lesson, you need to apply. If the application is passed, students can go to the first $ i $ classroom time periods \ (d_i \) class, or are still in the classroom \ (c_i \) class.

Cow found that application to replace the first \ (i \) when the classroom lesson, the probability is through the application of a known real number \ (K_i \) , and apply for different courses, the probability of being adopted are independent from each other.

All applications submitted only once, and each person can only select up to \ (m \) lesson apply.

University of beef where there \ (v \) classrooms, there \ (e \) path. Each road connecting two classrooms, and can be a two-way traffic. Depending on the length and extent of the congested road, it takes different paths through the physical strength may vary. When the first \ (I \) ( \ (. 1 \ Leq I \-n-Leq. 1 \) ) After class, classroom lessons from beef will start selecting a least-cost physical route to the next lesson classroom.

Taurus now wondering which of these courses so that he can apply for a minimum sum of physical value expectations due to move between classrooms consuming.

Input Format

The first line of four integers \ (n-, m, V, E \) .

The second row \ (n-\) positive integer, the \ (I \) ( \ (. 1 \ Leq I \ n-Leq \) ) positive integer C \ (C_i \) , ie \ (I \) th Taurus is scheduled class time classroom; ensure \ (1 \ Le C_i \ Le v \) .

The third row \ (n-\) positive integer, the \ (I \) ( \ (. 1 \ Leq I \ n-Leq \) ) positive integer \ (D_i \) , i.e. of $ I $ another time period the same classroom courses on one; ensure \ (1 \ Le D_i \ Le v \) .

The fourth row \ (n-\) real numbers, the \ (I \) ( n-\) \ (. 1 \ Leq I \ Leq a real number) \ (K_i \) , i.e. beef application at the \ (I \) th period of time to replace the classroom probability of getting passed. Ensure \ (0 \ Le K_i \ Le 1 \) .

Next, \ (E \) lines of three positive integers \ (a_j, b_j, w_j \) , expressed a bidirectional connection path classroom \ (a_j, b_j \) , this road takes physical value \ (w_j \) ; guaranteed \ (. 1 \ Le a_j, b_j \ Le V \) , \ (. 1 \ Le w_j \ Le 100 \) .

保证\ (1 \ leq n \ leq 2000 \) , \ (0 \ leq m \ leq 2000 \) , \ (1 \ leq v \ leq 300 \) , \ (0 \ leq e \ leq 90000 \) .

Guaranteed by the school on the road, starting from any classroom, you can reach all the other classrooms.

Enter the number of real guarantee of containing up to \ (3 \) decimal places.

Output Format

An output line, comprising a real number, rounded to the nearest decimal exactly \ (2 \) , indicates that the answer. Your output must be exactly the same as the standard output will be right.

Test data to ensure the correct answer and the answer to the difference of the absolute value is not greater than the rounding \ (. 4 \ Times 10 ^ {-}. 3 \) . (If you do not know what a floating point error, these words can be understood as: For most algorithms, you can normally use floating point type instead of its special handling)

Sample

3 2 3 3
2 1 2
1 2 1
0.8 0.2 0.5 
1 2 5
1 3 3
2 3 1
2.80

prompt

  1. There may be more than a two-way road connecting the same two classrooms road. There may also be connected to both ends of the road are the same classroom.
  2. Please note the distinction between \ (n, m, v, e \) meaning, (the n-\) \ is not the number of classrooms, \ (m \) number is not the road.

\(Solution\)

Consider DP. State how set up? Obviously the first few lessons in the state in need. But also to ensure the most change \ (m \) times, so have the number of years in the state. So, how is transferred from a lesson on it? Obviously if we do not know which track a lesson in the classroom, it can not be transferred. Therefore, also in the plus-one-dimensional, whether to apply to change the current record classroom.

Therefore, the state is \ (F [I] [J] [K],. 1 \ Leq I \ Leq n-, 0 \ Leq J \ Leq m, 0 \ Leq K \ Leq. 1 \) , representing the forward \ (I \) lesson, for the \ (j \) times the classroom, the \ (i \) times change or not change expectations.

As for the transfer is relatively simple

f[i][j][0] = min(f[i - 1][j][0] + w[c[i]][c[i - 1]], 
                f[i - 1][j][1] + w[c[i]][d[i - 1]] * k[i - 1] 
                + w[c[i]][c[i - 1]] * (1 - k[i - 1]));
f[i][j][1] = min(f[i - 1][j - 1][0] + w[c[i]][c[i - 1]] * (1 - k[i]) + w[d[i]][c[i - 1]] * k[i],
                f[i - 1][j - 1][1] + w[d[i]][d[i - 1]] * k[i - 1] * k[i] 
                + w[c[i]][c[i - 1]] * (1 - k[i - 1]) * (1 - k[i])
                + w[c[i - 1]][d[i]] * (1 - k[i - 1]) * k[i]
                + w[d[i - 1]][c[i]] * k[i - 1] * (1 - k[i]));

1

As shown in the classroom is not currently options for change

2

Change the current selection is shown in the classroom

This question is there are several points to mind, in preprocessing. Specific look at the code

#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
long long read(){
  long long x = 0; int f = 0; char c = getchar();
  while(c < '0' || c > '9') f |= c == '-', c = getchar();
  while(c >= '0' && c <= '9') x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();
  return f? -x:x;
}

int n, m, V, E, c[2002], d[2002], w[305][305];
double f[2002][2002][2], k[2002], ans;
int main(){
  memset(w, 63, sizeof w);//距离初始化
  n = read(); m = read(); V = read(); E = read();
  for(int i = 1; i <= n; ++i) c[i] = read();
  for(int i = 1; i <= n; ++i) d[i] = read();
  for(int i = 1; i <= n; ++i) scanf("%lf", &k[i]);
  for(int i = 1; i <= E; ++i){
    int x = read(), y = read(), z = read();
    w[x][y] = w[y][x] = min(w[x][y], z);//有可能重边
  }
  for(int l = 1; l <= V; ++l)
    for(int i = 1; i <= V; ++i)
      for(int j = 1; j <= V; ++j)
        w[i][j] = min(w[i][j], w[i][l] + w[l][j]);//floyed最短路
  for(int i = 1; i <= V; ++i) w[i][i] = w[i][0] = w[0][i] = 0;//初始化
  for(int i = 1; i <= n; ++i) f[i][0][0] = f[i - 1][0][0] + w[c[i]][c[i - 1]], f[i][0][1] = 1e9;
    //初始化,处理一个也不申请的情况
  for(int i = 1; i <= n; ++i)
    for(int j = 1; j <= m; ++j){
      f[i][j][0] = min(f[i - 1][j][0] + w[c[i]][c[i - 1]], 
                       f[i - 1][j][1] + w[c[i]][d[i - 1]] * k[i - 1] 
                        + w[c[i]][c[i - 1]] * (1 - k[i - 1]));
      f[i][j][1] = min(f[i - 1][j - 1][0] + w[c[i]][c[i - 1]] * (1 - k[i]) 
                       + w[d[i]][c[i - 1]] * k[i],
                      f[i - 1][j - 1][1] + w[d[i]][d[i - 1]] * k[i - 1] * k[i] 
                      + w[c[i]][c[i - 1]] * (1 - k[i - 1]) * (1 - k[i])
                      + w[c[i - 1]][d[i]] * (1 - k[i - 1]) * k[i]
                      + w[d[i - 1]][c[i]] * k[i - 1] * (1 - k[i]));
    }
  double ans = f[n][0][0];
  for(int i = 1; i <= m; ++i) ans = min(ans, min(f[n][i][1], f[n][i][0]));//更新答案
  printf("%.2lf", ans);
  return 0;
} 

Guess you like

Origin www.cnblogs.com/kylinbalck/p/11258482.html