結果
問題 |
No.1480 Many Complete Graphs
|
ユーザー |
![]() |
提出日時 | 2021-05-23 18:40:23 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 200 ms / 2,000 ms |
コード長 | 2,334 bytes |
コンパイル時間 | 2,451 ms |
コンパイル使用メモリ | 124,012 KB |
最終ジャッジ日時 | 2025-01-21 17:49:51 |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 57 |
ソースコード
#include <iostream> #include <algorithm> #include <iomanip> #include <vector> #include <queue> #include <set> #include <map> #include <tuple> #include <cmath> #include <numeric> #include <functional> #include <cassert> #define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl; #define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl; template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } using namespace std; typedef long long ll; const ll INF = 1e+15; typedef pair<ll, int> P; struct edge{ int to; ll cost; }; vector<ll> dijkstra(int s, vector<vector<edge>> G){ priority_queue<P, vector<P>, greater<P>> que; int n = G.size(); vector<ll> d(n, INF); d[s] = 0; que.push(P(0, s)); while(!que.empty()){ P p = que.top();que.pop(); int v = p.second; if(d[v] < p.first) continue; for(int i = 0; i < G[v].size(); i++){ edge e = G[v][i]; if(d[v] + e.cost < d[e.to]){ d[e.to] = d[v] + e.cost; que.push(P(d[e.to], e.to)); } } } return d; } int main(){ ios::sync_with_stdio(false); cin.tie(0); cout << setprecision(10) << fixed; int n, m; cin >> n >> m; auto to_idx = [&](int i, bool s, bool even){ int x = ((s ? 0 : 1) << 1) + (even ? 0 : 1); return n+4*i+x+1; }; vector<vector<edge>> g(n+4*m+1); for(int i = 0; i < m; i++){ int k; cin >> k; ll c; cin >> c; g[to_idx(i, true, false)].push_back(edge{to_idx(i, false, false), c+1}); g[to_idx(i, true, false)].push_back(edge{to_idx(i, false, true), c+1}); g[to_idx(i, true, true)].push_back(edge{to_idx(i, false, false), c+1}); g[to_idx(i, true, true)].push_back(edge{to_idx(i, false, true), c}); for(int j = 0; j < k; j++){ int s; cin >> s; g[s].push_back(edge{to_idx(i, true, s%2 == 0), s/2}); g[to_idx(i, false, s%2 == 0)].push_back(edge{s, s/2}); } } auto d = dijkstra(1, g); if(d[n] == INF){ cout << -1 << endl; }else{ cout << d[n] << endl; } }