1 条题解

  • 0
    @ 2021-6-15 10:11:12

    C++ :

    #include<cstdio>
    #include<cstring>
    #include<limits>
    #include<queue>
    
    using namespace std;
    
    int f[201][201][201];
    bool inQ[201][201][201];
    
    int main() {
      short s[3], d;
      //freopen("input.txt","r",stdin);
      //freopen("output.txt","w",stdout);
      int T;
      scanf("%d",&T);
      while(T--) {
        scanf("%hd%hd%hd%hd", s, s+1, s+2, &d);
        short res1 = -1;
        int res0 = numeric_limits<int>::max();
        queue<int> q;
        memset(f, 0x7f, sizeof(f));
        f[0][0][s[2]] = 0;
        while(!q.empty())
          q.pop();
        q.push(s[2]);
        memset(inQ, 0, sizeof(inQ));
        inQ[0][0][s[2]] = 1;
        while(!q.empty()) {
          int t0 = q.front();
          short
            u[] = {(short)(t0>>16), (short)((t0>>8)&((1<<8)-1)), (short)(t0&((1<<8)-1))};
          q.pop();
          inQ[u[0]][u[1]][u[2]] = 0;
          for(int i = 0; i < 3; i++)
            if((u[i]<=d && u[i]>res1) || (u[i]==res1 && f[u[0]][u[1]][u[2]]<res0)) {
              res0 = f[u[0]][u[1]][u[2]];
              res1 = u[i];
            }
          for(int i = 0; i < 3; i++)
            for(int j = 0; j < 3; j++)
              if(i != j) {
                short t1 = min(u[i], (short)(s[j]-u[j]));
                short v[3];
                if(t1 == 0)
                  continue;
                v[i] = u[i]-t1;
                v[j] = u[j]+t1;
                v[3-i-j] = u[3-i-j];
                if(f[v[0]][v[1]][v[2]] > f[u[0]][u[1]][u[2]]+t1) {
                  f[v[0]][v[1]][v[2]] = f[u[0]][u[1]][u[2]]+t1;
                  if(!inQ[v[0]][v[1]][v[2]]) {
                    inQ[v[0]][v[1]][v[2]] = 1;
                    q.push((v[0]<<16) | (v[1]<<8) | v[2]);
                  }
                }
              }
        }
        printf("%d %d\n", res0, res1);
      }
      return 0;
    }
    
    
    • 1

    信息

    ID
    408
    时间
    1000ms
    内存
    128MiB
    难度
    10
    标签
    递交数
    3
    已通过
    0
    上传者