[NOIP2016] In other classrooms solution to a problem

Topic background:

For just college Taurus, the first question he faced was how to apply the right course according to the actual situation.
  May be selected in the curriculum, there are 2n curriculum section on the n time periods. At the i (1 <= i <= n) time period, the same course at the same time the content of two different locations, which are arranged in advance beef ci school classroom, while the other section in a classroom courses di .
  In the case of non-submission of any application, students need to complete all the scheduled classes n sections in the order of time in turn. If a student wants to change the i-classroom lesson, you need to apply. If the application is passed, students can go to the classroom in the i-th period class di, ci otherwise still in the classroom.
  Because too many classroom needs replacement, application may not be able to get through. By calculating, beef find application to replace the classroom courses section i, the probability of the application is known by a real number Ki, and for various application programs, through the probability of being independent of each other.
  School regulations, all applications can only be submitted one time before the beginning of the semester, and each person can only select up to m lesson to apply. This means that the beef must decide whether to apply for a one-time replacement of the classroom each lesson, not to decide whether to apply for other courses on the application of the results of certain programs; beef can apply their best hope to replace classroom m course, you can not run out this is the opportunity of m application, you can not even apply for a course.
  Because different courses may be arranged in different classrooms, so the need to use the time between classes beef arrived from one classroom to another classroom.
  V beef University where there are classrooms, there are e road. 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 <= i <= n-1) the end of class, classroom lessons from beef will start, select a path with the least cost to the physical classroom next class.
  Taurus now wondering which of these courses can apply for his physical because of the expected value of the sum of the value of the minimum cost mobile classrooms, ask you to help him find the minimum value.

Input formats:

The first line of four integers n, m, v, e. n represents the number of periods in the semester; m represents how much beef up the classroom section of the course to request a replacement; v represents the number of beef school classroom; e represents the number of schools where Taurus road.
The second row of n positive integer, the i (1 <= i <= n) CI positive integer, i.e., the i-th period is arranged beef this classroom; guaranteed 1 <= ci <= v.
The third row of n positive integer, the i (1 <= i <= n) DI positive integer, i.e., the i-th period other between the same classroom courses; guaranteed 1 = <di <= v.
The fourth row of n real numbers, the i (1 <= i <= n) represents a real number Ki, i.e. the probability of obtaining beef classroom application by replacing the i-th period. To ensure that 0 <= ki <= 1.
Next e lines of three positive integers aj, bj, wj, there is a two-way road link represents a classroom aj, bj, this road takes physical value of Wj; guaranteed 1 <= aj, bj <= v , 1 <= wj <= 100 .
To ensure that 1 <= n <= 2000,0 < = m <= 2000,1 <= v <= 300,0 <= e <= 90000.
Guaranteed by the school on the road, starting from any classroom, you can reach all the other classrooms.

Ensure that the input comprises a real number up to three decimal places.

answer:

  We naturally think first with floyd shortest path algorithm any two points is obtained, then think of setting a state f [i] [j] represents a change of course came up i j times the minimum physical exertion classroom expectations, but you can not think so the state transition equation, since f [i] [j] need to rely on the classroom first i-1 course is located, that is, the last time whether to choose change the classroom to launch, so we thought of setting f [i] [j] [ 0] i-1 represents the time change is not selected classrooms, f [i] [j] [1] represents the first i-1 times to select the desired transducer classroom minimum desired exhaustion. Then the state transition equation then is:
  F [I] [J] [0] = min (F [I] [J] [0], min (F [I-. 1] [J] [0] + DIS [C [ i-1]] [c [ i]], f [i-1] [j] [1] + dis [c [i-1]] [c [i]] * (1-k [i-1] ) + DIS [D [I-. 1]] [C [I]] * K [I-. 1]));
  F [I] [J] [. 1] = min (F [I] [J] [. 1] , min (f [i-1 ] [j-1] [0] + dis [c [i-1]] [d [i]] * k [i] + dis [c [i-1]] [c [i]] * (1- k [i]), f [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])));
DIS array represents the shortest distance between two points, related dis * k denotes a desired value exhaustion.
Attach Code:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const double sup=1e17 + 5;const int N=2e3 + 5;
 4 int n,m,v,e,cnt,c[N],d[N],dis[305][305];
 5 double k[N],f[N][N][2],ans;
 6  
 7 int main(){
 8     memset(dis,63,sizeof(dis));
 9     scanf("%d %d %d %d",&n,&m,&v,&e);
10     for(int i=1;i<=n;++i) scanf("%d",&c[i]);
11     for(int i=1;i<=n;++i) scanf("%d",&d[i]);
12     for(int i=1;i<=n;++i) scanf("%lf",&k[i]);
13     for(int i=1;i<=e;++i){
14         int x,y,z;
15         scanf("%d %d %d",&x,&y,&z);
16         dis[x][y]=dis[y][x]=min(dis[x][y],z);
17     }
18     //floyd求最短距离 
19     for(int k1=1;k1<=v;++k1)
20         for(int i=1;i<=v;++i)
21             for(int j=1;j<=v;++j)
22                 dis[i][j]=min(dis[i][j],dis[i][k1]+dis[k1][j]);
23     //初始化 
24     for(int i=1;i<=v;++i) 
25         dis[i][i]=dis[i][0]=dis[0][i]=0;
26     for(int i=0;i<=n;++i)
27         for(int j=0;j<=m;++j)
28             f[i][j][0]=f[i][j][1]=sup;
29     //dp
30     f[1][0][0]=f[1][1][1]=0;
31     for(int i=2;i<=n;++i){
32         f[i][0][0]=f[i-1][0][0]+dis[c[i-1]][c[i]];
33         for(int j=1;j<=min(i,m);++j){
34             f[i][j][0]=min(f[i][j][0],min(f[i-1][j][0]+dis[c[i-1]][c[i]],f[i-1][j][1]+dis[c[i-1]][c[i]]*(1-k[i-1])+dis[d[i-1]][c[i]]*k[i-1]));
35             f[i][j][1]=min(f[i][j][1],min(f[i-1][j-1][0]+dis[c[i-1]][d[i]]*k[i]+dis[c[i-1]][c[i]]*(1-k[i]),f[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])));
36         }
37     }
38     ans=sup;    
39     for(int i=0;i<=m;++i) ans=min(ans,min(f[n][i][0],f[n][i][1]));
40     printf("%.2f",ans);
41     return 0;
42 }

 

                    

Guess you like

Origin www.cnblogs.com/Asika3912333/p/11333001.html
Recommended