Mobile Service solution to a problem report

Topic Portal

[Title] effect

L-positions (numbered 1 ~ L) and N requirements, respectively, in the initial state of three staff 1,2,3 positions, the position of each required to give a p [i], the position needs to go to a waiter , it is known from position i to position j cost is C [i] [j], find the minimum cost.

[Analysis] ideas

We f [i] [x] [y] [z] denotes the i-th completion of the previous claims, three minimum cost attendant respectively x, y, z position. Then we found that, where a waiter must be in the position required by the i-th, i.e., z = p [i], it can be removed with a one-dimensional array.

Next, look at the transfer equation, i + 1 for the first requirement, there are three cases, i.e., let p [i], where an x, y position to the waiter to the position P [i + 1], whereby can be obtained:

$if(x!=p[i+1]且y!=p[i+1])  f[i+1][x][y]=min(f[i+1][x][y],f[i][x][y]+c[p[i]][p[i+1]])$

$if(p[i]!=p[i+1]且y!=p[i+1])  f[i+1][p[i]][y]=min(f[i+1][p[i]][y],f[i][x][y]+c[x][p[i+1]])$

$if(x!=p[i+1]且p[i]!=p[i+1])  f[i+1][x][p[i]]=min(f[i+1][x][p[i]],f[i][x][y]+c[y][p[i+1]])$

By observing transfer equation we find that every state is only related to its previous state, so you can use the scroll array to prevent the explosion of space, which is detailed in the code.

【Code】

 

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 #define rg register
 5 #define go(i,a,b) for(rg int i=a;i<=b;i++)
 6 using namespace std;
 7 int T,l,n,c[202][202],p[1002];
 8 int f[2][202][202];
 9 int ans,a;
10 const int maxn=1e9+7;
11 int main(){
12     scanf("%d",&T);
13     int x,y;
14     while(T--){
15         ans=1<<30;
16         scanf("%d%d",&l,&n);
17         go(i,1,l) go(j,1,l) scanf("%d",&c[i][j]);
18         go(i,1,n) scanf("%d",&p[i]);
19         memset(f,0x3F , the sizeof (F));
 20 is          F [ 0 ] [ . 1 ] [ 2 ] = 0 ; P [ 0 ] = . 3 ;
 21 is          Go (K, 0 , N- . 1 ) {
 22 is              A = A ^ . 1 ; // by XOR implement scrolling array 
23 is              Memset (F [A], 0x3F , the sizeof (F [A])); // remember to update the initial value of 
24              X = P [K], Y = P [K + . 1 ];
 25              Go ( I, . 1 , L) Go (J, . 1 , L) {
26                 if(i==j) continue;
27                 if(i!=y&&j!=y) f[a][i][j]=min(f[a][i][j],f[a^1][i][j]+c[x][y]);
28                 if(x!=y&&j!=y) f[a][x][j]=min(f[a][x][j],f[a^1][i][j]+c[i][y]);
29                 if(i!=y&&x!=y) f[a][i][x]=min(f[a][i][x],f[a^1][i][j]+c[j][y]);
30             }
31         }
32         go(i,1,l) go(j,1,l)
33             if(i!=j&&i!=p[n]&&j!=p[n])
34                 ans=min(ans,f[a][i][j]);
35         printf("%d\n",ans);
36     }
37     return 0;
38 }
Code poke here

 

Guess you like

Origin www.cnblogs.com/THWZF/p/10991002.html