vva1025- 알고리즘 입문 경전

제목 링크https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=3466분석이 dp[T][n]에서 앞으로 계속 전환
#include
#include
#include

#define maxn 59
#define maxt 209
#define INF 0x3f3f3f3f


int N,T,M1,M2;
int dp[maxt][maxn];//t   n         
int has_train[maxt][maxn][2];//t   i     ( )    
int t[maxn];//i   i+1     


using namespace std;

void init()
{

    memset(dp,0,sizeof(dp));
    memset(has_train,0,sizeof(has_train));
    memset(t,0,sizeof(t));

}



int main()
{
    freopen("H:\\c++\\file\\stdin.txt","r",stdin);
    int k = 0; 
    while(scanf("%d",&N)&&N!=0)
    {
        init();
        scanf("%d",&T);
        for(int i=1 ; iscanf("%d",&t[i]);
        t[0] = 0;
        t[N] = 0;
        //T,    n                
        for(int i=1 ; iscanf("%d",&M1);
        while(M1--)
        {
            int start;
            scanf("%d",&start);
            for(int i=0 ; i1][0]=1;
                start += t[i];
                }

         } 



        scanf("%d",&M2);
        while(M2--)
        {
            int start;
            scanf("%d",&start);
            has_train[start][N] [1]= 1;
            for(int i=N -1; i>0;i--)
            {
                has_train[start+t[i]][i][1]=1;
                start += t[i];
                }

         } 



         //  
         for(int i=T-1 ; i>=0 ; i--)
         {
            for(int j=1 ; j<=N ; ++j)
            {
                dp[i][j] = dp[i+1][j]+1;
                if(j0] && i+t[j]<=T)
                    {//   ,           
                        if(dp[i][j]>dp[i+t[j]][j+1]){
                            dp[i][j] = dp[i+t[j]][j+1];

                         }
                     }
                if(j>1 && has_train[i][j][1] && i+t[j-1] <=T)
                    {//  
                            if(dp[i][j]>dp[i+t[j-1]][j-1]){
                            dp[i][j] = dp[i+t[j-1]][j-1];

                         }  
                     }
             }
          } 
          printf("Case Number %d: ",++k);
          if(dp[0][1]>=INF)printf("impossible
"
); else printf("%d
"
,dp[0][1]); /* */ for(int i=0 ; i<=T ; ++i) { for(int j=1 ; j<=N ; ++j) { if(dp[i][j] == dp[0][1] && has_train[i][j][0]&&has_train[i][j][1]) { printf("%d %d
"
,i,j); } } } printf("
"
); } return 0; }

출력 결과(엄격하게 증명되지 않음)
  /*         */
          for(int i=0 ; i<=T ; ++i)
          {
            for(int j=1 ; j<=N ; ++j)
            {
                if(dp[i][j] == dp[0][1] && has_train[i][j][0]&&has_train[i][j][1])
                {
                    printf("%d %d
"
,i,j); } } }

좋은 웹페이지 즐겨찾기