結果

問題 No.1480 Many Complete Graphs
ユーザー se1ka2se1ka2
提出日時 2021-04-16 21:19:22
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 167 ms / 2,000 ms
コード長 2,380 bytes
コンパイル時間 1,003 ms
コンパイル使用メモリ 87,692 KB
実行使用メモリ 29,224 KB
最終ジャッジ日時 2023-09-16 02:53:18
合計ジャッジ時間 7,530 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
8,016 KB
testcase_01 AC 4 ms
8,328 KB
testcase_02 AC 4 ms
8,804 KB
testcase_03 AC 4 ms
8,056 KB
testcase_04 AC 4 ms
8,196 KB
testcase_05 AC 4 ms
8,764 KB
testcase_06 AC 4 ms
8,056 KB
testcase_07 AC 4 ms
8,076 KB
testcase_08 AC 4 ms
8,120 KB
testcase_09 AC 5 ms
8,468 KB
testcase_10 AC 5 ms
8,700 KB
testcase_11 AC 5 ms
8,344 KB
testcase_12 AC 5 ms
8,316 KB
testcase_13 AC 29 ms
14,176 KB
testcase_14 AC 22 ms
12,396 KB
testcase_15 AC 46 ms
15,400 KB
testcase_16 AC 25 ms
11,776 KB
testcase_17 AC 26 ms
11,912 KB
testcase_18 AC 8 ms
9,448 KB
testcase_19 AC 28 ms
13,012 KB
testcase_20 AC 51 ms
15,688 KB
testcase_21 AC 38 ms
15,572 KB
testcase_22 AC 22 ms
12,208 KB
testcase_23 AC 88 ms
22,780 KB
testcase_24 AC 129 ms
24,232 KB
testcase_25 AC 133 ms
24,060 KB
testcase_26 AC 131 ms
24,216 KB
testcase_27 AC 128 ms
24,200 KB
testcase_28 AC 85 ms
23,392 KB
testcase_29 AC 88 ms
22,756 KB
testcase_30 AC 87 ms
23,224 KB
testcase_31 AC 86 ms
23,560 KB
testcase_32 AC 88 ms
22,712 KB
testcase_33 AC 88 ms
22,988 KB
testcase_34 AC 88 ms
22,640 KB
testcase_35 AC 84 ms
22,728 KB
testcase_36 AC 86 ms
22,680 KB
testcase_37 AC 82 ms
22,316 KB
testcase_38 AC 84 ms
22,788 KB
testcase_39 AC 84 ms
22,396 KB
testcase_40 AC 84 ms
22,364 KB
testcase_41 AC 85 ms
23,052 KB
testcase_42 AC 84 ms
22,736 KB
testcase_43 AC 85 ms
22,668 KB
testcase_44 AC 83 ms
22,740 KB
testcase_45 AC 85 ms
22,768 KB
testcase_46 AC 86 ms
23,320 KB
testcase_47 AC 167 ms
26,972 KB
testcase_48 AC 163 ms
27,376 KB
testcase_49 AC 161 ms
26,848 KB
testcase_50 AC 112 ms
26,308 KB
testcase_51 AC 159 ms
27,412 KB
testcase_52 AC 123 ms
26,244 KB
testcase_53 AC 140 ms
25,488 KB
testcase_54 AC 125 ms
25,844 KB
testcase_55 AC 127 ms
26,160 KB
testcase_56 AC 129 ms
25,464 KB
testcase_57 AC 106 ms
29,224 KB
testcase_58 AC 109 ms
28,940 KB
evil_aftercontest.txt AC 221 ms
49,936 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <queue>
using namespace std;
typedef long long ll;

template <typename T>
struct Edge
{
    int to;
    T cost;
};

template <typename T>
struct WeightedGraph
{
    int n;
    std::vector<std::vector<Edge<T>>> g;
    
    WeightedGraph(){}
    
    WeightedGraph(int n) : n(n){
        g.resize(n);
    }
    
    void add_edge(int from, int to, T cost){
        g[from].push_back((Edge<T>){to, cost});
    }
};

template <typename T>
std::vector<T> dijkstra(WeightedGraph<T> &g, int s){
    int n = g.n;
    std::vector<T> d(n);
    fill(d.begin(), d.end(), -2);
    std::priority_queue<std::pair<T, int>, std::vector<std::pair<T, int>>, std::greater<std::pair<T, int>>> que;
    d[s] = 0;
    que.push(std::pair<T, int>(0, s));
    while(que.size()){
        std::pair<T, int> p = que.top();
        que.pop();
        int u = p.second;
        if(d[u] < p.first) continue;
        for(Edge<T> &e : g.g[u]){
            int v = e.to;
            if(d[v] == -2 || d[v] > d[u] + e.cost){
                d[v] = d[u] + e.cost;
                que.push(std::pair<T, int>(d[v], v));
            }
        }
    }
    return d;
}

int main()
{
    int n, m;
    cin >> n >> m;
    int k[100005];
    ll c[100005];
    vector<int> s[100005];
    int l = 0;
    for(int i = 0; i < m; i++){
        cin >> k[i] >> c[i];
        l += k[i];
        s[i].resize(k[i]);
        for(int j = 0; j < k[i]; j++) cin >> s[i][j];
    }
    WeightedGraph<ll> g(l + m * 2 + 2);
    vector<int> v[100005];
    int id = m * 2;
    for(int i = 0; i < m; i++){
        for(int j = 0; j < k[i]; j++){
            v[s[i][j]].push_back(id);
            g.add_edge(id, i * 2, (s[i][j] + 1) / 2 * 2 + c[i]);
            g.add_edge(i * 2, id, (s[i][j] + 1) / 2 * 2 + c[i]);
            if(s[i][j] % 2){
                g.add_edge(id, i * 2 + 1, s[i][j] + c[i]);
                g.add_edge(i * 2 + 1, id, s[i][j] + c[i]);
            }
            if(s[i][j] == 1) g.add_edge(l + m * 2, id, 0);
            if(s[i][j] == n) g.add_edge(id, l + m * 2 + 1, 0);
            id++;
        }
    }
    for(int i = 1; i <= n; i++){
        for(int j = 0; j < (int)v[i].size() - 1; j++){
            g.add_edge(v[i][j], v[i][j + 1], 0);
            g.add_edge(v[i][j + 1], v[i][j], 0);
        }
    }
    vector<ll> d = dijkstra(g, l + m * 2);
    cout << d[l + m * 2 + 1] / 2 << endl;
}
0