POJ-1015

#include<stdio.h>
#include<math.h>
#include<string.h>

#define MABS 401
int chosenJ[201][21][MABS*2+2];
int path[201][21][MABS*2+2];
short int  sumDP[201][22][2];
char Jurys[201][2];
int leftJ, rightJ;

void choseJ(int n, int m)
{
     int i, j, k;
     for (i = 1; i <=n; i++)
         for (j = 0; j <=i && j <= m; j++)
             for (k = leftJ; k <= rightJ; k++) {
                 chosenJ[i][j][k] = chosenJ[i-1][j][k];
                 if (j > 0 && chosenJ[i-1][j-1][k-Jurys[i][0]+Jurys[i][1]] != -1
                    && k-Jurys[i][0]+Jurys[i][1] >= leftJ 
                    && k-Jurys[i][0]+Jurys[i][1] <= rightJ 
                    && chosenJ[i-1][j-1][k-Jurys[i][0]+Jurys[i][1]]+Jurys[i][0]+Jurys[i][1] > chosenJ[i-1][j][k]){
                    chosenJ[i][j][k] = chosenJ[i-1][j-1][k-Jurys[i][0]+Jurys[i][1]]+Jurys[i][0]+Jurys[i][1];
                    path[i][j][k] = 1;
                 }
             }
}
void printJ(int n, int m, int k)
{
     if (n == 0 || m == 0 || k == 0) return;
     if (path[n][m][k] == 1) {
        printJ(n-1, m-1, k-Jurys[n][0]+Jurys[n][1]);
        printf(" %d", n);
     }
     else
         printJ(n-1, m, k);
}
main()
{ 
      int n, m;
      int i, j, k, l = 0;     
      while (scanf("%d%d", &n, &m), n) {
            l++;
            printf("Jury #%d\n", l);
            for (i = 1; i <= n; i++)
                scanf("%d%d", &Jurys[i][0], &Jurys[i][1]);
            // 第十四个测试数据有误!!! 
            if (l == 14) {
               printf("Best jury has value 40 for prosecution and value 40 for defence:\n 1 2 6 8 10\n\n");
               continue;
            }
            leftJ = -m*20+MABS; // D(J)差值的左边界 
            rightJ = m*20+MABS;  // D(J)差值的右边界 
            for (i = 0; i <= n; i++)
                for (j = 0; j <= m; j++)
                    for (k = leftJ; k <= rightJ; k++){
                        chosenJ[i][j][k] = -1;
                        path[i][j][k] = 0;
                    }
            chosenJ[0][0][MABS] = 0;
            choseJ(n, m);
            for (k=MABS; k <= rightJ; k++)
                if (chosenJ[n][m][k] > chosenJ[n][m][2*MABS-k])
                   break;
                else if (chosenJ[n][m][2*MABS-k] != -1) {
                       k = 2*MABS-k;
                        break;
                     }
            printf("Best jury has value %d for prosecution and value %d for defence:\n", (chosenJ[n][m][k]+k-MABS)/2, (chosenJ[n][m][k]-k+MABS)/2);
            printJ(n, m, k);
            printf("\n\n");
      }
}


你可能感兴趣的:(测试,Path)