IPUOJ24101旅行商问题(状压dp)

 

旅行商问题·基础版

题目↓(建议全屏看图)

 

 

挺简单的 集合状压,具体看代码吧

复制代码
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define fr(g,h) for(int g = 0; g < h; g++)
  4. const int N = 25, M = 2100000;
  5. int n,nn,mm,a[N][N],f[N][M];
  6. inline void init ()
  7. {
  8. scanf("%d",&n);
  9. fr(i,n)
  10. fr(j,n)
  11. cin >> a[i][j];
  12. memset(f, 0x3f, sizeof f);
  13. }
  14. void floyd()
  15. {
  16. fr(k,n)
  17. fr(i,n)
  18. fr(j,n)
  19. a[i][j] = min(a[i][j], a[i][k] + a[k][j]);
  20. }
  21. void dp()
  22. {
  23. for(int s = 1; s <= (1 << n) - 1; s++) //从第一个开始走过 (这个地方s的优化还是蛮重要的,暴力开210万只能险过)
  24. fr(i,n)
  25. fr(j,n)
  26. if(s & (1 << j))
  27. f[i][s|(1<<i)] = min(f[i][s|(1<<i)], f[j][s] + a[i][j]);
  28. }
  29. int main()
  30. {
  31. init();
  32. f[0][1] = 0;
  33. floyd();
  34. dp();
  35. cout << f[0][(1<<n) - 1];
  36. return 0;
  37. }
复制代码

 旅行商问题·进阶版

 

 

复制代码
  1. //旅行商2
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4. #define fr(g,h) for(int g = 0; g < h; g++)
  5.  
  6. const int N = 13, M = 531441 * 3; //M是3的13次方
  7. int pow3[N + 1], n;
  8. int a[N][N], f[N][M];
  9. inline void init ()
  10. {
  11. scanf("%d",&n);
  12. fr(i,n)
  13. fr(j,n)
  14. cin >> a[i][j];
  15. memset(f, 0x3f, sizeof f);
  16. }
  17. void floyd()
  18. {
  19. fr(k,n)
  20. fr(i,n)
  21. fr(j,n)
  22. a[i][j] = min(a[i][j], a[i][k] + a[k][j]);
  23. }
  24. int main()
  25. {
  26. pow3[0] = 1;
  27. for (int i = 1; i <= N; ++i) pow3[i] = pow3[i - 1] * 3; //预处理3的指数表
  28. init();
  29. floyd();
  30. int nn = pow3[n] - 1;
  31. f[0][1] = 0;
  32. for (int k = 1; k <= nn; k++)
  33. for (int i = 0; i < n; i++) {
  34. if (f[i][k] == 0x3f3f3f3f) continue;
  35. for (int j = 0; j < n; j++) {
  36. if (i == j) continue;
  37. int x = pow3[j];
  38. if ((k / x) % 3 < 2) {
  39. int kn = k + x;
  40. f[j][kn] = min(f[j][kn], f[i][k] + a[i][j]);
  41. }
  42. }
  43. }
  44. printf("%d", f[0][nn]);
  45. return 0;
  46. }
复制代码

 

posted @   QUEKI嶺冬  阅读(268)  评论(0编辑  收藏  举报
/*! Color themes for Google Code Prettify | MIT License | github.com/jmblog/color-themes-for-google-code-prettify */ .pln{color:#4d4d4c}ol.linenums{margin-top:0;margin-bottom:0;color:#8e908c}li.L0,li.L1,li.L2,li.L3,li.L4,li.L5,li.L6,li.L7,li.L8,li.L9{padding-left:1em;background-color:#fff;list-style-type:decimal!important;}@media screen{.str{color:#718c00}.kwd{color:#8959a8}.com{color:#8e908c}.typ{color:#4271ae}.lit{color:#f5871f}.pun{color:#4d4d4c}.opn{color:#4d4d4c}.clo{color:#4d4d4c}.tag{color:#c82829}.atn{color:#f5871f}.atv{color:#3e999f}.dec{color:#f5871f}.var{color:#c82829}.fun{color:#4271ae}} /*下面是我设置背景色,字体大小和字体*/ .cnblogs-markdown code{ background:#fff!important; } .cnblogs_code,.cnblogs_code span,.cnblogs-markdown .hljs{ font-size:16px!important; } .syntaxhighlighter a, .syntaxhighlighter div, .syntaxhighlighter code, .syntaxhighlighter table, .syntaxhighlighter table td, .syntaxhighlighter table tr, .syntaxhighlighter table tbody, .syntaxhighlighter table thead, .syntaxhighlighter table caption, .syntaxhighlighter textarea { font-size: 16px!important; } .cnblogs_code, .cnblogs_code span, .cnblogs-markdown .hljs{ font-family:consolas, "Source Code Pro", monaco, monospace !important; } //以上是代码高亮 /* 文字特效 */
点击右上角即可分享
微信分享提示