結果

問題 No.1480 Many Complete Graphs
ユーザー 蜜蜂
提出日時 2021-03-04 20:15:38
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 416 ms / 2,000 ms
コード長 1,471 bytes
コンパイル時間 1,939 ms
コンパイル使用メモリ 177,552 KB
実行使用メモリ 16,372 KB
最終ジャッジ日時 2024-07-03 04:04:05
合計ジャッジ時間 10,533 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 56 RE * 1
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function 'int main()':
main.cpp:69:10: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17' [-Wc++17-extensions]
   69 |     auto [now,dist] = que.top();
      |          ^
main.cpp:79:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17' [-Wc++17-extensions]
   79 |     for(auto [next,dist2] : graph[now]){
      |              ^

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using ld = long double;
#define fi first
#define se second
#define pb push_back

using T = pair<int,ll>;

int main(){
  int n,m;
  cin>>n>>m;
  
  assert(1<=n&&n<=1e5);
  assert(0<=m&&m<=1e5);

  vector<vector<T>> graph(n+2*m);
  vector<ll> ans(n+2*m,1e18);
  ans[0]=0;
  
  
  int ksum=0;
  
  for(int i=0;i<m;i++){
    int k;
    ll c;
    cin>>k>>c;
    
    ksum+=k;
    assert(1<=k&&k<=n);
    assert(1<=c&&c<=1e12);
    
    graph[n+2*i].pb({n+2*i+1,1});
    graph[n+2*i+1].pb({n+2*i,1});
    
    int si[k];
    
    for(int j=0;j<k;j++){
      int s;
      cin>>s;
      
      assert(1<=s&&s<=n);
      si[j]=s;
      
      if(s%2==1){
        graph[s-1].pb({n+2*i,s+c});
        graph[n+2*i].pb({s-1,s+c});
      }
      else{
        graph[s-1].pb({n+2*i+1,s+c});
        graph[n+2*i+1].pb({s-1,s+c});
      }
    }
    
    
    for(int j=1;j<k;j++){
      assert(si[j-1]<si[j]);
    }
  }
  
  assert(ksum<=1e5);

  priority_queue<T,vector<T>,greater<T>> que;
  que.push({0,0});

  while(!que.empty()){
    auto [now,dist] = que.top();
    que.pop();

    if(dist>ans[now]){
      continue;
    }
    if(graph[now].size()==0){
      continue;
    }
    
    for(auto [next,dist2] : graph[now]){
      if(ans[now]+dist2<ans[next]){
        que.push({next,dist2});
        ans[next]=ans[now]+dist2;
      }
    }
  }
  
  if(ans[n-1]>=1e18){
    ans[n-1]=-2;
  }
  cout<<ans[n-1]/2<<endl;
}
0