Advertisement
pdaogu

TSPCOUNT

Oct 10th, 2019
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.33 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define oo 999999999
  3. #define MOD 1000000007
  4. #define MAXN 30
  5.  
  6. int n;
  7. int a[MAXN][MAXN];
  8. int f[MAXN];
  9. int config[MAXN];
  10. int mpath;
  11. int cost;
  12. int eCost;
  13. int count;
  14.  
  15.  
  16. void input () {
  17.     scanf("%d%d", &n, &eCost);
  18.     mpath = oo;
  19.     for (int i = 1; i <= n; ++i) {
  20.         for (int j = 1; j <= n; ++j) {
  21.             scanf("%d", &a[i][j]);
  22.             if (a[i][j] < mpath && a[i][j] != 0) {
  23.                 mpath = a[i][j];
  24.             }
  25.         }
  26.         f[i] = 0;
  27.     }
  28.     cost = 0;
  29.     config[1] = config[n+1] = 1;
  30.     f[1] = 1;
  31.     count = 0;
  32. }
  33.  
  34. bool check (int k, int v) {
  35.     if (f[v]) {
  36.         return false;
  37.     }
  38.     if (cost + a[config[k-1]][v] + mpath * (n+1-k) > eCost) {
  39.         return false;
  40.     }
  41.     return true;
  42. }
  43.  
  44. void update (int uCost) {
  45.     ++count;
  46.     // for (int i = 0; i <= n + 1; ++i)
  47.     //  printf("%d ", config[i]);
  48.     // printf("\n");
  49. }
  50.  
  51. void attempt (int k) {
  52.     for (int v = 2; v <= n; ++v) {
  53.         if (check(k, v)) {
  54.             cost += a[config[k-1]][v];
  55.             config[k] = v;
  56.             f[v] = 1;
  57.             if (k < n) {
  58.                 attempt(k+1);
  59.             } else {
  60.                 if (cost + a[v][1] <= eCost) {
  61.                     update(cost + a[v][0]);
  62.                 }
  63.             }
  64.             f[v] = 0;
  65.             cost -= a[config[k-1]][v];
  66.         }
  67.     }
  68. }
  69.  
  70. void solve () {
  71.     attempt(2);
  72. }
  73.  
  74. void output () {
  75.     printf("%d\n", count);
  76.  
  77. }
  78.  
  79. int main () {
  80.     // freopen("inp", "r", stdin);
  81.     // freopen("out", "w", stdout);
  82.     input();
  83.     solve();
  84.     output();
  85.     return 0;
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement