Advertisement
midnight_sun

Untitled

Nov 17th, 2023
566
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.21 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. //0-O-석산고, 1-A-서석고, 2-B-중남중(?), 3-C-광주여상(?), 4-D-봉선행정복지, 5-E-학동 중심사거리, 6-F-(이름 X), 7-G-광주역
  4. int path[8][8] = {};
  5. bool visited[8] = {};
  6. vector<int> v[8] = {};
  7. int idx = 0;
  8. void set_distance() {//단위 처리, 소수점 처리하기 귀찮으니 *10으로 처리해줌
  9.     path[1][0] = path[0][1] = 42;
  10.     path[2][0] = path[0][2] = 38;
  11.     path[3][0] = path[0][3] = 37;
  12.     path[4][0] = path[0][4] = 16;
  13.     path[5][0] = path[0][5] = 25;
  14.     path[6][0] = path[0][6] = 36;
  15.     path[7][0] = path[0][7] = 44;
  16.  
  17.     path[1][2] = path[2][1] = 42;
  18.     path[1][3] = path[3][1] = 50;
  19.     path[1][4] = path[4][1] = 47;
  20.     path[1][5] = path[5][1] = 57;
  21.     path[1][6] = path[6][1] = 53;
  22.     path[1][7] = path[7][1] = 34;
  23.  
  24.     path[2][3] = path[3][2] = 45;
  25.     path[2][4] = path[4][2] = 48;
  26.     path[2][5] = path[5][2] = 55;
  27.     path[2][6] = path[6][2] = 70;
  28.     path[2][7] = path[7][2] = 67;
  29.  
  30.     path[3][4] = path[4][3] = 32;
  31.     path[3][5] = path[5][3] = 53;
  32.     path[3][6] = path[6][3] = 66;
  33.     path[3][7] = path[7][3] = 70;
  34.  
  35.     path[4][5] = path[5][4] = 23;
  36.     path[4][6] = path[6][4] = 45;
  37.     path[4][7] = path[7][4] = 59;
  38.  
  39.     path[5][6] = path[6][5] = 29;
  40.     path[5][7] = path[7][5] = 45;
  41.  
  42.     path[6][7] = path[7][6] = 25;
  43. }
  44. vector<pair<pair<int, vector<int>>, string>> tem;
  45. //first.first 거리 총합, first.second 노선, second 방문
  46. vector<int> cur_path;
  47. void find_path(int local_idx, int cost, int goal) {
  48.     if (goal == 0) {
  49.         string k = "00000000";
  50.         for (auto i : cur_path) {
  51.             k[i] = '1';
  52.         }
  53.         if (visited[0] == 1) tem.push_back({ { cost,cur_path },k });
  54.         return;
  55.     }
  56.     for (int i = 0; i <= 7; i++) {
  57.         if (visited[i] == 0) {
  58.             cur_path.push_back(i);
  59.             visited[i] = 1;
  60.             find_path(i, cost + path[local_idx][i], goal - 1);
  61.             visited[i] = 0;
  62.             cur_path.pop_back();
  63.         }
  64.     }
  65. }
  66. int main() {
  67.     set_distance();
  68.     for (int g = 1; g <= 7; g++) {
  69.         cur_path.push_back(g);
  70.         visited[g] = 1;
  71.         find_path(g, 0, 7 / 2 + 1);
  72.         find_path(g, 0, 7 / 2 + 2);
  73.         cur_path.pop_back();
  74.         visited[g] = 0;
  75.     }
  76.     int final_cost = 1e9;
  77.     vector<int> final_path[7] = {};
  78.     std::sort(tem.begin(), tem.end());
  79.  
  80.     /*for (auto i : tem) {
  81.         cout << i.first.first << " " << i.second << "\n";
  82.         for (auto g : i.first.second) cout << g << " ";
  83.         cout << "\n";
  84.     }*/
  85.     //cout << tem.size() << "\n";
  86.  
  87.     for (int i = 0; i < tem.size(); i++) {
  88.         int a = tem[i].first.first;
  89.         int b = 1e9;
  90.         if (a > final_cost) continue;
  91.         string need = "11111111";
  92.         for (auto g : tem[i].first.second) {
  93.             //cout << g << " ";
  94.             need[g] = '0';
  95.         }
  96.         int idx = -1;
  97.         need[0] = '1';
  98.         for (int g = i + 1; g < tem.size(); g++) {
  99.             bool flag = 1;
  100.             for (int z = 0; z < 8; z++) {
  101.                 if (need[z] == '1' && tem[g].second[z] == '0') flag = 0;
  102.             }
  103.             if (flag==1) {
  104.  
  105.                 if (b > tem[g].first.first) {
  106.                     b = tem[g].first.first;
  107.                     idx = g;
  108.                 }
  109.             }
  110.         }
  111.         if (idx == -1) continue;
  112.         //cout << a << " " << final_cost << "\n";
  113.         if (a < final_cost) {
  114.             final_cost = a;
  115.             final_path[0] = tem[i].first.second;
  116.             final_path[1] = tem[idx].first.second;
  117.         }
  118.     }
  119.     cout << final_cost << "\n";
  120.     for (int i : final_path[0]) cout << i << " ";
  121.     cout << "\n";
  122.     for (int i : final_path[1]) cout << i << " ";
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement