Advertisement
matheus_monteiro

Forming Quiz Teams

Dec 26th, 2021
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.23 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define ii pair<int, int>
  5. #define fi first
  6. #define se second
  7. #define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
  8.  
  9. const int OO = 0x3f3f3f3f; // 0x3f3f3f3f;
  10.  
  11. int n;
  12. vector<ii> v;
  13. double dp[1 << 17];
  14. double seen[1 << 17];
  15. int tempo = 1;
  16.  
  17. double sq(double x) {
  18.     return x * x;
  19. }
  20.  
  21. double dist(int i, int j) {
  22.     return sqrt(sq(v[i].fi - v[j].fi) + sq(v[i].se - v[j].se));
  23. }
  24.  
  25. double sol(int mask) {
  26.     if(mask == ((1 << n) - 1))
  27.         return 0;
  28.     if(seen[mask] == tempo) return dp[mask];
  29.     double ans = OO;
  30.     for(int i = 0; i < n; ++i)
  31.         for(int j = i + 1; j < n; ++j)
  32.             if((mask & (1 << i)) == 0 and (mask & (1 << j)) == 0)  
  33.                 ans = min(ans, sol(mask | (1 << i) | (1 << j)) + dist(i, j));
  34.     seen[mask] = tempo;
  35.     return dp[mask] = ans;
  36. }
  37.  
  38. void solve(){  
  39.     int caso = 1;
  40.     while(cin >> n and n) {
  41.         n *= 2;
  42.         string s;
  43.         v.clear();
  44.         v.resize(n);
  45.         for(ii &a : v) cin >> s >> a.fi >> a.se;
  46.         ++tempo;
  47.         cout << "Case " << caso++ << ": " << fixed << setprecision(2) << sol(0) << '\n';
  48.     }
  49. }
  50.  
  51. int32_t main() {
  52.     fastio;
  53.     solve();
  54.     return 0;
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement