Advertisement
Mohammad_Dipu_Sultan

Travelling SalesPerson_SRBD

Oct 15th, 2023
677
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.60 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int a[12][12];
  4. vector<int>seen;
  5. int ans, n;
  6. void solve(int cur, int ind, int cost){
  7.     if(ind == n-1){
  8.         int x;
  9.         x = a[cur][0];
  10.         ans = min(ans, x+cost);
  11.         return;
  12.     }
  13.  
  14.     for(int i = 1; i < n; i++){
  15.         if(seen[i] == 0){
  16.             seen[i] = 1;
  17.             solve(i, ind+1, cost+a[cur][i]);
  18.             seen[i] = 0;
  19.         }
  20.     }
  21. }
  22. int main(){
  23.  
  24.     int t;
  25.     cin >> t;
  26.     while(t--){
  27.         cin >> n;
  28.         seen.assign(n, 0);
  29.         for(int i= 0; i < n; i++){
  30.             for(int j = 0; j < n; j++){
  31.                 cin >> a[i][j];
  32.             }
  33.         }
  34.         ans = INT_MAX;
  35.         solve(0, 0, 0);
  36.         cout << ans << endl;
  37.     }
  38.     return 0;
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement