标签:
最基本的在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\n"); 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\n", dp[b]); 52 } 53 return 0; 54 }
标签:
原文地址:http://www.cnblogs.com/huoxiayu/p/4694606.html