結果

問題 No.1244 Black Segment
ユーザー beetbeet
提出日時 2020-10-02 22:34:33
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 70 ms / 2,000 ms
コード長 1,907 bytes
コンパイル時間 2,130 ms
コンパイル使用メモリ 207,128 KB
実行使用メモリ 11,900 KB
最終ジャッジ日時 2023-09-24 21:47:30
合計ジャッジ時間 5,101 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,384 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 3 ms
4,380 KB
testcase_14 AC 24 ms
6,320 KB
testcase_15 AC 3 ms
4,380 KB
testcase_16 AC 24 ms
6,144 KB
testcase_17 AC 3 ms
4,380 KB
testcase_18 AC 33 ms
8,960 KB
testcase_19 AC 48 ms
9,636 KB
testcase_20 AC 50 ms
10,136 KB
testcase_21 AC 41 ms
10,328 KB
testcase_22 AC 54 ms
9,944 KB
testcase_23 AC 57 ms
10,364 KB
testcase_24 AC 51 ms
10,340 KB
testcase_25 AC 55 ms
10,752 KB
testcase_26 AC 55 ms
10,004 KB
testcase_27 AC 53 ms
9,612 KB
testcase_28 AC 58 ms
10,556 KB
testcase_29 AC 51 ms
10,324 KB
testcase_30 AC 56 ms
10,736 KB
testcase_31 AC 52 ms
10,088 KB
testcase_32 AC 53 ms
10,236 KB
testcase_33 AC 49 ms
10,332 KB
testcase_34 AC 70 ms
11,356 KB
testcase_35 AC 55 ms
11,900 KB
testcase_36 AC 53 ms
11,368 KB
testcase_37 AC 59 ms
11,600 KB
testcase_38 AC 57 ms
10,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

using Int = long long;
const char newl = '\n';

template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}
template<typename T> void drop(const T &x){cout<<x<<endl;exit(0);}
template<typename T=int>
vector<T> read(size_t n){
  vector<T> ts(n);
  for(size_t i=0;i<n;i++) cin>>ts[i];
  return ts;
}


template<typename T>
struct Dijkstra{
  struct Edge{
    int to;
    T cost;
    Edge(int to,T cost):to(to),cost(cost){}
    bool operator<(const Edge &o)const{return cost>o.cost;}
  };

  vector< vector<Edge> > G;
  vector<T> ds;
  vector<int> bs;
  Dijkstra(int n):G(n){}

  void add_edge(int u,int v,T c){
    G[u].emplace_back(v,c);
  }

  void build(int s){
    int n=G.size();
    ds.assign(n,numeric_limits<T>::max());
    bs.assign(n,-1);

    priority_queue<Edge> pq;
    ds[s]=0;
    pq.emplace(s,ds[s]);

    while(!pq.empty()){
      auto p=pq.top();pq.pop();
      int v=p.to;
      if(ds[v]<p.cost) continue;
      for(auto e:G[v]){
        if(ds[e.to]>ds[v]+e.cost){
          ds[e.to]=ds[v]+e.cost;
          bs[e.to]=v;
          pq.emplace(e.to,ds[e.to]);
        }
      }
    }
  }

  T operator[](int k){return ds[k];}

  vector<int> restore(int to){
    vector<int> res;
    if(bs[to]<0) return res;
    while(~to) res.emplace_back(to),to=bs[to];
    reverse(res.begin(),res.end());
    return res;
  }
};

//INSERT ABOVE HERE
signed main(){
  cin.tie(0);
  ios::sync_with_stdio(0);

  int n,m,a,b;
  cin>>n>>m>>a>>b;
  a--;

  int S=n+1,T=n+2;
  Dijkstra<int> D(n+3);
  for(int i=0;i<m;i++){
    int l,r;
    cin>>l>>r;
    l--;
    D.add_edge(l,r,1);
    D.add_edge(r,l,1);
  }
  for(int i=0;i<=a;i++)
    D.add_edge(S,i,0);
  for(int i=b;i<=n;i++)
    D.add_edge(i,T,0);
  D.build(S);

  if(~D.bs[T]) drop(D[T]);
  drop(-1);
  return 0;
}
0