結果

問題 No.2039 Copy and Avoid
ユーザー 🍮かんプリン🍮かんプリン
提出日時 2022-08-13 10:03:56
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 44 ms / 2,000 ms
コード長 2,681 bytes
コンパイル時間 1,960 ms
コンパイル使用メモリ 188,616 KB
実行使用メモリ 7,200 KB
最終ジャッジ日時 2023-10-24 19:42:10
合計ジャッジ時間 3,129 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
6,660 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 4 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 1 ms
4,348 KB
testcase_05 AC 4 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 43 ms
6,608 KB
testcase_14 AC 44 ms
7,200 KB
testcase_15 AC 44 ms
6,884 KB
testcase_16 AC 41 ms
6,464 KB
testcase_17 AC 4 ms
4,348 KB
testcase_18 AC 4 ms
4,348 KB
testcase_19 AC 4 ms
4,348 KB
testcase_20 AC 6 ms
4,348 KB
testcase_21 AC 2 ms
4,348 KB
testcase_22 AC 2 ms
4,348 KB
testcase_23 AC 2 ms
4,348 KB
testcase_24 AC 2 ms
4,348 KB
testcase_25 AC 2 ms
4,348 KB
testcase_26 AC 2 ms
4,348 KB
testcase_27 AC 2 ms
4,348 KB
testcase_28 AC 2 ms
4,348 KB
testcase_29 AC 2 ms
4,348 KB
testcase_30 AC 2 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/**
 *   @FileName	a.cpp
 *   @Author	kanpurin
 *   @Created	2022.08.13 10:03:51
**/

#include "bits/stdc++.h" 
using namespace std; 
typedef long long ll;


template<typename T>
struct Dijkstra {
private:
    int V;
    struct edge { int to; T cost; };
    vector<vector<edge>> G;
public:
    const T inf = numeric_limits<T>::max();

    vector<T> d; 

    Dijkstra() {}
    Dijkstra(int V) : V(V) {
        G.resize(V);
    }

    Dijkstra<T>& operator=(const Dijkstra<T>& obj) {
        this->V = obj.V;
        this->G = obj.G;
        this->d = obj.d;
        return *this;
    }

    void add_edge(int from, int to, T weight, bool directed = false) {
        G[from].push_back({to,weight});
        if (!directed) G[to].push_back({from,weight});
    }

    int add_vertex() {
        G.push_back(vector<edge>());
        return V++;
    }

    void build(int s) {
        d.assign(V, inf); 
        typedef tuple<T, int> P; 
        priority_queue<P, vector<P>, greater<P>> pq;
        d[s] = 0; 
        pq.push(P(d[s], s)); 
        while (!pq.empty()) {
            P p = pq.top(); pq.pop();
            int v = get<1>(p);
            if (d[v] < get<0>(p)) continue; 
            for (const edge &e : G[v])
            {
                if (d[e.to] > d[v] + e.cost) {
                    d[e.to] = d[v] + e.cost;
                    pq.push(P(d[e.to], e.to));
                }
            }
        }
    }
};

template<typename T>
vector<T> enum_div(T n) {
    vector<T> ret;
    for (T i = 1; i * i <= n; ++i)
    {
        if (n % i == 0)
        {
            ret.push_back(i);
            if (i * i != n)
            {
                ret.push_back(n / i);
            }
        }
    }
    return ret;
}

int main() {
    int n,m;
    ll a,b;cin >> n >> m >> a >> b;
    vector<int> c(m);
    for (int i = 0; i < m; i++) {
        cin >> c[i];
    }
    auto v = enum_div(n);
    sort(v.begin(), v.end());
    vector<int> d(v.size(),n+1);
    for (int i = 0; i < v.size(); i++) {
        for (int j = 0; j < m; j++) {
            if (c[j]%v[i]!=0) continue;
            d[i] = min(d[i],c[j]);
        }
    }
    Dijkstra<ll> g(v.size());
    for (int i = 1; i < v.size(); i++) {
        if (d[0] <= v[i]) continue;
        g.add_edge(0,i,a*(v[i]-1),true);
    }
    for (int i = 1; i < v.size()-1; i++) {
        for (int j = i+1; j < v.size(); j++) {
            if (v[j] % v[i] != 0) continue;
            if (d[i] <= v[j]) continue;
            g.add_edge(i,j,a*(v[j]/v[i]-1)+b,true);
        }
    }
    g.build(0);
    if (g.d[v.size()-1] == g.inf) {
        cout << -1 << endl;
    }
    else {
        cout << g.d[v.size()-1] << endl;
    }
    return 0;
}
0