Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
题目太BT 了,差不多的代码,一个AC , 一个TLE,另附有N=20的测试数据TLE的代码 #include <stdio.h> int n,a[22][22],max; bool use[22]; int power() //就这个函数不一样而已 { int i,j,ans=0; for(i=1;i<=n;i++) { if(use[i]) continue; for(j=1;j<=n;j++) { if(i==j||use[j]==0) continue; ans+=a[i][j]; } } return ans; } void dfs(int p) { if(p>n) { if(max<power()) max=power(); return ; } use[p]=1; dfs(p+1); use[p]=0; dfs(p+1); } int main() { int i,j; scanf("%d",&n); for(i=0;i<22;i++) use[i]=0; for(i=1;i<=n;i++) for(j=1;j<=n;j++) scanf("%d",&a[i][j]); max=0; dfs(1); printf("%d\n",max); return 0; } /* 20 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 */ AC了的 如果你是大牛的话,告诉我为什么??????????????????? #include <stdio.h> int n,a[22][22],max; bool use[22]; int power() { int i,j,ans=0; for(i=1;i<=n;i++) { if(use[i]) for(j=1;j<=n;j++) { if(use[j]==0) ans+=a[i][j]; } } return ans; } void dfs(int p) { if(p>n) { if(max<power()) max=power(); return ; } use[p]=1; dfs(p+1); use[p]=0; dfs(p+1); } int main() { int i,j; scanf("%d",&n); for(i=0;i<22;i++) use[i]=0; for(i=1;i<=n;i++) for(j=1;j<=n;j++) scanf("%d",&a[i][j]); max=0; dfs(1); printf("%d\n",max); return 0; } Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator