Advertisement
a_chn

Untitled

Feb 2nd, 2024
906
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.37 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4. typedef pair<int,int> pii;
  5. # define im INT_MAX
  6. # define umap unordered_map
  7. # define uset unordered_set
  8. # define f first
  9. # define s second
  10. # define pq priority_queue
  11. # define pb push_back
  12. # define ins insert
  13. # define INF LLONG_MAX
  14. #define int long long
  15. uset<int> visited;
  16. set<int> curr_component;
  17. map<int,vector<int>> graph;
  18. int component_num = 1;
  19. void dfs(int node){
  20.     visited.insert(node);
  21.     curr_component.insert(node);
  22.     for (int v : graph[node]){
  23.         if (!visited.count(v)){dfs(v);}
  24.     }
  25. }
  26.  
  27. void setIO(string s) {
  28.     ios_base::sync_with_stdio(0);
  29.     cin.tie(0);
  30.     freopen((s + ".in").c_str(), "r", stdin);
  31.     freopen((s + ".out").c_str(), "w", stdout);
  32. }
  33.  
  34. signed main(){
  35.     // setIO("test");
  36.     // ifstream cin("test.in");  
  37.     // ofstream cout("test.out");
  38.    
  39.     int t; cin >> t;
  40.     while (t--){
  41.         visited.clear(); graph.clear(); component_num=1;
  42.         int n, m; cin >> n >> m;
  43.         for (int i=0; i<m; i++){
  44.             int a, b; cin >> a >> b;
  45.             graph[a].pb(b); graph[b].pb(a);
  46.         }
  47.  
  48.         vector<set<int>> components(n+1);
  49.         set<int> component_with_1; set<int> component_with_n;
  50.         for (int i=1; i<=n; i++){
  51.             if (!visited.count(i)){
  52.                 curr_component.clear();
  53.                 dfs(i);
  54.                 components[component_num] = curr_component;
  55.                 component_num++;
  56.             }
  57.         }
  58.  
  59.         // print2dCont(components);
  60.  
  61.         vector<int> cost_from_1(n+1, LLONG_MAX); vector<int> cost_to_n(n+1, LLONG_MAX);
  62.         for (int i=1; i<component_num; i++){
  63.             if (components[i].count(1)){component_with_1 = components[i]; cost_from_1[i] = 0;}
  64.             if (components[i].count(n)){component_with_n = components[i]; cost_to_n[i] = 0;}
  65.         }
  66.  
  67.         // printArray(cost_from_1,component_num);
  68.         // printArray(cost_to_n,component_num);
  69.  
  70.         // compute the min cost from component with 1 to all components and then from n to all components
  71.         // then we take the min value of (1 to intermediary) + (intermediary to n)
  72.    
  73.         for (int id=1; id<component_num; id++){ // for all components except for 1
  74.             set<int> intermediary = components[id];
  75.             for (int mid_barn : intermediary){ // for all nodes in
  76.                 // compute minimum cost from mid_barn starting at start_barn
  77.                 auto it = component_with_1.lower_bound(mid_barn);
  78.                 if (it==begin(component_with_1)){
  79.                     cost_from_1[id] = min(cost_from_1[id],(mid_barn-*it)*(mid_barn-*it));
  80.                 } else if (it==end(component_with_1)){
  81.                     it--;
  82.                     cost_from_1[id] = min(cost_from_1[id],(mid_barn-*it)*(mid_barn-*it));
  83.                 } else {
  84.                     int cur = *it; --it;
  85.                     int prev = *it;
  86.                     cost_from_1[id] = min(cost_from_1[id],min((mid_barn-cur)*(mid_barn-cur),(mid_barn-prev)*(mid_barn-prev)));
  87.                 }
  88.             }
  89.         }
  90.         // cout << "cost from component with 1:\n";
  91.         // printMap(cost_from_1); cout << '\n';
  92.  
  93.  
  94.         for (int id=1; id<component_num; id++){
  95.             set<int> intermediary = components[id];
  96.             for (int mid_barn : intermediary){
  97.                 // compute minimum cost from mid_barn starting at start_barn
  98.                 auto it = component_with_n.lower_bound(mid_barn);
  99.                 if (it==begin(component_with_n)){
  100.                     cost_to_n[id] = min(cost_to_n[id],(mid_barn-*it)*(mid_barn-*it));
  101.                 } else if (it==end(component_with_n)){
  102.                     it--;
  103.                     cost_to_n[id] = min(cost_to_n[id],(mid_barn-*it)*(mid_barn-*it));
  104.                 } else {
  105.                     int cur = *it; --it;
  106.                     int prev = *it;
  107.                     cost_to_n[id] = min(cost_to_n[id],min((mid_barn-cur)*(mid_barn-cur),(mid_barn-prev)*(mid_barn-prev)));
  108.                 }
  109.             }                
  110.         }
  111.         // cout << "cost from component with n:\n";
  112.         // printMap(cost_to_n);
  113.  
  114.         int total_min_cost = LLONG_MAX;
  115.         for (int id=1; id<component_num; id++){
  116.             total_min_cost = min(total_min_cost,cost_from_1[id]+cost_to_n[id]);
  117.         }
  118.         cout << total_min_cost << '\n';
  119.     }
  120.    
  121.  
  122.     return 0;
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement