BZOJ 2165 大樓 倍增Floyd,bzoj2165

來源:互聯網
上載者:User

BZOJ 2165 大樓 倍增Floyd,bzoj2165

題目大意:給定一張圖,求從1開始到達m的權值至少需要遍曆多少條邊

n<=100,果斷倍增Floyd

f[temp][i][j]表示經過2^temp條邊從i走到j的最大權值

更新時f[temp[i][j]=max{f[temp-1][i][k]+f[temp-1][k][j]}

然後用矩陣g[i][j]記錄當前走的權值,初始主對角線為0,其餘為-∞

從大到小枚舉temp,利用f[temp]和g得到矩陣h

如果h中1到某個點的權值超過了m就跳出 否則將當前temp計入ans 然後將g數組更新為h

最後輸出ans+1就是答案 證明略

#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace std;int n;long long f[70][110][110],g[110][110],h[110][110],m,ans;void Initialize(){ans=0;memset(f,0xef,sizeof f);memset(g,0xef,sizeof g);}int main(){int T,i,j,k,temp;for(cin>>T;T;T--){Initialize();cin>>n>>m;for(i=1;i<=n;i++)for(j=1;j<=n;j++){#ifndef ONLINE_JUDGEscanf("%I64d",&f[0][i][j]);#elsescanf("%lld",&f[0][i][j]);#endifif(!f[0][i][j])f[0][i][j]=0xefefefefefefefefll;}try{for(temp=1;1ll<<temp<=m;temp++)for(k=1;k<=n;k++)for(i=1;i<=n;i++)for(j=1;j<=n;j++){f[temp][i][j]=max(f[temp][i][j],f[temp-1][i][k]+f[temp-1][k][j]);if(i==1&&f[temp][i][j]>=m)throw(true);}}catch(bool) {}for(i=1;i<=n;i++)g[i][i]=0;while(temp--){memset(h,0xef,sizeof h);try{for(k=1;k<=n;k++)for(i=1;i<=n;i++)for(j=1;j<=n;j++){h[i][j]=max(h[i][j],f[temp][i][k]+g[k][j]);if(i==1&&h[i][j]>=m) throw(true);}memcpy(g,h,sizeof g);ans+=1ll<<temp;}catch(bool) {}}cout<<ans+1<<endl;}return 0;}//lld!!!!


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.