好久没写搜索的题目了 复习一下/。/ 这道题目是暴力的全排列
#include<cstdio> #include<iostream> #include<cstring> #define maxn 31 #define inf 1<<29
using namespace std; int mapp[maxn][maxn],line[8],minn,vis[8]; int k; int mymin(int x,int y) { if(x>y) return y; else return x; } void dfs(int pos,int time,int cost) { if(time>k) { //cout<<minn<<endl; minn=mymin(minn,cost); return; } for(int i=1;i<=k;i++) { if(vis[i]) continue; vis[i]=1; //cout<<i<<' '<<time<<endl; dfs(i,time+1,cost+mapp[line[pos]][line[i]]); vis[i]=0; } } int main() { int t; cin.sync_with_stdio(false); while(cin>>t&&t) { for(int i=0;i<t;i++) for(int j=0;j<t;j++) cin>>mapp[i][j]; cin>>k; for(int i=1;i<=k;i++) cin>>line[i],vis[i]=0; minn=inf; dfs(0,1,0); cout<<minn<<endl; } return 0; }

相关文章:

  • 2021-10-12
  • 2021-06-28
  • 2022-12-23
  • 2021-08-11
  • 2022-12-23
  • 2022-12-23
  • 2021-05-26
  • 2022-02-26
猜你喜欢
  • 2022-01-07
  • 2022-12-23
  • 2022-12-23
  • 2022-02-20
  • 2021-07-19
  • 2021-07-26
  • 2021-04-15
相关资源
相似解决方案