Advertisement
Infernale

Shortest Summation Distance [NCTU Floor 9] [Bruteforce]

Dec 24th, 2019
503
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.47 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <algorithm>
  4. #include <vector>
  5. #include <sstream>
  6. #include <cstdlib>
  7. using namespace std;
  8.  
  9. struct Point{
  10.     int x, y, index;
  11.     Point(int x, int y, int idx){
  12.         this->x = x;
  13.         this->y = y;
  14.         this->index = idx;
  15.     }
  16. };
  17.  
  18. void createCombinations(vector <int> arr, string out, int i, int n, int k, vector <string> &results){
  19.     if(k == 0){
  20.         results.push_back(out);
  21.         return;
  22.     }
  23.     for(int j = i; j < n; j++){
  24.         createCombinations(arr, out + " " + to_string(arr[j]), j + 1, n, k - 1, results);
  25.     }
  26. }
  27.  
  28. double calculateDistance(int x1, int y1, int x2, int y2){
  29.     return sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2));
  30. }
  31.  
  32. bool compareDistance(pair <Point, Point> p1, pair <Point, Point> p2){
  33.     return calculateDistance(p1.first.x, p1.first.y, p1.second.x, p1.second.y) < calculateDistance(p2.first.x, p2.first.y, p2.second.x, p2.second.y);
  34. }
  35.  
  36. void solve(vector <Point> houses){
  37.     const int n = houses.size();
  38.     vector <pair<Point, Point>> points;
  39.     vector <double> distances;
  40.     vector <bool> flag(n, false);
  41.     vector <int> index(n * (n - 1) / 2);
  42.     vector <string> combinations;
  43.  
  44.     for(int i = 0; i < index.size(); i++){
  45.         index[i] = i;
  46.     }
  47.     createCombinations(index, "", 0, n * (n - 1) / 2, n / 2, combinations);
  48.  
  49.     for(int i = 0; i < n; i++){
  50.         for(int j = i + 1; j < n; j++){
  51.             points.push_back({houses[i], houses[j]});
  52.         }
  53.     }
  54.     for(int i = 0; i < n * (n - 1) / 2; i++){
  55.         distances.push_back(calculateDistance(points[i].first.x, points[i].first.y, points[i].second.x, points[i].second.y));
  56.     }
  57.  
  58.     int pair;
  59.     bool valid;
  60.     double sum = 10e9, temp = 0;
  61.     for(int i = 0; i < combinations.size(); i++){
  62.         fill(flag.begin(), flag.end(), false);
  63.         temp = 0;
  64.         valid = true;
  65.         stringstream ss(combinations[i]);
  66.         while(ss >> pair){
  67.             if(!flag[points[pair].first.index] && !flag[points[pair].second.index]){
  68.                 temp += distances[pair];
  69.                 flag[points[pair].first.index] = true;
  70.                 flag[points[pair].second.index] = true;
  71.             }else{
  72.                 valid = false;
  73.                 break;
  74.             }
  75.         }
  76.         if(valid){
  77.             sum = min(sum, temp);
  78.         }
  79.         ss.clear();
  80.     }
  81.     cout << fixed << setprecision(2) << sum << " ";
  82. }
  83.  
  84. int main(){
  85.     int n, x, y, index;
  86.     vector <Point> houses;
  87.     string temp;
  88.     getline(cin, temp);
  89.     stringstream stream(temp);
  90.     while(stream >> n){
  91.         index = 0;
  92.         houses.reserve(2 * n);
  93.         for(int i = 0; i < 2 * n; i++){
  94.             stream >> x >> y;
  95.             Point p(x, y, index++);
  96.             houses.push_back(p);
  97.         }
  98.         solve(houses);
  99.         houses.clear();
  100.     }
  101.     return 0;
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement