#include #include #include #include #include using namespace std; const int MAXN = 405; mt19937 randomGenerator(69420); int n; int a[MAXN]; int typeIndex, typeValue; int costIndex[MAXN][MAXN], costValue[MAXN][MAXN]; int aCopy[MAXN]; vector assistance; vector < pair > answer, help; void gen(int index, int i, int j) { if(index==n+1) return; if(index==i || index==j) { gen(index+1, i, j); return; } if(randomGenerator()%100<10) assistance.push_back(index); gen(index+1, i, j); } vector < pair > optimize(int I, int J) { int lastIndex = -1; int minValue = 0, currentValue; vector < pair > answer, current; answer.push_back({I, J}); minValue = costIndex[I][J] + costValue[ a[I] ][ a[J] ]; for(int roll = 0;roll<15;roll++) { assistance.clear(); gen(1, I, J); random_shuffle(assistance.begin(), assistance.end()); /* cout << "assistance: "; for(int i = 0;i=k;i--) { currentValue += costIndex[lastIndex][ assistance[i] ] + costValue[ a[lastIndex] ][ a[ assistance[i] ] ]; current.push_back({lastIndex, assistance[i]}); lastIndex = assistance[i]; } int index1 = I; if(k>0) index1 = assistance[k - 1]; int index2 = J; if(k=0;i--) { currentValue += costIndex[lastIndex][ assistance[i] ] + costValue[ a[lastIndex] ][ a[ assistance[i] ] ]; current.push_back({lastIndex, assistance[i]}); lastIndex = assistance[i]; } currentValue += costIndex[lastIndex][ I ] + costValue[ a[lastIndex] ][ a[ I ] ]; current.push_back({lastIndex, I}); lastIndex = index2; for(int i = k+1;i> n; for(int i = 1;i<=n;i++) { cin >> a[i]; } cin >> typeIndex; for(int i = 1;i<=n;i++) { for(int j = 1;j<=n;j++) { cin >> costIndex[i][j]; } } cin >> typeValue; for(int i = 1;i<=n;i++) { for(int j = 1;j<=n;j++) { cin >> costValue[i][j]; } } for(int i = 1;i<=n;i++) { index = i; minValue = a[i]; for(int j = i+1;j<=n;j++) { if(a[j]