zoukankan      html  css  js  c++  java
  • acdream 1025 简单dp

    最基本的在DAG上求最短路。

     1 #include <iostream>
     2 #include <cstring>
     3 #include <cstdio>
     4 using namespace std;
     5 
     6 const int INF = 999999;
     7 const int N = 100001;
     8 int dp[N];
     9 
    10 int main ()
    11 {
    12     int a, b;
    13     while ( scanf("%d%d", &a, &b) != EOF )
    14     {
    15         if ( a > b )
    16         {
    17             printf("-1
    ");
    18             continue;
    19         }
    20         dp[a] = 0;
    21         for ( int i = a + 1; i <= b; i++ )
    22         {
    23             dp[i] = INF;
    24         }    
    25         for ( int i = a; i < b; i++ )
    26         {
    27             int j;
    28             for ( j = 1; j * j < i; j++ )
    29             {
    30                 if ( i % j ) continue;
    31                 int x = i + j;
    32                 if ( x <= b )
    33                 {
    34                     dp[x] = min( dp[x], dp[i] + 1 );
    35                 }
    36                 int y = i + i / j;
    37                 if ( y <= b )
    38                 {
    39                     dp[y] = min( dp[y], dp[i] + 1 );
    40                 }
    41             }
    42             if ( i % j == 0 )
    43             {
    44                 int z = i + j;
    45                 if ( z <= b )
    46                 {
    47                     dp[z] = min( dp[z], dp[i] + 1 );
    48                 }
    49             }
    50         }
    51         printf("%d
    ", dp[b]);
    52     }
    53     return 0;
    54 }
  • 相关阅读:
    Linux基础
    杂谈
    MySQL基础
    Effective Java-第4章
    Effective Java-第三章
    Effective Java-第二章
    mybatis
    mapper.xml文件
    Mybatis
    mybatis-config.xml文件详解
  • 原文地址:https://www.cnblogs.com/huoxiayu/p/4694606.html
Copyright © 2011-2022 走看看