It's really simple, just search directly as required. Although I don't know whether the horizontal row represents the work or the vertical column, but the search is still the same.
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<iostream> 2 #include<cstdio> 3 #include<algorithm> 4 #include<cstring> 5 #include<cmath> 6 using namespace std; 7 int i,f,n,o[30][30],ans=200000; 8 bool flag[30]; 9 void dfs(int sum,int now) 10 { 11 if(sum>=ans) return ; 12 if(now==n) 13 { 14 ans=sum; 15 return ; 16 } 17 now++; 18 for(int j=1;j<=n;j++) 19 { 20 if(flag[j]==0) 21 { 22 flag[j]=1; 23 dfs(sum+o[j][now],now); 24 flag[j]=0; 25 } 26 } 27 return; 28 } 29 int main() 30 { 31 ios::sync_with_stdio(false); 32 cin>>n; 33 for(i=1;i<=n;i++) 34 for(f=1;f<=n;f++) 35 cin>>o[i][f]; 36 for(i=1;i<=n;i++) 37 { 38 flag[i]=1; 39 dfs(o[i][1],1); 40 flag[i]=0; 41 } 42 cout<<ans; 43 }
Then the reason for the delay is actually that I am used to making the loop variable global. In this question, the variables inside dfs cannot be changed by their own lower layers. Then there are only a few special cases.
There is also a small fear: if the person who gave the question gave the same data for all working hours, wouldn’t it explode, even if (sum>=ans)return was optimized; the complexity would still be n to the nth power, it’s simply .