/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 344.0 KiB

Code

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

int main(){
    int t;
    cin>>t;
    while(t--){
        int n,k; 
        cin>>n>>k; 
        vector<int>a(n);
        for(int i=0; i<n; i++){
            cin>>a[i]; 
        }
        vector<int> m(n, numeric_limits<int>::max());
        m[0]=0;
        using pii = pair<int, int>;
        priority_queue<pii,vector<pii>,greater<pii>>pq;
        pq.push({0, 0});

        while (!pq.empty()){
            int cost=pq.top().first;
            int index=pq.top().second;
            pq.pop();
            if (cost>m[index]) 
            continue;
            if (index+1<n){
                int new_cost=cost+k;
                if(new_cost<m[index+1]){
                    m[index+1]=new_cost;
                    pq.push({new_cost, index+1});
                }
            }

            for(int j=index+1; j<n; j++){
                int move_cost=a[index]/gcd(a[index], a[j]);
                int new_cost=cost+move_cost;
                if(new_cost<m[j]){
                    m[j]=new_cost;
                    pq.push({new_cost, j});
                }
            }
        }
        cout<<m[n-1]<<endl; 
    }
}

Information

Submit By
Type
Pretest
Problem
P1099 Which way to go
Language
C++17 (G++ 13.2.0)
Submit At
2024-10-03 17:13:18
Judged At
2024-10-03 17:13:18
Judged By
Score
10
Total Time
1ms
Peak Memory
344.0 KiB