求简单代码的复杂度,不会求
查看原帖
求简单代码的复杂度,不会求
784813
SakurajiamaMai楼主2024/3/4 11:38

我怀疑是 n4n^4,但是ac了

#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e6+10,mod=1e9+7;
int dp[205][205][205],t[205],res;
signed main()
{
    std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    int n,m; cin>>n>>m;
    for(int i=1;i<=n;i++) cin>>t[i];
    memset(dp,0x3f,sizeof dp);
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++) dp[i][j][j]=0;
    for(int i=1;i<=m;i++){
        int u,v,w; cin>>u>>v>>w;
        u++,v++;
        for(int j=1;j<=n;j++)
            if(u<=j&&v<=j) dp[j][u][v]=w,dp[j][v][u]=w;
    }
    for(int e=1;e<=n;e++)
        for(int k=1;k<=e;k++)
            for(int i=1;i<=e;i++)
                for(int j=1;j<=e;j++)   
                    dp[e][i][j]=min(dp[e][i][j],dp[e][i][k]+dp[e][k][j]);
    int q; cin>>q;
    while(q--){
        int x,y,ti; cin>>x>>y>>ti;
        x++,y++;
        int l=1,r=n;
        while(l<r){
            int mid=l+r+1>>1;
            if(t[mid]<=ti) l=mid;
            else r=mid-1; 
        }
        if(dp[l][x][y]>=0x3f3f3f3f/2) cout<<-1<<endl;
        else cout<<dp[l][x][y]<<endl;
    }
    return 0;   
}

2024/3/4 11:38
加载中...