結果

問題 No.1565 Union
ユーザー もなもな
提出日時 2021-06-26 13:49:54
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 769 ms / 2,000 ms
コード長 3,278 bytes
コンパイル時間 3,257 ms
コンパイル使用メモリ 222,280 KB
実行使用メモリ 44,512 KB
最終ジャッジ日時 2023-09-07 16:35:05
合計ジャッジ時間 12,611 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,384 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 134 ms
12,276 KB
testcase_11 AC 430 ms
33,048 KB
testcase_12 AC 313 ms
20,228 KB
testcase_13 AC 105 ms
11,260 KB
testcase_14 AC 501 ms
30,340 KB
testcase_15 AC 753 ms
43,992 KB
testcase_16 AC 703 ms
42,468 KB
testcase_17 AC 736 ms
43,384 KB
testcase_18 AC 752 ms
44,512 KB
testcase_19 AC 769 ms
43,256 KB
testcase_20 AC 337 ms
40,996 KB
testcase_21 AC 337 ms
42,380 KB
testcase_22 AC 337 ms
41,844 KB
testcase_23 AC 335 ms
41,616 KB
testcase_24 AC 339 ms
41,400 KB
testcase_25 AC 344 ms
43,344 KB
testcase_26 AC 339 ms
43,060 KB
testcase_27 AC 342 ms
42,664 KB
testcase_28 AC 343 ms
41,052 KB
testcase_29 AC 339 ms
41,300 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
#define MOD2 998244353
#define rep(i,n) for (int i = 0; i < (int)(n); i++)
#define reps(i,s,n) for (int i = (int)(s); i < (int)(n); i++)
#define repit(i,C) for (auto i = (C).begin(); i != (C).end(); i++)
#define pr(a) cout << a
#define prl(a) cout << (a) << endl
#define prld(a) cout << setprecision(15)<< (a) << endl 
#define allrange(a) a.begin(),a.end()

template<typename T = int>
struct Edge {
  int src, dst;
  T weight;
  Edge(int src, int dst, T weight) :
    src(src), dst(dst), weight(weight) { };
  Edge() {};
};
template<typename T = int>
bool operator < (const Edge<T> &e, const Edge<T> &f) {
  return e.weight != f.weight ? e.weight < f.weight : // コストが低いほう優先
    e.src != f.src ? e.src < f.src : e.dst < f.dst;
};

template <typename T= int>
using Edges = std::vector<Edge<T>>;
using Vertexs = std::vector<int>;


template<typename T = int>
struct Graph { //directed graph
    Vertexs vertexs;
    map<int,int> vindex;
    Edges<T> edges;
    std::vector<
      std::vector< 
       std::pair<T,int>
    >> adjacent_to, adjacent_from;

    Graph(): vertexs(Vertexs (0)),edges(Edges<T>(0)),vindex(),
      adjacent_to(std::vector<std::vector<std::pair<T,int>>>(0)),
      adjacent_from(std::vector<std::vector<std::pair<T,int>>>(0)) {};

    void addv(int t){
        vertexs.push_back(t);
        vindex[t]=vertexs.size()-1;
        adjacent_to.push_back(std::vector<std::pair<T,int>>(0));
        adjacent_from.push_back(std::vector<std::pair<T,int>>(0));
    }
    void adde(int s,int d,T w){
        edges.push_back(Edge<T>(s,d,w));
        adjacent_to.at(vindex[s]).push_back(std::make_pair(w,d));
        adjacent_from.at(vindex[s]).push_back(std::make_pair(w,d));
    }

};

template<typename T = int>
struct UndirectedGraph : Graph<T>{ //undirected graph
    void adde(int s,int d,T w){
        this.adde(s,d,w);
        this.adjacent_to.at(d).push_back(std::make_pair(w,s));
        this.adjacent_from.at(d).push_back(std::make_pair(w,s));
    }
};

/* 2021/06/20

 unordered_setを使って拡張可能なデータ構造。
  find  所属判定
  insert  元の挿入
 が平均o(1)で可能。
  グループの合併は再帰関数を使用
  
  元の削除はできない
*/
vector<int> graph_bfs(struct Graph<int>& G,int s=0)
{
  int N = G.vertexs.size();
  vector<int> out(N);
  vector<bool> visited(N);
  queue<int> work;

  for(int i=0;i<N;i++)
  {
    out[i]=INT_MAX;
    visited[i]=false;
  }
  out[s]=0;
  work.push(s);

  while(!work.empty())
  {
    int pt = work.front();
    work.pop();
    for(auto e:G.adjacent_from[pt])
      if(out[e.second]==INT_MAX) out[e.second]=out[pt]+e.first;
      else out[e.second] = min(out[e.second],out[pt]+e.first);
    
    if(visited[pt]) continue;
     visited[pt]=true;
    for(auto e:G.adjacent_from[pt])
       if(!visited[e.second]) work.push(e.second);
   }

   for(int &l:out) if(l==INT_MAX) l=-1;
   return out;
};


int main(){
   //1変数入力
    int N,M;
    cin >> N >> M;
    Graph<int> G;

    rep(i,N) G.addv(i); 

    rep(i,M){
        int u,v;
        cin >> u >> v;
        u--;v--;
        G.adde(u,v,1);
        G.adde(v,u,1);
    }

    auto ans =  graph_bfs(G,0);
    prl(ans[N-1]);

}
0