結果

問題 No.1480 Many Complete Graphs
ユーザー firiexp
提出日時 2021-04-16 22:55:45
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 158 ms / 2,000 ms
コード長 2,237 bytes
コンパイル時間 1,740 ms
コンパイル使用メモリ 110,572 KB
最終ジャッジ日時 2025-01-20 20:30:58
ジャッジサーバーID
(参考情報)
judge5 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 57
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:74:18: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   74 |             scanf("%d", &x);
      |             ~~~~~^~~~~~~~~~

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>

static const int MOD = 1000000007;
using ll = long long;
using u32 = unsigned;
using u64 = unsigned long long;
using namespace std;

template<class T> constexpr T INF = ::numeric_limits<T>::max() / 32 * 15 + 208;

template <typename T>
struct edge {
    int from, to; T cost;
    edge(int to, T cost) : from(-1), to(to), cost(cost) {}
    edge(int from, int to, T cost) : from(from), to(to), cost(cost) {}
};

template <typename T>
vector<T> dijkstra(int s,vector<vector<edge<T>>> &G){
    auto n = G.size();
    vector<T> d(n, INF<T>);
    priority_queue<pair<T, int>,vector<pair<T, int>>,greater<>> Q;
    d[s] = 0;
    Q.emplace(0, s);
    while(!Q.empty()){
        T cost; int i;
        tie(cost, i) = Q.top(); Q.pop();
        if(d[i] < cost) continue;
        for (auto &&e : G[i]) {
            auto cost2 = cost + e.cost;
            if(d[e.to] <= cost2) continue;
            d[e.to] = cost2;
            Q.emplace(d[e.to], e.to);
        }
    }
    return d;
}


template <class T>
ostream& operator<<(ostream& os, vector<T> v) {
    os << "{";
    for (int i = 0; i < v.size(); ++i) {
        if(i) os << ", ";
        os << v[i];
    }
    return os << "}";
}

template <class L, class R>
ostream& operator<<(ostream& os, pair<L, R> p) {
    return os << "{" << p.first << ", " << p.second << "}";
}


int main() {
    int n, m;
    cin >> n >> m;
    vector<vector<edge<ll>>> G(n+2*m);
    int id = n;
    for (int i = 0; i < m; ++i) {
        int k, c;
        cin >> k >> c;
        for (int j = 0; j < k; ++j) {
            int x;
            scanf("%d", &x);
            if(x&1) {
                G[x-1].emplace_back(id, x/2+1);
                G[id].emplace_back(x-1, x/2+c);
                G[id+1].emplace_back(x-1, x/2+c+1);
            } else {
                G[x-1].emplace_back(id+1, x/2);
                G[id].emplace_back(x-1, x/2+c);
                G[id+1].emplace_back(x-1, x/2+c);
            }
        }
        id += 2;
    }
    ll ans = dijkstra(0, G)[n-1];
    if(ans == INF<ll>) puts("-1");
    else cout << ans << "\n";
    return 0;
}
0