Acwing P274 mobile service solution to a problem

Daily questions day21 punch

Analysis

DP is the number of status request has been completed by assigning an attendant
can "i completed - 1 request state" to "i completed requests status"
then we can know that transferred from dp [i - 1] - > DP [i]
DP [i] [X] [Y] [Z] is representative of a case where the i-th selected, corresponding 1,2,3 attendant
position corresponding to
it can be known
DP [i] [ arr [i]] [y] [z] = min (dp [i] [arr [i]] [y] [z], dp [i - 1] [x] [y] [z] + cost [x ] [ARR [I]])
DP [I] [X] [ARR [I]] [Z] = min (DP [I] [X] [ARR [I]] [Z], DP [I -. 1] [X] [Y] [Z] + cost [Y] [ARR [I]])
DP [I] [X] [Y] [ARR [I]] = min (DP [I] [X] [Y] [arr [i]], dp [i - 1] [x] [y] [z] + cost [z] [arr [i]])

Because there must be a position, he inherited a position
so save two states on it

三种状态转移 pi,x,y
dp[i][x][y] = min(dp[i][x][y], dp[i - 1][x][y] + cost[pi - 1][pi])
dp[i][pi][y] = min(dp[i][pi][y], dp[i - 1][x][y] + cost[x][pi])
dp[i][x][pi] = min(dp[i][x][pi], dp[i - 1][x][y] + cost[y][pi])

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define maxl 200+10
#define maxn 1000+10
#define INF 2147483647
using namespace std;
inline int read() 
{
    int x=0;
    bool f=1;
    char c=getchar();
    for(; !isdigit(c); c=getchar()) if(c=='-') f=0;
    for(; isdigit(c); c=getchar()) x=(x<<3)+(x<<1)+c-'0';
    if(f) return x;
    return 0-x;
}
inline void write(int x)
{
    if(x<0){putchar('-');x=-x;}
    if(x>9)write(x/10);
    putchar(x%10+'0');
}
int l,n;
int c[maxl][maxl],p[maxn],dp[maxn][maxl][maxl];
signed main()
{
    memset(dp,127,sizeof(dp));
    l=read();n=read();
    for(int i=1;i<=l;i++)
        for(int j=1;j<=l;j++)
            c[i][j]=read();
    for(int i=1;i<=n;i++) p[i]=read();
    dp[0][1][2]=0;
    p[0]=3;
    for(int i=0;i<=n-1;i++)
        for(int x=1;x<=l;x++)
            for(int y=1;y<=l;y++)
            {
                if(x==y||x==p[i]||y==p[i]) continue;
                dp[i+1][x][y]=min(dp[i+1][x][y],dp[i][x][y]+c[p[i]][p[i+1]]);
                dp[i+1][p[i]][y]=min(dp[i+1][p[i]][y],dp[i][x][y]+c[x][p[i+1]]);
                dp[i+1][x][p[i]]=min(dp[i+1][x][p[i]],dp[i][x][y]+c[y][p[i+1]]);
            }
    int ans=INF;
    for(int i=1;i<=l;i++)
        for(int j=1;j<=l;j++)
            ans=min(ans,dp[n][i][j]);
    write(ans);
    return 0;
}

Please Gangster treatise(Anyway, I do not know what that means treatise)

Guess you like

Origin www.cnblogs.com/handsome-zyc/p/11586880.html