标签:
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2068 Accepted Submission(s): 844
#include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <cmath> #define maxn 20 #define inf 0x3f3f3f3f3f3f3f3f using namespace std; int d[1<<maxn]; // int a[maxn]; int n; int Min(int x,int y) { if(x<=y) return x; else return y; } void init() { for(int i=0;i<(1<<maxn);i++) { d[i]=inf; } for(int i=0;i<20;i++) d[(1<<i)]=0; } int dist[maxn][maxn]; int p[maxn]; int pos[maxn]; void work() { for(int s=3;s<(1<<10);s++) { int i,j; for(i=0;i<10;i++) if(s & (1<<i)) { int MIN=inf,ID=-1; for(j=0;j<10;j++) { if(s & (1<<j)) { if(i-j<=0) continue; if(i-j<=MIN) { MIN=i-j; ID=j; } } } if(ID!=-1) d[s]=Min(d[s],d[s ^ (1<<ID)]+dist[i][ID]); } } } void input() { for(int i=0;i<10;i++) { scanf("%d",&a[i]); a[i]--; pos[a[i]]=i; } for(int i=0;i<10;i++) { for(int j=0;j<10;j++) { dist[i][j]=abs(pos[i]-pos[j]); } // cout<<endl; } } int main() { // freopen("test.txt","r",stdin); int t; scanf("%d",&t); while(t--) { init(); input(); // solve(); work(); printf("%d\n",d[(1<<10)-1]); } return 0; }
标签:
原文地址:http://www.cnblogs.com/xianbin7/p/4757960.html