結果

問題 No.1480 Many Complete Graphs
ユーザー miyo2580miyo2580
提出日時 2024-01-09 00:01:45
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 3,712 bytes
コンパイル時間 2,813 ms
コンパイル使用メモリ 215,228 KB
実行使用メモリ 23,392 KB
最終ジャッジ日時 2024-01-09 00:01:56
合計ジャッジ時間 10,534 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
10,032 KB
testcase_01 AC 5 ms
10,032 KB
testcase_02 AC 5 ms
10,036 KB
testcase_03 AC 5 ms
10,032 KB
testcase_04 AC 5 ms
10,032 KB
testcase_05 AC 5 ms
10,032 KB
testcase_06 AC 4 ms
10,032 KB
testcase_07 AC 5 ms
10,032 KB
testcase_08 AC 5 ms
10,056 KB
testcase_09 AC 6 ms
10,208 KB
testcase_10 AC 5 ms
10,264 KB
testcase_11 AC 5 ms
10,108 KB
testcase_12 AC 5 ms
10,144 KB
testcase_13 AC 18 ms
13,360 KB
testcase_14 AC 14 ms
12,592 KB
testcase_15 AC 28 ms
14,388 KB
testcase_16 AC 17 ms
12,348 KB
testcase_17 AC 18 ms
12,440 KB
testcase_18 AC 7 ms
10,504 KB
testcase_19 AC 18 ms
12,640 KB
testcase_20 AC 27 ms
14,356 KB
testcase_21 AC 21 ms
14,280 KB
testcase_22 AC 13 ms
12,528 KB
testcase_23 AC 39 ms
18,456 KB
testcase_24 AC 41 ms
19,328 KB
testcase_25 AC 53 ms
19,136 KB
testcase_26 AC 55 ms
19,084 KB
testcase_27 AC 40 ms
19,288 KB
testcase_28 AC 51 ms
19,308 KB
testcase_29 AC 52 ms
19,304 KB
testcase_30 AC 49 ms
19,308 KB
testcase_31 AC 50 ms
19,308 KB
testcase_32 AC 50 ms
19,304 KB
testcase_33 AC 49 ms
19,308 KB
testcase_34 AC 49 ms
19,312 KB
testcase_35 AC 48 ms
19,308 KB
testcase_36 AC 51 ms
19,308 KB
testcase_37 AC 48 ms
19,312 KB
testcase_38 AC 48 ms
19,308 KB
testcase_39 AC 47 ms
19,312 KB
testcase_40 AC 49 ms
19,316 KB
testcase_41 AC 50 ms
19,308 KB
testcase_42 AC 49 ms
19,312 KB
testcase_43 AC 50 ms
19,308 KB
testcase_44 AC 50 ms
19,308 KB
testcase_45 AC 51 ms
19,308 KB
testcase_46 AC 51 ms
19,308 KB
testcase_47 RE -
testcase_48 RE -
testcase_49 RE -
testcase_50 RE -
testcase_51 RE -
testcase_52 AC 81 ms
19,704 KB
testcase_53 AC 80 ms
19,708 KB
testcase_54 AC 85 ms
19,704 KB
testcase_55 AC 81 ms
19,700 KB
testcase_56 AC 85 ms
19,712 KB
testcase_57 RE -
testcase_58 AC 68 ms
23,392 KB
evil_aftercontest.txt RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define repd(i,a,b) for (ll i=(a);i<(b);i++)
#define rep(i,n) repd(i,0,n)
#define all(x) (x).begin(),(x).end()
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
typedef long long ll;
typedef pair<ll,ll> P;
typedef vector<ll> vec;
using Graph = vector<vector<ll>>;
const long long INF = 1LL<<60;
const long long MOD = 1000000007;

// Lowest Common Ancestor by binary lifting
// https://youtu.be/8uowVvQ_-Mo?t=4306
template<typename T> // T: type of cost
struct lca {
  int n, root, l;
  vector<vector<int>> to;
  vector<vector<T>> co;
  vector<int> dep;
  vector<T> costs;
  vector<vector<int>> par;
  lca(int n):n(n),to(n),co(n),dep(n),costs(n) {
    l = 0;
    while ((1<<l) < n) ++l;
    par = vector<vector<int>>(n+1,vector<int>(l,n));
  }
  void addEdge(int a, int b, T c=0) {
    to[a].push_back(b); co[a].push_back(c);
    to[b].push_back(a); co[b].push_back(c);
  }
  void dfs(int v, int d=0, T c=0, int p=-1) {
    if (p != -1) par[v][0] = p;
    dep[v] = d;
    costs[v] = c;
    for (int i = 0; i < to[v].size(); ++i) {
      int u = to[v][i];
      if (u == p) continue;
      dfs(u, d+1, c+co[v][i], v);
    }
  }

  void init(int _root=0) {
    root = _root;
    dfs(root);
    for (int i = 0; i < l-1; ++i) {
      for (int v = 0; v < n; ++v) {
        par[v][i+1] = par[par[v][i]][i];
      }
    }
  }
  // LCA
  int LCA(int a, int b) {
    if (dep[a] > dep[b]) swap(a,b);
    int gap = dep[b]-dep[a];
    for (int i = l-1; i >= 0; --i) {
      int len = 1<<i;
      if (gap >= len) {
        gap -= len;
        b = par[b][i];
      }
    }
    if (a == b) return a;
    for (int i = l-1; i >= 0; --i) {
      int na = par[a][i];
      int nb = par[b][i];
      if (na != nb) a = na, b = nb;
    }
    return par[a][0];
  }
  int length(int a, int b) {
    int c = LCA(a,b);
    return dep[a]+dep[b]-dep[c]*2;
  }
  T dist(int a, int b) {
    int c = LCA(a,b);
    return costs[a]+costs[b]-costs[c]*2;
  }
};

struct edge { ll to, cost; };   //辺
int V;   //頂点数
const int MAX_V = 200000;
vector<vector<edge>> G(MAX_V);
ll d[MAX_V];
ll memo[10][MAX_V];

void dijkstra(int s) {
    //greater<P>でfirstが小さい順に取り出せる
    //first:最短距離,second:頂点番号
    priority_queue<P, vector<P>, greater<P>> que;
    fill(d, d + V, 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;
        rep(i, G[v].size()) {
            edge e = G[v][i];
            if (d[e.to] > d[v] + e.cost) {
                d[e.to] = d[v] + e.cost;
                que.push(P(d[e.to], e.to));
            }
        }
    }
}

int main()
{  
    ios::sync_with_stdio(false);
    cin.tie(0);
    ll n,m;
    cin>>n>>m;
    V=n+4*m+1;
    rep(i,m){
      ll k;cin>>k;
      ll c;cin>>c;
      vec s,t;
      rep(j,k){
        ll x;cin>>x;
        if(x%2==0)s.push_back(x);
        else t.push_back(x);
      }
      ll idx=n+4*i+1;
      rep(j,s.size()){
        G[s[j]].push_back((edge){idx,c+s[j]/2});
        G[s[j]].push_back((edge){idx+2,c+s[j]/2});
        G[idx].push_back((edge){s[j],s[j]/2});
        G[idx+3].push_back((edge){s[j],1+s[j]/2});
      }
      rep(j,t.size()){
        G[t[j]].push_back((edge){idx+1,c+t[j]/2});
        G[t[j]].push_back((edge){idx+3,c+t[j]/2});
        G[idx+1].push_back((edge){t[j],1+t[j]/2});
        G[idx+2].push_back((edge){t[j],1+t[j]/2});
      }
    }
    dijkstra(1);
    if(d[n]>=INF)d[n]=-1;
    cout<<d[n]<<endl;
    return 0;
}
0