UVA 662 - Fast Food

举报
Linux猿 发表于 2021/08/05 00:07:30 2021/08/05
【摘要】 题目链接~~> 做题感悟: 这题因为输出的时候没有看到单复数,wa了老半天,无语,净犯这些低级错误。 解题思路:                 (1) . 看这题首先想到区间 dp ,一段区间如果要找一个服务的饭店那是一定的(奇数的时候是中间的那个,偶数的时候中间的两个任意一个...

题目链接~~>

做题感悟: 这题因为输出的时候没有看到单复数,wa了老半天,无语,净犯这些低级错误。

解题思路:

                (1) . 看这题首先想到区间 dp ,一段区间如果要找一个服务的饭店那是一定的(奇数的时候是中间的那个,偶数的时候中间的两个任意一个),这样我们就可以预处理出来任意一个区间放置一个主饭店服务此区间各个饭店的路程总和,同时记录下左右区间端点值,分别用 le ,rt 代表 ,这样为下面的 dp 做好了铺垫。

动态方程:dp[ rt ] [ k ]  = min( dp[ rt ] [ k  ]  , dp [ le  -  1 ]  [  k - 1  ]  +  w )  ;  dp[  rt  ] [  k  ] 代表用 k 个主饭店服务到第 rt 个饭店最小的路程,min 括号中 dp[ rt  ] [  k  ] 代表不放置当前区间 ,dp[ le - 1 ][ k - 1 ]  +  w  代表放上当前区间 。

              (2) , 跟上面一样先处理出来 d[  i  ] [  j  ] 代表  i  ~  j  这一段区间放置一个主饭店的总路程,那么用 dp [ i ] [ j ] 代表 用了 i 个主饭店管理到第 j 个饭店的最小路程。

动态方程: dp [ i ][  j ]  = min( dp[  i  ] [  j  ]  , dp[ i - 1 ] [ k ]  +  d[ k + 1 ] [ j ] )  ;

代码(1):


  
  1. #include<iostream>
  2. #include<fstream>
  3. #include<iomanip>
  4. #include<ctime>
  5. #include<fstream>
  6. #include<sstream>
  7. #include<stack>
  8. #include<cstring>
  9. #include<cmath>
  10. #include<map>
  11. #include<vector>
  12. #include<cstdio>
  13. #include<algorithm>
  14. #define INT long long int
  15. using namespace std ;
  16. const double esp = 0.00000001 ;
  17. const int mod = 1e9 + 7 ;
  18. const int MY = 30 + 10 ;
  19. const int MX = 30000 + 10 ;
  20. int n ,m ,num ,k ;
  21. int dp[MX][MY] ,sum[MX] ,g[MX] ,pre[MX][MY] ;
  22. struct node
  23. {
  24. int x ,le ,rt ,w ;
  25. }T[MX] ;
  26. void DP()
  27. {
  28. int le ,rt ,w ;
  29. memset(dp ,0x3f ,sizeof(dp)) ;
  30. dp[0][0] = 0 ;
  31. for(int i = 0 ;i < num ;++i)
  32. {
  33. le = T[i].le ; rt = T[i].rt ;
  34. w = T[i].w ;
  35. for(int k = 1 ;k <= m ;k++)
  36. if(dp[rt][k] > dp[le-1][k-1] + w)
  37. {
  38. dp[rt][k] = dp[le-1][k-1] + w ;
  39. pre[rt][k] = i ;
  40. }
  41. }
  42. }
  43. void input()
  44. {
  45. num = 0 ;
  46. sum[0] = 0 ;
  47. for(int i = 1 ;i <= n ;i++)
  48. {
  49. scanf("%d" ,&g[i]) ;
  50. sum[i] = sum[i-1] + g[i] ;
  51. }
  52. for(int i = 1 ;i <= n ;i++)
  53. for(int j = i ;j <= n ;j++)
  54. {
  55. T[num].le = i ;
  56. T[num].rt = j ;
  57. int mid = (i + j)/2 ;
  58. T[num].w = g[mid]*(2*mid-i-j)+sum[j]+sum[i-1]-sum[mid]-sum[mid-1] ;
  59. T[num++].x = mid ;
  60. }
  61. }
  62. void output(int x ,int k) // 递归打印
  63. {
  64. int pi ,le ,rt ;
  65. pi = pre[x][k] ;
  66. if(k)
  67. {
  68. le = T[pi].le ;
  69. rt = T[pi].rt ;
  70. output(le-1 ,k-1) ;
  71. if(le != rt) //注意单复数
  72. cout<<"Depot "<<k<<" at restaurant "<<T[pi].x<<" serves restaurants "<<le<<" to "<<rt<<endl ;
  73. else cout<<"Depot "<<k<<" at restaurant "<<T[pi].x<<" serves restaurant "<<T[pi].x<<endl ;
  74. }
  75. return ;
  76. }
  77. int main()
  78. {
  79. int cse = 1 ;
  80. while(scanf("%d%d" ,&n ,&m) ,n+m)
  81. {
  82. input() ;
  83. DP() ;
  84. cout<<"Chain "<<cse++<<endl ;
  85. output(n ,m) ;
  86. cout<<"Total distance sum = "<<dp[n][m]<<endl ;
  87. cout<<endl ;
  88. }
  89. return 0 ;
  90. }
代码(2):


  
  1. #include<iostream>
  2. #include<fstream>
  3. #include<iomanip>
  4. #include<ctime>
  5. #include<fstream>
  6. #include<sstream>
  7. #include<stack>
  8. #include<cstring>
  9. #include<cmath>
  10. #include<map>
  11. #include<vector>
  12. #include<cstdio>
  13. #include<algorithm>
  14. #define INT long long int
  15. using namespace std ;
  16. const double esp = 0.00000001 ;
  17. const int mod = 1e9 + 7 ;
  18. const int MY = 30 + 10 ;
  19. const int MX = 200 + 10 ;
  20. int n ,m ,num ,k ;
  21. int dp[MX][MX] ,sum[MX] ,g[MX] ,pre[MX][MX] ,d[MX][MX] ;
  22. void DP()
  23. {
  24. memset(dp ,0x3f ,sizeof(dp)) ;
  25. for(int i = 1 ;i <= n ; ++i)
  26. dp[1][i] = d[1][i] ;
  27. for(int i = 2 ;i <= m ; ++i)
  28. for(int j = i ;j <= n ; ++j)
  29. for(int k = i-1 ;k < j ; ++k)
  30. if(dp[i][j] > dp[i-1][k] + d[k+1][j])
  31. {
  32. dp[i][j] = dp[i-1][k] + d[k+1][j] ;
  33. pre[i][j] = k ;
  34. }
  35. }
  36. void input()
  37. {
  38. num = 0 ;
  39. sum[0] = 0 ;
  40. for(int i = 1 ;i <= n ;i++)
  41. {
  42. scanf("%d" ,&g[i]) ;
  43. sum[i] = sum[i-1] + g[i] ;
  44. }
  45. for(int i = 1 ;i <= n ;i++)
  46. for(int j = i ;j <= n ;j++)
  47. {
  48. int mid = (i + j)/2 ;
  49. d[i][j]= g[mid]*(2*mid-i-j)+sum[j]+sum[i-1]-sum[mid]-sum[mid-1] ;
  50. }
  51. }
  52. void output(int k ,int x) // 递归打印
  53. {
  54. if(k)
  55. {
  56. output(k-1 ,pre[k][x]) ;
  57. cout<<"Depot "<<k<<" at restaurant "<<(pre[k][x]+1+x)/2<<" serves restaurants "<<pre[k][x]+1<<" to "<<x<<endl ;
  58. }
  59. return ;
  60. }
  61. int main()
  62. {
  63. int cse = 1 ;
  64. while(scanf("%d%d" ,&n ,&m) ,n+m)
  65. {
  66. input() ;
  67. DP() ;
  68. cout<<"Chain "<<cse++<<endl ;
  69. output(m ,n) ;
  70. cout<<"Total distance sum = "<<dp[m][n]<<endl ;
  71. cout<<endl ;
  72. }
  73. return 0 ;
  74. }






文章来源: blog.csdn.net,作者:Linux猿,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/nyist_zxp/article/details/38686763

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。