結果

問題 No.2565 はじめてのおつかい
ユーザー eom2357eom2357
提出日時 2023-12-02 17:25:58
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 75 ms / 2,000 ms
コード長 3,717 bytes
コンパイル時間 1,232 ms
コンパイル使用メモリ 104,540 KB
実行使用メモリ 9,252 KB
最終ジャッジ日時 2023-12-02 17:26:06
合計ジャッジ時間 5,946 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 2 ms
6,676 KB
testcase_02 AC 1 ms
6,676 KB
testcase_03 AC 73 ms
9,252 KB
testcase_04 AC 61 ms
9,252 KB
testcase_05 AC 1 ms
6,676 KB
testcase_06 AC 45 ms
6,676 KB
testcase_07 AC 25 ms
6,676 KB
testcase_08 AC 23 ms
6,676 KB
testcase_09 AC 43 ms
6,676 KB
testcase_10 AC 27 ms
6,676 KB
testcase_11 AC 63 ms
6,676 KB
testcase_12 AC 18 ms
6,676 KB
testcase_13 AC 30 ms
6,676 KB
testcase_14 AC 47 ms
6,676 KB
testcase_15 AC 45 ms
6,676 KB
testcase_16 AC 49 ms
7,552 KB
testcase_17 AC 12 ms
6,676 KB
testcase_18 AC 15 ms
6,676 KB
testcase_19 AC 71 ms
6,676 KB
testcase_20 AC 53 ms
6,676 KB
testcase_21 AC 49 ms
6,676 KB
testcase_22 AC 29 ms
6,676 KB
testcase_23 AC 34 ms
6,676 KB
testcase_24 AC 8 ms
6,676 KB
testcase_25 AC 52 ms
6,676 KB
testcase_26 AC 31 ms
6,676 KB
testcase_27 AC 50 ms
6,676 KB
testcase_28 AC 58 ms
6,676 KB
testcase_29 AC 62 ms
6,924 KB
testcase_30 AC 52 ms
6,676 KB
testcase_31 AC 54 ms
6,676 KB
testcase_32 AC 31 ms
6,676 KB
testcase_33 AC 54 ms
6,676 KB
testcase_34 AC 53 ms
6,676 KB
testcase_35 AC 54 ms
6,984 KB
testcase_36 AC 55 ms
6,676 KB
testcase_37 AC 29 ms
6,676 KB
testcase_38 AC 54 ms
6,676 KB
testcase_39 AC 44 ms
6,676 KB
testcase_40 AC 63 ms
6,872 KB
testcase_41 AC 75 ms
7,168 KB
testcase_42 AC 34 ms
6,676 KB
testcase_43 AC 50 ms
6,676 KB
testcase_44 AC 28 ms
6,676 KB
testcase_45 AC 13 ms
6,676 KB
testcase_46 AC 53 ms
6,676 KB
testcase_47 AC 39 ms
6,676 KB
testcase_48 AC 27 ms
6,676 KB
testcase_49 AC 16 ms
6,676 KB
testcase_50 AC 45 ms
6,676 KB
testcase_51 AC 2 ms
6,676 KB
testcase_52 AC 39 ms
6,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<string>
#include<string.h>
#include<algorithm>
#include<vector>
#include<tuple>
#include<math.h>
#include<numeric>
#include<set>
#include<climits>
#include<map>
#include<stack>
#include<queue>
#include <unordered_set>
#include <algorithm>
#include <climits>
#include <cstdio>
#include <set>
#include <utility>
//
//多分勝ちましたーwww
// 
//うえーい
// 
//
using namespace std;
typedef long long ll;
typedef long double ld;
using Graph = vector<vector<int>>;
using vi = vector<int>;
using vll = vector<long long>;
using vs = vector<string>;
using pii = pair<int, int>;
using pll = pair<long long, long long>;
template <typename T>
bool chmax(T& a, const T& b) { if (a < b) { a = b;return true; }return false; }
template <typename T>
bool chmin(T& a, const T& b) { if (a > b) { a = b;return true; }return false; }
#define YN(bool) if(bool){cout<<"Yes"<<endl;}else{cout<<"No"<<endl;}
#define reps(i, a, n) for (int i = (a); i < (int)(n); ++i)
#define rep(i, n) reps(i, 0, n)
#define rrep(i, n) for (int i = (int)(n) - 1; i >= 0; i--)
#define ALL(box)  (box).begin(),(box).end()
#define all(box)  (box).begin(),(box).end()
#define RALL(box)  (box).rbegin(),(box).rend()
#define ft first
#define sd second
#define pb push_back
//#define mp make_pair
#define pqueue priority_queue
//sort(box.begin(), box.end());
//sort(box.rbegin(), box.rend());
//vector<vector<int>> box( a , vector<int>( a ));
//printf("%.7Lf", n);
//reverse(t.begin(), t.end());
//auto It = lower_bound(ALL(box), n);以上 4
//auto It = upper_bound(ALL(box), n);含まない上 7
//cout << box.end() - It ; 末尾までの距離
//cout << It - a.begin() ; 先頭までの距離
//auto It =box.upper_bound( k);set,multiset
// vector<vector<vector<bool>>> flag(350, vector<vector<bool>> 350, vector<bool,false>(350)));
//pqueue < int, vector<int>, greater<int>> q;

vector<vector<char>> migi(vector<vector<char>>& grid) {
	int N = grid.size();

	// 回転後の新しいグリッドを生成
	vector<vector<char>> rotatedGrid(N, vector<char>(N, 0));

	// 右に90度回転させる処理
	for (int i = 0; i < N; ++i) {
		for (int j = 0; j < N; ++j) {
			rotatedGrid[i][j] = grid[N - 1 - j][i];
		}
	}

	return rotatedGrid;
}
ll sum = 0;
ll num = 0;
int pum = 0;
ll mum = 0;
int min1 = 1000000001;
int max1 = -2;
ll min2 = 9223372036854775807;
ll max2 = -10000000000000000;
ll MOD1 = 1000000007;
ll MOD = 998244353;
int Graph_saitan(const vector<vector<int>>& box, int a1, int a2) {
	queue<int> st;
	st.push(a1);
	vector<int> saitan(box.size(),20000000);
	saitan[a1] = 0;
	while (!st.empty()) {
		int p = st.front();
		st.pop();
		rep(i, box[p].size()) {
			if (saitan[box[p][i]] > saitan[p] + 1) {
				st.push(box[p][i]);
				saitan[box[p][i]] = saitan[p] + 1;
			}
		}
		if (saitan[a2] != 20000000) {
			return saitan[a2];
		}
	}
	return -2;
}
int main() {
	int a, b;
	cin >> a >> b;
	Graph box(a + 1);
	rep(i, b) {
		int l, m;
		cin >> l >> m;
		l--;
		m--;
		box[l].push_back(m);
	}
	vector<int> f(3), f2(3);
	int ans = 20000000;
	f[0] = Graph_saitan(box, 0, a - 2);
	f[1] = Graph_saitan(box, a - 2, a - 1);
	f[2] = Graph_saitan(box, a - 1, 0);
	f2[0] = Graph_saitan(box, 0, a - 1);
	f2[1] = Graph_saitan(box, a - 1, a - 2);
	f2[2] = Graph_saitan(box, a - 2, 0);
	rep(i, 3) {
		//cout << f[i] << " ";
	}
	rep(i, 3) {
		//cout << f2[i] << " ";
	}
	bool ok=true;
	int sum1 = 0, sum2 = 0;
	rep(i, f.size()) {
		if (f[i] == -2) {
			ok = false;
		}
		sum1 += f[i];
	}
	if (ok) {
		ans = min(ans, sum1);
	}
	ok = true;
	rep(i, f2.size()) {
		if (f2[i] == -2) {
			ok = false;
		}
		sum2 += f2[i];
	}
	if (ok) {
		ans = min(ans, sum2);
	}
	if (ans == 20000000) {
		cout << -1;
	}
	else {
		cout << ans;
	}
	

}
0