Advertisement
maycod23

Untitled

Feb 20th, 2022
729
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.44 KB | None | 0 0
  1. # include   <bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4.  
  5. typedef pair<ll, ll> pl;
  6.  
  7. typedef vector<ll> vl;
  8.  
  9. ///these are some Macros which you can use in your code too
  10. # define    fast                ios_base::sync_with_stdio(false);cin.tie(NULL);
  11. # define    MOD                 1000000007
  12. # define    endl                '\n'
  13. # define    INF                 9e18
  14. # define    lb                  lower_bound
  15. # define    ub                  upper_bound
  16. # define    pb                  push_back
  17. # define    fi                  first
  18. # define    se                  second
  19. # define    all(a)              a.begin(), a.end()
  20. bool check(string s, ll k, vector<vl>& t)
  21. {
  22.     //time matrix
  23.     ll sum = t[1][s[0] - 48];
  24.     for (int i = 1; i < s.length(); i++)
  25.     {
  26.         sum += t[s[i - 1] - 48][s[i] - 48];
  27.     }
  28.     int n = s.length();
  29.     sum += t[s[n - 1] - 48][1];
  30.  
  31.     if (sum == k)return true;
  32.     return false;
  33. }
  34. int main()
  35. {
  36.     fast;
  37.     ll tt = 1;
  38.     // cin >> tt;
  39.     while (tt--)
  40.     {
  41.         ll n, k, i, j, count = 0;
  42.         cin >> n >> k;
  43.         //2d matrix
  44.         //int a[10][10]; array declaration
  45.         //2d vector
  46.         //vector<int> v(n+1,-1) vector declaration
  47.         vector<vector<ll>> t(n + 1, vector<ll> (n + 1, 0));
  48.         for (i = 1; i <= n; i++)
  49.         {
  50.             for (j = 1; j <= n; j++)
  51.             {
  52.                 ll x; cin >> x; t[i][j] = x;
  53.             }
  54.         }
  55.         //N (1,2,3,4..) K ()
  56.         //N*N
  57.         //t[i][j]->time taken to reach city j from city i
  58.         //t[i][j]->(i+1)th city to (j+1)th city
  59.         string s;
  60.         for (i = 2; i <= n; i++)
  61.         {
  62.             s.push_back(i + 48);//here (i+48) means 50 if i==2
  63.             //and pushing 50 in the string means converting to
  64.             //the character whose ascii value is 50
  65.             //s.push_back(i);
  66.         }
  67.         //n=3 s(2,3)
  68.         // string s;
  69.         // s.push_back(i)
  70.         //cout<<s[0]<<endl;
  71.  
  72.  
  73.         sort(s.begin(), s.end());
  74.         //s ->2,3,4
  75.         do
  76.         {
  77.             bool cal = check(s, k, t);
  78.             if (cal) count++;
  79.         }
  80.         while (next_permutation(s.begin(), s.end()));
  81.  
  82.         cout << count << endl;
  83.         //time complexity_>O(n*n)+O(n!)_>O(n!)
  84.         //space complexity_>O(n*n)+O(n-1)_>O(n*n)
  85.     }
  86.     return 0;
  87. }
  88. /*
  89.  
  90. //stl representation of the function
  91. do
  92. {
  93.  
  94. }
  95. while(next_permutation(s.begin(),s.end()));
  96. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement