Change classroom to p1850
Question description
For Niu, who just graduated from college, his first question is how to apply for a suitable course based on the actual situation.
Among the courses that can be selected, \ (2N \) courses are arranged in \ (n \) time periods. In the \ (I (1 \ Leq I \ Leq N) \) time period, two courses with the same content are simultaneously conducted in different locations, niu is pre-arranged in the classroom \ (C_ I \), while the other course is in the classroom \ (d_ I.
If you do not submit any applications, students must complete all the NN courses in sequence of time. If a student wants to change the classroom of the \ (I \) Course, he/she must submit an application. If the application is approved, students can attend classes in the classroom \ (d_ I \) during the \ (I \) period. Otherwise, they will still attend classes in the classroom \ (C_ I.
The application may not pass because there are too many requirements for changing classrooms. After calculation, Niu finds that the probability of applying to change the classroom of section \ (I \) is a known real number \ (K_ I \), in addition, applications for different courses are mutually independent of each other.
According to school regulations, all applications can be submitted only one time before the start of the semester, and each person can only choose at most \ (M \) courses to apply. This means that Niu must decide whether to change the classroom of each course at a time, but cannot decide whether to apply for other courses based on the application results of some courses; niu can apply for the \ (M \) Course he most wishes to change the classroom, or even not to apply for the \ (M \) course.
Because different courses may be arranged in different classrooms, Niu needs to use the course time to rush from one classroom to another.
Niu's university has \ (V \) Classrooms and \ (E \) roads. Each road connects two classrooms and supports two-way access. Because the length and congestion of the road are different, the physical strength consumed by different roads may be different. When the \ (I (1 \ Leq I \ Leq N-1) \) class ends, Niu starts from the classroom of this class, select a path that consumes the least effort to go to the classroom of the next lesson.
Now, Niu wants to know which courses he applies to minimize the expected value of the total amount of physical strength required for moving between classrooms. Please help him find the minimum value.
Input/output format:
The first line contains four integers \ (n, m, V, E \). \ (N \) indicates the number of time periods in this semester; \ (M \) indicates the maximum number of classrooms for which Niu can apply for a change; \ (V \) number of classrooms in niuniu school; \ (E \) indicates the number of roads in niuniu School.
The second line \ (n \) is a positive integer, And the \ (I (1 \ Leq I \ Leq N) \) is a positive integer representing \ (C_ I \), that is, the classroom where Niu is arranged for class in the \ (I \) period; ensure \ (1 \ Leq C_ I \ Leq V \).
The third line \ (n \) is a positive integer, And the \ (I (1 \ Leq I \ Leq N) \) is a positive integer representing \ (d_ I \), that is, the classroom of the same course in the \ (I \) period; guarantee \ (1 \ Leq d_ I \ Leq V \).
Row 4 \ (n \) real numbers, \ (I (1 \ Leq I \ Leq N) \) real numbers represent \ (K_ I \), that is, the probability that the ox application will pass the change of classroom in the \ (I \) period. Guarantee \ (0 \ Leq K_ I \ Leq 1 \).
Next, the \ (E \) line contains three positive integers \ (a_j, B _j, w_j \) in each row, indicating a two-way road connecting classroom \ (a_j, B _j \), the physical strength required to pass this path is \ (w_j \); ensure \ (1 \ Leq a_j, B _j \ Leq V \), \ (1 \ Leq w_j \ Leq 100 \).
\ (1 \ Leq n \ Leq 2000 \), \ (0 \ Leq m \ Leq 2000 \), \ (1 \ Leq V \ Leq 300 \), \ (0 \ Leq e \ Leq 90000 \).
Ensure that all other classrooms are accessible from any classroom on the road in the school.
Make sure that the input real number contains a maximum of \ (3 \) decimal places.
Output Format:
The output row contains a real number, rounded to exactly the \ (2 \) digit after the decimal point, indicating the answer. Your output must be exactly the same as the standard output.
Test data ensures that the absolute value of the difference between the rounded answer and the accurate answer is not greater than \ (4 \ times 10 ^ {-3 }\). (If you do not know what a floating point error is, this section can be understood as: For most algorithms, You can normally use the floating point type without special processing)
Input and Output sample input sample #1:
3 2 3 32 1 21 2 10.8 0.2 0.51 2 51 3 32 3 1
Output sample #1:
2.80
Description
[Example 1]
The following table lists all feasible application plans and expected benefits:
Tip]
There may be multiple two-way roads connecting the same two classrooms. It is also possible that the two sides of the road are connected to the same classroom.
Note the meaning of distinguishing \ (n, m, V, E \). \ (n \) is not the number of classrooms, \ (M \) is not the number of roads.
Special Properties \ (1 \): There is a path containing only one road between any two points \ (a_ I, B _ I, a_ I \ NEQ B _ I \) on the graph.
Special Properties \ (2 \): For all \ (1 \ Leq I \ Leq n \), \ (K_ I = 1 \).
Ideas
It is easy to figure out that the status \ (DP [I] [J] \) indicates the previous \ (I \) lesson and the \ (J \) the physical expectation consumed after the class. But how should we transfer it?
Think about what we need. First, we must know the shortest distance between any two classrooms. This can be done using the \ (Floyd \) algorithm. Second, we need to know that lesson \ (i-1 \) is finally in that classroom so that we can transfer from \ (i-1 \) to \ (I \). Since any course can only have two classrooms, we directly change the status to \ (DP [I] [J] [0/1] \), and the last one-dimensional \ (0/1 \) indicates whether the classroom of the \ (I \) class has been changed.
Now let's consider the transfer equation. If neither of the two classes is changed, add the distance from the last status to the Current status:
dp[i][j][0]=dp[i-1][j][0]+dis[c[i-1]][c[i]];
Of course, it is possible that the previous class has changed to a classroom, so you cannot directly transfer it like this:
dp[i][j][0]=min(dp[i-1][j][0]+dis[c[i-1]][c[i]],dp[i-1][j][1]+dis[c[i-1]][c[i]]*(1-k[i-1])+dis[d[i-1]][c[i]]*k[i-1]);
\ (K \) is the probability of classroom change success. If the change succeeds, go from \ (d [I-1] \) to \ (C [I] \), or else from \ (C [I-1] \) go to \ (C [I] \). Multiply probability to get expectation.
The change in the classroom for this lesson is similar, but it is longer and harder to write:
dp[i][j][1]=min(dp[i-1][j-1][0]+dis[c[i-1]][c[i]]*(1-k[i])+dis[c[i-1]][d[i]]*k[i],dp[i-1][j-1][1]+dis[d[i-1]][d[i]]*k[i-1]*k[i]+dis[c[i-1]][d[i]]*(1-k[i-1])*k[i]+dis[d[i-1]][c[i]]*k[i-1]*(1-k[i])+dis[c[i-1]][c[i]]*(1-k[i-1])*(1-k[i]));
To the end, the minimum value of \ (DP [N] [0 \ SIM m] [0/1] \) is the answer.
AC code
#include<bits/stdc++.h>using namespace std;const int MAXN=2005;int n,m,v,e,c[MAXN],d[MAXN];double ans=DBL_MAX,k[MAXN],dis[305][305],dp[MAXN][MAXN][2];int main(){ scanf("%d%d%d%d",&n,&m,&v,&e); for(int i=1;i<=n;i++) scanf("%d",&c[i]); for(int i=1;i<=n;i++) scanf("%d",&d[i]); for(int i=1;i<=n;i++) scanf("%lf",&k[i]); for(int i=1;i<=v;i++) for(int j=1;j<=v;j++) dis[i][j]=(i==j?0:0x3f3f3f3f); while(e--) { int x,y,z;scanf("%d%d%d",&x,&y,&z); dis[x][y]=dis[y][x]=min(dis[x][y],double(z)); } for(int k=1;k<=v;k++) for(int i=1;i<=v;i++) for(int j=1;j<=v;j++) dis[i][j]=min(dis[i][j],dis[i][k]+dis[k][j]); for(int i=1;i<=n;i++) for(int j=0;j<=m;j++) dp[i][j][0]=dp[i][j][1]=0x3f3f3f3f; 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[c[i-1]][c[i]]; for(int j=1;j<=min(i,m);j++) { dp[i][j][0]=min(dp[i][j][0],min(dp[i-1][j][0]+dis[c[i-1]][c[i]],dp[i-1][j][1]+dis[c[i-1]][c[i]]*(1-k[i-1])+dis[d[i-1]][c[i]]*k[i-1])); dp[i][j][1]=min(dp[i][j][1],min(dp[i-1][j-1][0]+dis[c[i-1]][c[i]]*(1-k[i])+dis[c[i-1]][d[i]]*k[i],dp[i-1][j-1][1]+dis[d[i-1]][d[i]]*k[i-1]*k[i]+dis[c[i-1]][d[i]]*(1-k[i-1])*k[i]+dis[d[i-1]][c[i]]*k[i-1]*(1-k[i])+dis[c[i-1]][c[i]]*(1-k[i-1])*(1-k[i]))); } } for(int i=0;i<=m;i++) ans=min(ans,min(dp[n][i][0],dp[n][i][1])); printf("%.2f",ans); return 0;}
Luogu p1850 for classroom (expectation DP)