frp

Aquarium

frp
Aug 22nd, 2011
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. #include <fstream>
  2. #include <vector>
  3. using namespace std;
  4. typedef unsigned short ushort;
  5. int main()
  6. {
  7. ifstream in("aquarium.in");
  8. int n;
  9. in >> n;
  10. int m[13][13];
  11. for(int i = 0; i < n; i++)
  12. for(int j = 0; j < n; j++)
  13. in >> m[i][j];
  14. int res[16384][13];
  15. int pm[16384][13];
  16. int pl[16384][13];
  17. for(ushort i = 0; i < (1 << n); i++)
  18. {
  19. for(int j = 0; j < n; j++)
  20. if(i & (1 << j))
  21. {
  22. ushort pmask = i ^ (1 << j);
  23. res[i][j] = 1000000000;
  24. if(pmask != 0)
  25. {
  26. for(int l = 0; l < n; l++)
  27. if(pmask & (1 << l))
  28. if(res[pmask][l] + m[l][j] < res[i][j])
  29. {
  30. res[i][j] = res[pmask][l] + m[l][j];
  31. pl[i][j] = l;
  32. pm[i][j] = pmask;
  33. }
  34. }
  35. else
  36. {
  37. res[i][j] = 0;
  38. pl[i][j] = -1;
  39. }
  40. }
  41. }
  42. in.close();
  43. int mlast = 0;
  44. for(int i = 0; i < n; i++)
  45. if(res[(1 << n) - 1][i] < res[(1 << n) - 1][mlast])
  46. mlast = i;
  47. vector<int> path;
  48. int pl_c = mlast, pm_c = (1 << n) - 1;
  49. while(pl_c != -1)
  50. {
  51. path.push_back(pl_c);
  52. int opl = pl_c, opm = pm_c;
  53. pl_c = pl[opm][opl];
  54. pm_c = pm[opm][opl];
  55. }
  56. ofstream out("aquarium.out");
  57. out << res[(1 << n) - 1][mlast] <<'\n';
  58. for(int i = path.size() - 1; i >= 0; i--)
  59. out << (i == path.size() - 1 ? "" : " ") << path[i] + 1;
  60. out << '\n';
  61. out.close();
  62. return 0;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment