Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # include <bits/stdc++.h>
- using namespace std;
- typedef long long ll;
- typedef pair<ll, ll> pl;
- typedef vector<ll> vl;
- ///these are some Macros which you can use in your code too
- # define fast ios_base::sync_with_stdio(false);cin.tie(NULL);
- # define MOD 1000000007
- # define endl '\n'
- # define INF 9e18
- # define lb lower_bound
- # define ub upper_bound
- # define pb push_back
- # define fi first
- # define se second
- # define all(a) a.begin(), a.end()
- bool check(string s, ll k, vector<vl>& t)
- {
- //time matrix
- ll sum = t[1][s[0] - 48];
- for (int i = 1; i < s.length(); i++)
- {
- sum += t[s[i - 1] - 48][s[i] - 48];
- }
- int n = s.length();
- sum += t[s[n - 1] - 48][1];
- if (sum == k)return true;
- return false;
- }
- int main()
- {
- fast;
- ll tt = 1;
- // cin >> tt;
- while (tt--)
- {
- ll n, k, i, j, count = 0;
- cin >> n >> k;
- //2d matrix
- //int a[10][10]; array declaration
- //2d vector
- //vector<int> v(n+1,-1) vector declaration
- vector<vector<ll>> t(n + 1, vector<ll> (n + 1, 0));
- for (i = 1; i <= n; i++)
- {
- for (j = 1; j <= n; j++)
- {
- ll x; cin >> x; t[i][j] = x;
- }
- }
- //N (1,2,3,4..) K ()
- //N*N
- //t[i][j]->time taken to reach city j from city i
- //t[i][j]->(i+1)th city to (j+1)th city
- string s;
- for (i = 2; i <= n; i++)
- {
- s.push_back(i + 48);//here (i+48) means 50 if i==2
- //and pushing 50 in the string means converting to
- //the character whose ascii value is 50
- //s.push_back(i);
- }
- //n=3 s(2,3)
- // string s;
- // s.push_back(i)
- //cout<<s[0]<<endl;
- sort(s.begin(), s.end());
- //s ->2,3,4
- do
- {
- bool cal = check(s, k, t);
- if (cal) count++;
- }
- while (next_permutation(s.begin(), s.end()));
- cout << count << endl;
- //time complexity_>O(n*n)+O(n!)_>O(n!)
- //space complexity_>O(n*n)+O(n-1)_>O(n*n)
- }
- return 0;
- }
- /*
- //stl representation of the function
- do
- {
- }
- while(next_permutation(s.begin(),s.end()));
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement