Advertisement
Guest User

Untitled

a guest
Apr 4th, 2020
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.00 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. int n;
  6. int a[16][16];
  7. int visited[16] = {0};
  8. int res = INT_MAX;
  9.  
  10. void init(){
  11.     cin >> n;
  12.     for(int i=1;i<=n;i++){
  13.         for(int j=1;j<=n;j++){
  14.             cin >> a[i][j];
  15.         }
  16.     }
  17. }
  18.  
  19. bool checkz(){
  20.     for(int i=1;i<=n;i++){
  21.         if (visited[i] == 0){
  22.             return false;
  23.         }
  24.     }
  25.     return true;
  26. }
  27.  
  28. void Try(int u,int cnt){
  29.     if (cnt < res){
  30.         if (checkz()){
  31.             res = min(res,cnt + a[1][u]);
  32.         }
  33.         else{
  34.             for(int i=1;i<=n;i++){
  35.                 if (visited[i] == 0 && u != i ){
  36.                     visited[i] = 1;
  37.                     Try(i,cnt+a[u][i]);
  38.                     visited[i] = false;
  39.                 }    
  40.             }
  41.         }
  42.     }
  43. }
  44.  
  45.  
  46. void process(){
  47.     visited[1] = true;
  48.     Try(1,0);
  49.     cout << res;
  50. }
  51.  
  52. int main(){
  53.     ios_base::sync_with_stdio(0);
  54.     cin.tie(NULL);cout.tie(NULL);
  55.     init();
  56.     process();
  57.  
  58.     return 0;
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement