QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#424513#6437. Paimon's TreethisisatestWA 810ms4508kbC++236.1kb2024-05-29 11:43:282024-05-29 11:43:28

Judging History

你现在查看的是最新测评结果

  • [2024-05-29 11:43:28]
  • 评测
  • 测评结果:WA
  • 用时:810ms
  • 内存:4508kb
  • [2024-05-29 11:43:28]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;


long long inf = numeric_limits<long long>::max()/3;


int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    //ifstream cin("input.txt");
    long long t;
    cin>>t;
    while (t--){
        long long n;
        cin>>n;
        vector<long long> nums(n);
        for (long long i=0;i<n;i++){
            long long num;
            cin>>num;
            nums[i] = num;
        }

        vector<vector<long long>> adj_matrix(n+1,vector<long long>(n+1,inf));
        vector<vector<long long>> adj_list(n+1);
        for (long long i=0;i<=n;i++){
            adj_matrix[i][i] = 0;
        }

        for (long long i=0;i<n;i++){
            long long a,b;
            cin>>a>>b;
            adj_matrix[a-1][b-1] = 1;
            adj_matrix[b-1][a-1] = 1;
            adj_list[a-1].push_back(b-1);
            adj_list[b-1].push_back(a-1);
        }

        if (n==1){
            cout<<nums[0]<<"\n";
            continue;
        }
        for (long long k=0;k<=n;k++){
            for (long long i=0;i<=n;i++){
                for (long long j=0;j<=n;j++){
                    if (adj_matrix[i][k] + adj_matrix[k][j] < adj_matrix[i][j]){
                        adj_matrix[i][j] = adj_matrix[i][k] + adj_matrix[k][j];
                    }
                }
            }
        }

        vector<vector<long long>> garbage3(n+1,vector<long long>(n+1));
        vector<vector<long long>> garbage2(n+1,vector<long long>(n+1));
        vector<vector<long long>> garbage1(n+1,vector<long long>(n+1));
        for (long long i=0;i<=n;i++){
            for (long long j=0;j<=n;j++){
                for (long long k=0;k<=n;k++){
                    if (adj_matrix[i][k]+adj_matrix[j][k]!=adj_matrix[i][j] && abs(adj_matrix[i][k]-adj_matrix[j][k])!=adj_matrix[i][j]){
                        garbage3[i][j]++;
                    }
                    if (adj_matrix[i][k]+adj_matrix[j][k]!=adj_matrix[i][j] && adj_matrix[k][j]-adj_matrix[k][i]!=adj_matrix[i][j]){
                        garbage2[i][j]++;
                    }
                    if (adj_matrix[i][k]+adj_matrix[j][k]!=adj_matrix[i][j] && adj_matrix[k][i]-adj_matrix[k][j]!=adj_matrix[i][j]){
                        garbage1[i][j]++;
                    }
                }
            }
        }
        //index in nums, left, right, mask
        //0: no expansion, 1: expand to left, 2: expand to right, 3: expand to both
        vector<vector<vector<vector<long long>>>> dp(n+1,vector<vector<vector<long long>>>(n+1,vector<vector<long long>>(n+1,vector<long long>(4))));

        for (long long i=0;i<n;i++){
            for (long long j=0;j<=n;j++){
                for (long long k=0;k<=n;k++){
                    for (long long mask=0;mask<=3;mask++){
                        if (dp[i][j][k][mask]==0 && adj_matrix[j][k]!=2){
                            continue;
                        }
                        
                        if (mask==3){
                            dp[i+1][j][k][1] = max(dp[i+1][j][k][1],dp[i][j][k][3] + nums[i]);
                            dp[i+1][j][k][2] = max(dp[i+1][j][k][2],dp[i][j][k][3] + nums[i]);
                        }else if (mask==2){
                            dp[i+1][j][k][0] = max(dp[i+1][j][k][0],dp[i][j][k][2] + nums[i]);
                        }else if (mask==1){
                            dp[i+1][j][k][0] = max(dp[i+1][j][k][0],dp[i][j][k][1] + nums[i]);
                        }


                        if (mask==0){                 
                            dp[i+1][j][k][0] = max(dp[i+1][j][k][0],dp[i][j][k][0]);
                        }else if (mask==1){
                            if (garbage1[j][k]>=i-adj_matrix[j][k]+2){
                                dp[i+1][j][k][1] = max(dp[i+1][j][k][1],dp[i][j][k][1]);
                            }
                            for (long long child:adj_list[j]){
                                if (adj_matrix[j][child]+adj_matrix[child][k]!=adj_matrix[j][k]){
                                    dp[i+1][child][k][1] = max(dp[i+1][child][k][1],dp[i][j][k][1] + nums[i]);
                                }
                            }
                        }else if (mask==2){
                            if (garbage2[j][k]>=i-adj_matrix[j][k]+2){
                                dp[i+1][j][k][2] = max(dp[i+1][j][k][2],dp[i][j][k][2]);
                            }
                            
                            for (long long child:adj_list[k]){
                                if (adj_matrix[j][child]+adj_matrix[child][k]!=adj_matrix[j][k]){
                                    dp[i+1][j][child][2] = max(dp[i+1][j][child][2],dp[i][j][j][2] + nums[i]);
                                }
                            }
                        }else{
                            if (garbage3[j][k]>=i-adj_matrix[j][k]+3){
                                dp[i+1][j][k][3] = max(dp[i+1][j][k][3],dp[i][j][k][3]);
                            }
                            for (long long child:adj_list[j]){
                                if (adj_matrix[j][child]+adj_matrix[child][k]!=adj_matrix[j][k]){
                                    dp[i+1][child][k][3] = max(dp[i+1][child][k][3],dp[i][j][k][3] + nums[i]);
                                }
                            }
                            for (long long child:adj_list[k]){
                                if (adj_matrix[j][child]+adj_matrix[child][k]!=adj_matrix[j][k]){
                                    dp[i+1][j][child][3] = max(dp[i+1][j][child][3],dp[i][j][j][3] + nums[i]);
                                }
                            }
                        }
                        //cout<<i<<" "<<j<<" "<<k<<" "<<mask<<" "<<dp[i][j][k][mask]<<endl;
                    }
                }
            }
        }


        long long answer = 0;
        for (long long i=0;i<=n;i++){
            for (long long j=0;j<=n;j++){
                answer = max(dp[n][i][j][0],answer);
            }
        }
        cout<<answer<<"\n";
    }
    return 0;
}

详细

Test #1:

score: 100
Accepted
time: 1ms
memory: 3780kb

input:

2
5
1 7 3 5 4
1 3
2 3
3 4
4 5
4 6
1
1000000000
1 2

output:

16
1000000000

result:

ok 2 number(s): "16 1000000000"

Test #2:

score: -100
Wrong Answer
time: 810ms
memory: 4508kb

input:

5000
19
481199252 336470888 634074578 642802746 740396295 773386884 579721198 396628655 503722503 971207868 202647942 2087506 268792718 46761498 443917727 16843338 125908043 691952768 717268783
9 4
4 18
12 9
10 9
4 1
6 12
2 18
9 13
6 14
4 8
2 3
10 17
2 20
19 20
5 1
12 15
15 16
4 7
17 11
4
240982681 ...

output:

5750811120
1896999359
4208559611
4140156713
5361004844
1875024569
3690026656
3702623113
3412485417
7807375141
5341435147
2355946110
3090496776
5626636202
4729664767
2207592767
572868833
4759005973
2944749369
2538044586
3083947956
5757497518
1421427135
3971435093
1197051728
396588615
251138097
221986...

result:

wrong answer 48th numbers differ - expected: '5317528311', found: '5514819848'