結果
問題 | No.1480 Many Complete Graphs |
ユーザー |
![]() |
提出日時 | 2021-04-16 21:19:22 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 156 ms / 2,000 ms |
コード長 | 2,380 bytes |
コンパイル時間 | 998 ms |
コンパイル使用メモリ | 87,408 KB |
実行使用メモリ | 29,696 KB |
最終ジャッジ日時 | 2024-07-03 04:12:43 |
合計ジャッジ時間 | 6,686 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 57 |
ソースコード
#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;}