結果

問題 No.2565 はじめてのおつかい
ユーザー binapbinap
提出日時 2023-12-02 15:05:45
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 158 ms / 2,000 ms
コード長 3,288 bytes
コンパイル時間 4,613 ms
コンパイル使用メモリ 285,080 KB
実行使用メモリ 37,848 KB
最終ジャッジ日時 2023-12-02 15:05:58
合計ジャッジ時間 10,851 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 2 ms
6,676 KB
testcase_02 AC 2 ms
6,676 KB
testcase_03 AC 158 ms
37,848 KB
testcase_04 AC 110 ms
37,848 KB
testcase_05 AC 2 ms
6,676 KB
testcase_06 AC 109 ms
17,260 KB
testcase_07 AC 57 ms
11,244 KB
testcase_08 AC 49 ms
11,532 KB
testcase_09 AC 100 ms
17,600 KB
testcase_10 AC 68 ms
11,880 KB
testcase_11 AC 139 ms
24,568 KB
testcase_12 AC 42 ms
9,328 KB
testcase_13 AC 62 ms
11,092 KB
testcase_14 AC 100 ms
19,868 KB
testcase_15 AC 111 ms
19,608 KB
testcase_16 AC 87 ms
28,024 KB
testcase_17 AC 19 ms
7,748 KB
testcase_18 AC 25 ms
12,232 KB
testcase_19 AC 113 ms
23,444 KB
testcase_20 AC 101 ms
21,400 KB
testcase_21 AC 89 ms
21,652 KB
testcase_22 AC 45 ms
15,092 KB
testcase_23 AC 61 ms
14,916 KB
testcase_24 AC 10 ms
6,676 KB
testcase_25 AC 93 ms
22,044 KB
testcase_26 AC 60 ms
13,688 KB
testcase_27 AC 81 ms
23,112 KB
testcase_28 AC 101 ms
22,712 KB
testcase_29 AC 119 ms
26,664 KB
testcase_30 AC 86 ms
24,140 KB
testcase_31 AC 130 ms
22,020 KB
testcase_32 AC 48 ms
14,060 KB
testcase_33 AC 85 ms
23,864 KB
testcase_34 AC 101 ms
20,532 KB
testcase_35 AC 91 ms
25,688 KB
testcase_36 AC 108 ms
23,772 KB
testcase_37 AC 58 ms
14,392 KB
testcase_38 AC 80 ms
20,212 KB
testcase_39 AC 100 ms
19,992 KB
testcase_40 AC 101 ms
26,252 KB
testcase_41 AC 113 ms
28,692 KB
testcase_42 AC 62 ms
13,288 KB
testcase_43 AC 72 ms
19,208 KB
testcase_44 AC 44 ms
11,488 KB
testcase_45 AC 21 ms
7,948 KB
testcase_46 AC 123 ms
19,912 KB
testcase_47 AC 89 ms
16,388 KB
testcase_48 AC 63 ms
11,344 KB
testcase_49 AC 39 ms
9,344 KB
testcase_50 AC 111 ms
16,092 KB
testcase_51 AC 2 ms
6,676 KB
testcase_52 AC 96 ms
16,272 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
#include<atcoder/all>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
using namespace atcoder;
typedef long long ll;
typedef vector<int> vi;
typedef vector<long long> vl;
typedef vector<vector<int>> vvi;
typedef vector<vector<long long>> vvl;
typedef long double ld;
typedef pair<int, int> P;

ostream& operator<<(ostream& os, const modint& a) {os << a.val(); return os;}
template <int m> ostream& operator<<(ostream& os, const static_modint<m>& a) {os << a.val(); return os;}
template<typename T> istream& operator>>(istream& is, vector<T>& v){int n = v.size(); assert(n > 0); rep(i, n) is >> v[i]; return is;}
template<typename U, typename T> ostream& operator<<(ostream& os, const pair<U, T>& p){os << p.first << ' ' << p.second; return os;}
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v){int n = v.size(); rep(i, n) os << v[i] << (i == n - 1 ? "\n" : " "); return os;}
template <typename T> ostream& operator<<(ostream& os, const vector<vector<T>>& v){int n = v.size(); rep(i, n) os << v[i] << (i == n - 1 ? "\n" : ""); return os;}

template<typename T>
struct Edge_Dijkstra{
	int from, to;
	T cost;
	Edge_Dijkstra(int from, int to, T cost) : from(from), to(to), cost(cost) {};
};

const long long INF = 1001001001001001;
template<typename T>
struct Dijkstra{
	int n, m;
	vector<bool> initialized;
	vector<Edge_Dijkstra<T>> E;
	vector<vector<int>> G;
	map<int, vector<T>> dist;
	map<int, vector<int>> idx;
	Dijkstra(int _n) : n(_n), m(0), initialized(n, false), G(n){}
	void add_edge(int from, int to, T cost){
		Edge_Dijkstra e(from, to, cost);
		E.push_back(e);
		G[from].emplace_back(m);
		m++;
	}
  void calc(int s){
    initialized[s] = true;
    dist[s] = vector<T>(n, INF);
    idx[s] = vector<int>(n, -1);
    priority_queue<tuple<T, int, int>, vector<tuple<T, int, int>>, greater<tuple<T, int, int>>> pq;
    pq.emplace(0, s, -1);
    while(pq.size()){
      auto [cost, from, index] = pq.top(); pq.pop();
      if(dist[s][from] <= cost) continue;
      dist[s][from] = cost;
      idx[s][from] = index;
      for(int index : G[from]){
        int to = E[index].to;
        T cost_plus = E[index].cost;
        if(dist[s][to] <= cost + cost_plus) continue;
        pq.emplace(cost + cost_plus, to, index);
      }
    }
  }
  int farthest(int s){
  	if(!initialized[s]) calc(s);
  	int idx = 0;
  	rep(i, n) if(dist[s][i] > dist[s][idx]) idx = i;
  	return idx;
  }
  T get_dist(int s, int t){
    if(!initialized[s]) calc(s);
    return dist[s][t];
  }
  vi restore(int s, int t){
    if(!initialized[s]) calc(s);
    if(dist[s][t] == INF) return vi(0);
    vi res;
    while(idx[s][t] != -1){
      auto e = E[idx[s][t]];
      res.push_back(idx[s][t]);
      t = e.from;
    }
    reverse(res.begin(), res.end());
    return res;
  }
};

int main(){
	int n, m;
	cin >> n >> m;
	Dijkstra<long long> graph(4 * n);
	rep(_, m){
		int u, v;
		cin >> u >> v;
		u--; v--;
		rep(i, 4) graph.add_edge(i * n + u, i * n + v, 1);
	}
	graph.add_edge(n - 2, n + n - 2, 0);
	graph.add_edge(2 * n + n - 2, 3 * n + n - 2, 0);
	graph.add_edge(n - 1, 2 * n + n - 1, 0);
	graph.add_edge(n + n - 1, 3 * n + n - 1, 0);
	auto res = graph.get_dist(0, 3 * n);
	if(res == INF) cout << -1 << "\n";
	else cout << res << "\n";
	return 0;
}
0