結果
問題 | No.1244 Black Segment |
ユーザー |
|
提出日時 | 2020-10-02 23:00:19 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 45 ms / 2,000 ms |
コード長 | 933 bytes |
コンパイル時間 | 1,947 ms |
コンパイル使用メモリ | 201,548 KB |
最終ジャッジ日時 | 2025-01-15 01:06:36 |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 36 |
コンパイルメッセージ
main.cpp: In function ‘int main()’: main.cpp:37:27: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 37 | int n,m,a,b; scanf("%d%d%d%d",&n,&m,&a,&b), a--; | ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~ main.cpp:40:31: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 40 | int u,v; scanf("%d%d",&u,&v); u--; | ~~~~~^~~~~~~~~~~~~~
ソースコード
#include <bits/stdc++.h>#define rep(i,n) for(int i=0;i<(n);i++)using namespace std;using graph=vector<vector<int>>;void add_undirected_edge(graph& G,int u,int v){G[u].emplace_back(v);G[v].emplace_back(u);}void add_directed_edge(graph& G,int u,int v){G[u].emplace_back(v);}vector<int> distance(const graph& G,int s){const int INF=INT_MAX;int n=G.size();vector<int> d(n,INF);d[s]=0;queue<int> Q; Q.emplace(s);while(!Q.empty()){int u=Q.front(); Q.pop();for(int v:G[u]) if(d[v]==INF) {d[v]=d[u]+1;Q.emplace(v);}}return d;}const int INF=1<<29;int main(){int n,m,a,b; scanf("%d%d%d%d",&n,&m,&a,&b), a--;graph G(n+2);rep(i,m){int u,v; scanf("%d%d",&u,&v); u--;add_undirected_edge(G,u,v);}int s=n+1;rep(u,a+1) add_directed_edge(G,s,u);auto d=distance(G,s);int res=INF;for(int u=b;u<=n;u++) res=min(res,d[u]);printf("%d\n",res<INF?res-1:-1);return 0;}