fork download
  1. /*
  2. * Author: Geeza
  3. */
  4.  
  5. #include <bits/stdc++.h>
  6.  
  7. #define ld long double
  8. #define ll long long
  9. #define pb push_back
  10. #define fin(a, n) for(int i = a; i < n; i++)
  11. #define fjn(a, n) for(int j = a; j < n; j++)
  12. #define all(a) a.begin(),a.end()
  13. #define allr(a) a.rbegin(),a.rend()
  14. #define FAST ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
  15.  
  16. using namespace std;
  17.  
  18. const double PI = acos(-1);
  19. const int N = 2e5+20;
  20. const ll oo = 0x3f3f3f3f3f3f3f3f;
  21. const int mod = 998244353, inf = 1e6;
  22.  
  23. string di[] = {"D","L", "U", "R", "UL", "UR", "DL", "DR"};
  24. int dx[] = {+1, +0, +0, -1, -1, -1, +1, +1};
  25. int dy[] = {+0, -1, +1, +0, -1, +1, -1, +1};
  26. char dc[] = {'D', 'L', 'R', 'U'};
  27.  
  28. int n;
  29. vector<vector<ll>> dist;
  30. ll dp[20][20];
  31. vector<array<ll, 3>> v;
  32.  
  33. ll calc(int idx = 0, ll mask = 1ll) {
  34. if (mask == (1<<n)-1) {
  35. return dist[idx][0];
  36. }
  37.  
  38. ll &ret = dp[idx][mask];
  39. if (~ret) return ret;
  40. ret = oo;
  41. for (int i = 0; i < n; i++) {
  42. if ((mask>>i)&1) continue;
  43. ret = min(ret, calc(i, mask|(1ll<<i))+dist[idx][i]);
  44. }
  45. return ret;
  46. }
  47.  
  48. void solve() {
  49. cin >> n;
  50. v.assign(n, {0, 0, 0});
  51. fin(0, n) cin >> v[i][0] >> v[i][1] >> v[i][2];
  52.  
  53. dist = vector<vector<ll>>(n, vector<ll>(n, 0));
  54. for (int i = 0; i < n; i++) {
  55. for (int j = 0; j < n; j++) {
  56. if (i == j) continue;
  57. dist[i][j] = abs(v[i][0]-v[j][0]) + abs(v[i][1]-v[j][1]) + max(0ll, v[j][2]-v[i][2]);
  58. }
  59. }
  60.  
  61. memset(dp, -1, sizeof dp);
  62. cout << calc() << "\n";
  63. }
  64.  
  65. int main() {
  66. FAST;
  67. #ifndef ONLINE_JUDGE
  68. freopen("input.txt","r",stdin);
  69. freopen("output.txt","w",stdout);
  70. #endif
  71. int tt = 1; //cin >> tt;
  72. while(tt--){
  73. solve();
  74. }
  75. return 0;
  76. }
Success #stdin #stdout 0.01s 5320KB
stdin
Standard input is empty
stdout
4557430888798830399