結果

問題 No.1480 Many Complete Graphs
ユーザー jupiro
提出日時 2021-04-16 22:51:34
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 73 ms / 2,000 ms
コード長 2,128 bytes
コンパイル時間 1,291 ms
コンパイル使用メモリ 123,980 KB
最終ジャッジ日時 2025-01-20 20:27:56
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 57
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <utility>
#include <tuple>
#include <cmath>
#include <numeric>
#include <set>
#include <map>
#include <array>
#include <complex>
#include <iomanip>
#include <cassert>
#include <random>
using ll = long long;
using std::cin;
using std::cout;
using std::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; }
const int inf = (int)1e9 + 7;
const long long INF = 1LL << 60;

namespace KKT89
{
  std::vector<ll> dijkstra(int start, std::vector<std::vector<std::pair<int, ll>>>& graph)
  {
    std::vector<ll> dist(graph.size(), INF); dist[start] = 0;
    std::priority_queue<std::pair<ll, int>, std::vector<std::pair<ll, int>>, std::greater<std::pair<ll, int>>> pq;
    pq.emplace(0, start);
    while (!pq.empty()) {
      ll cost; int idx;
      std::tie(cost, idx) = pq.top();
      pq.pop();
      if (dist[idx] < cost) continue;
      for (auto next : graph[idx]) 
        if (chmin(dist[next.first], cost + next.second))
          pq.emplace(dist[next.first], next.first);
    }
    return dist;
  }
}


void solve()
{
  int n, m; cin >> n >> m;
  std::vector<std::vector<std::pair<int, ll>>> g(n + m * 2);
  for (int i = 0; i < m; ++i)
  {
    int k; cin >> k;
    const int u = n + i * 2;
    const int v = u + 1;
    g[u].emplace_back(v, 1);
    g[v].emplace_back(u, 1);
    ll c; cin >> c;
    for (int i = 0; i < k; ++i)
    {
      int s; cin >> s;
      s -= 1;
      if(s & 1)
      {
        g[v].emplace_back(s, s + 1 + c);
        g[s].emplace_back(v, s + 1 + c);
      }
      else
      {
        g[u].emplace_back(s, s + 1 + c);
        g[s].emplace_back(u, s + 1 + c);
      }
    }
  }
  ll res = KKT89::dijkstra(0, g)[n - 1];
  if(res == INF)
  {
    cout << -1 << "\n";
    return;
  }
  res /= 2;
  cout << res << "\n";
}
int main()
{
  std::cin.tie(nullptr);
  std::ios::sync_with_stdio(false);

  int kkt = 1;
  //cin >> kkt;
  while(kkt--)
    solve();
  return 0;
}
0