记录编号 346966 评测结果 AAAAAAAA
题目名称 王伯买鱼 最终得分 100
用户昵称 GravatarAntiLeaf 是否通过 通过
代码语言 C++ 运行时间 0.053 s
提交时间 2016-11-12 17:43:44 内存使用 0.31 MiB
显示代码纯文本
  1. #include<cstdio>
  2. #include<cstring>
  3. #include<algorithm>
  4. #include<vector>
  5. using namespace std;
  6. const int maxn=35;
  7. void dfs(int,int,int);
  8. int n,m,x,y,w[maxn],ans=0,tmp=0;
  9. bool G[maxn][maxn]={{false}};
  10. vector<int>a,b;
  11. int main(){
  12. #define MINE
  13. #ifdef MINE
  14. freopen("fish.in","r",stdin);
  15. freopen("fish.out","w",stdout);
  16. #endif
  17. scanf("%d%d",&m,&n);
  18. for(int i=1;i<=n;i++){
  19. scanf("%d",&x);
  20. scanf("%d",&w[x]);
  21. }
  22. while(scanf("%d%d",&x,&y)==2&&x&&y)G[x][y]=G[y][x]=true;
  23. dfs(1,0,0);
  24. printf("%d %d\n",ans,tmp);
  25. for(int i=0;i<ans;i++)printf("%d\n",b[i]);
  26. #ifndef MINE
  27. printf("\n-------------------------DONE-------------------------\n");
  28. for(;;);
  29. #endif
  30. return 0;
  31. }
  32. void dfs(int x,int cnt,int cost){
  33. if(cost>m)return;
  34. if(x>n){
  35. if(cnt>ans){
  36. ans=cnt;
  37. tmp=cost;
  38. b=a;
  39. }
  40. else if(cnt==ans&&cost>tmp){
  41. tmp=cost;
  42. b=a;
  43. }
  44. return;
  45. }
  46. if(n-x+cnt+1<ans)return;
  47. bool ok=true;
  48. for(int i=0;i<cnt;i++)if(G[a[i]][x]){ok=false;break;}
  49. if(ok){
  50. a.push_back(x);
  51. dfs(x+1,cnt+1,cost+w[x]);
  52. a.pop_back();
  53. }
  54. dfs(x+1,cnt,cost);
  55. }