// Problem: G. Shuffling Songs // Contest: Codeforces - Codeforces Round 937 (Div. 4) // URL: https://codeforces.com/contest/1950/problem/G // Memory Limit: 256 MB // Time Limit: 3000 ms // // Powered by CP Editor (https://cpeditor.org)
#include <bits/stdc++.h> using namespace std; #define ll long long //# define int long long #define ull unsigned long long #define pii pair<int,int>
#define baoliu(x, y) cout << fixed << setprecision(y) << x #define endl "\n" #define debug1(x) cerr<<x<<" " #define debug2(x) cerr<<x<<endl const int N = 2e5 + 10; const int M = 1e6 + 10; const int inf = 0x3f3f3f3f; const int mod = 998244353; const double eps = 1e-8; int n, m; int a[N];
// Problem: P1171 售货员的难题 // Contest: Luogu // URL: https://www.luogu.com.cn/problem/P1171 // Memory Limit: 125 MB // Time Limit: 1000 ms // // Powered by CP Editor (https://cpeditor.org)
#include <bits/stdc++.h> using namespace std; #define ll long long //# define int long long #define ull unsigned long long #define pii pair<int,int>
#define baoliu(x, y) cout << fixed << setprecision(y) << x #define endl "\n" #define debug1(x) cerr<<x<<" " #define debug2(x) cerr<<x<<endl const int N =21 ; const int M = 1e6 + 10; const int inf = 0x3f3f3f3f; const int mod = 998244353; const double eps = 1e-8; int n, m; int a[N];
int w[N][N]; //首先思考为什么这里最短路不能用普通的最短路 //首先它需要把所有点都遍历到,而普通最短路根本不在乎这个 int dp[1<<20][N]; void solve(){ cin>>n; for(int i=0;i<n;i++){ for(int j=0;j<n;j++){ cin>>w[i][j]; } }
memset(dp,0x3f ,sizeof dp); dp[1][0]=0; for(int i=0;i<(1<<n);i++){ for(int j=0;j<n;j++){//枚举当前终点 if(((i>>j)&1)==0)continue; for(int k=0;k<n;k++){//枚举上一次转移点 if(((i>>k)&1)==0)continue; int pre=i-(1<<j); if(k==j)continue; dp[i][j]=min(dp[i][j],dp[pre][k]+w[k][j]); } } } int ed=(1<<n)-1; int ans=inf; for(int i=1;i<n;i++){ ans=min(ans,dp[ed][i]+w[i][0]); } cout<<ans<<endl; }
int main() { cin.tie(0); ios::sync_with_stdio(false);
int t; //cin>>t; t=1; while (t--) { solve(); } return 0; }