记录编号 141605 评测结果 AAAAAAAAAAAAAAAAAAAA
题目名称 [国家集训队2011]大楼 最终得分 100
用户昵称 Gravatarcstdio 是否通过 通过
代码语言 C++ 运行时间 12.311 s
提交时间 2014-12-03 15:58:39 内存使用 16.89 MiB
显示代码纯文本
  1. #include<iostream>
  2. #include<cstdio>
  3. #include<algorithm>
  4. #include<cstring>
  5. using namespace std;
  6. const int SIZEN=101;
  7. typedef long long LL;
  8. int N;
  9. LL M;
  10. class MATRIX{
  11. public:
  12. int n,m;
  13. LL s[SIZEN][SIZEN];
  14. void print(void){cout<<"size: "<<n<<" "<<m<<endl;for(int i=1;i<=n;i++){for(int j=1;j<=m;j++){cout<<s[i][j]<<" ";}cout<<endl;}cout<<endl;}
  15. void clear(int n_,int m_){n=n_,m=m_,memset(s,0,sizeof(s));}
  16. LL mxlen(void){
  17. LL ans=0;
  18. for(int i=1;i<=N;i++) ans=max(ans,s[1][i]);
  19. return ans;
  20. }
  21. };
  22. MATRIX operator * (MATRIX a,MATRIX b){
  23. MATRIX c;
  24. c.n=a.n,c.m=b.m;
  25. for(int i=1;i<=c.n;i++){
  26. for(int j=1;j<=c.m;j++){
  27. c.s[i][j]=0;
  28. for(int k=1;k<=a.m;k++){
  29. if(a.s[i][k]&&b.s[k][j])//0代表无边,不是长度为0的边
  30. c.s[i][j]=max(c.s[i][j],a.s[i][k]+b.s[k][j]);
  31. }
  32. }
  33. }
  34. return c;
  35. }
  36. MATRIX D,now,tmp;
  37. MATRIX P[210]={0};
  38. void work(void){
  39. P[0]=D;
  40. int t;
  41. for(t=0;P[t].mxlen()<M;t++) P[t+1]=P[t]*P[t];
  42. if(t<=1){
  43. printf("%lld\n",1LL<<t);
  44. return;
  45. }
  46. now=P[t-1];//现在已确定2^(t-1)不行,2^t一定行
  47. LL ans=1LL<<(t-1);
  48. for(int i=t-2;i>=0;i--){
  49. tmp=now*P[i];
  50. if(tmp.mxlen()<M){
  51. now=tmp;
  52. ans+=(1LL<<i);
  53. }
  54. }
  55. ans++;
  56. printf("%lld\n",ans);
  57. }
  58. void read(void){
  59. scanf("%d",&N);
  60. scanf("%lld",&M);
  61. D.clear(N,N);
  62. for(int i=1;i<=N;i++){
  63. for(int j=1;j<=N;j++) scanf("%lld",&D.s[i][j]);
  64. }
  65. }
  66. int main(){
  67. freopen("building.in","r",stdin);
  68. freopen("building.out","w",stdout);
  69. int T;
  70. scanf("%d",&T);
  71. while(T--){
  72. read();
  73. work();
  74. }
  75. return 0;
  76. }