[ustctraining][13.05.09]H - 数字排列
#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> using namespace std; #define FOR(i,a,b) for(int i=a;i<=b;i++) #define MST(a,b) memset(a,b,sizeof(a)) #define MAXN 12 int X,n,a[MAXN]; int g[MAXN][MAXN]; int ans; int b[MAXN]; void dfs(int k,int pre,int sum) { if(sum>=ans)return; if(k==n+1) { if(sum<ans)ans=sum; return ; } FOR(i,1,n)if(!b[i]) { if(a[i]==0 && k==1)continue; b[i]=1; int t=sum; if(k>1)t=t+g[pre][a[i]]; dfs(k+1,a[i],t); b[i]=0; } } int main() { freopen("in.txt","r",stdin); int nn; scanf("%d",&nn); FOR(ii,1,nn) { scanf("%d",&X); FOR(i,0,9)FOR(j,0,9)scanf("%d",&g[i][j]); ans=1000000*10; n=0; while(X) { a[++n]=X%10; X=X/10; } MST(b,0); dfs(1,0,0); printf("%d\n",ans); } }