結果

問題 No.2897 2集合間距離
ユーザー 👑 binapbinap
提出日時 2024-05-26 18:35:39
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,104 ms / 3,500 ms
コード長 1,964 bytes
コンパイル時間 2,567 ms
コンパイル使用メモリ 225,060 KB
実行使用メモリ 161,732 KB
最終ジャッジ日時 2024-09-20 20:51:38
合計ジャッジ時間 21,717 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 469 ms
108,772 KB
testcase_01 AC 481 ms
108,908 KB
testcase_02 AC 477 ms
109,100 KB
testcase_03 AC 526 ms
108,876 KB
testcase_04 AC 541 ms
108,776 KB
testcase_05 AC 535 ms
109,160 KB
testcase_06 AC 537 ms
108,792 KB
testcase_07 AC 531 ms
109,032 KB
testcase_08 AC 546 ms
108,860 KB
testcase_09 AC 535 ms
108,908 KB
testcase_10 AC 540 ms
109,028 KB
testcase_11 AC 541 ms
109,032 KB
testcase_12 AC 616 ms
109,684 KB
testcase_13 AC 643 ms
109,684 KB
testcase_14 AC 698 ms
112,012 KB
testcase_15 AC 691 ms
112,248 KB
testcase_16 AC 1,063 ms
161,732 KB
testcase_17 AC 1,094 ms
161,700 KB
testcase_18 AC 1,063 ms
161,652 KB
testcase_19 AC 1,099 ms
161,632 KB
testcase_20 AC 1,104 ms
161,724 KB
testcase_21 AC 942 ms
161,224 KB
testcase_22 AC 890 ms
160,980 KB
testcase_23 AC 935 ms
161,420 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;

struct Edge_BFS01{
	int from, to;
	int cost;
	Edge_BFS01(int from, int to, int cost) : from(from), to(to), cost(cost) {};
};

const int INF = 1001001001;
struct BFS01{
	int n, m;
	vector<bool> initialized;
	vector<Edge_BFS01> E;
	vector<vector<int>> G;
	map<int, vector<int>> dist;
	BFS01(int _n) : n(_n), m(0), initialized(n, false), G(n){}
	void add_edge(int from, int to, int cost){
		assert(cost == 0 or cost == 1);
		Edge_BFS01 e(from, to, cost);
		E.push_back(e);
		G[from].emplace_back(m);
		m++;
	}
	void calc(int s){
		initialized[s] = true;
		dist[s] = vector<int>(n, INF);
		deque<tuple<int, int ,int>> de;
		de.emplace_back(0, s, -1);
		while(de.size()){
			auto [cost, from, index] = de.front(); de.pop_front();
			if(dist[s][from] <= cost) continue;
			dist[s][from] = cost;
			for(int index : G[from]){
				int to = E[index].to;
				int cost_plus = E[index].cost;
				if(dist[s][to] <= cost + cost_plus) continue;
				if(cost_plus == 0) de.emplace_front(cost + cost_plus, to, index);
				if(cost_plus == 1) de.emplace_back(cost + cost_plus, to, index);
			}
		}
	}
	int get_dist(int s, int t){
		if(!initialized[s]) calc(s);
		return dist[s][t];
	}
};

const int D = 1000;

int main(){
	auto convert = [&](int x, int y){
		return D * x + y;
	};
	
	BFS01 graph(D * D + 2);
	int sv = D * D;
	int tv = D * D + 1;
	
	int n;
	cin >> n;
	rep(i, n){
		int x, y;
		cin >> x >> y;
		graph.add_edge(sv, convert(x, y), 0);
	}
	int m;
	cin >> m;
	rep(i, m){
		int x, y;
		cin >> x >> y;
		graph.add_edge(convert(x, y), tv, 0);
	}
	rep(x, D) rep(y, D - 1){
		graph.add_edge(convert(x, y), convert(x, y + 1), 1);
		graph.add_edge(convert(x, y + 1), convert(x, y), 1);
	}
	rep(y, D) rep(x, D - 1){
		graph.add_edge(convert(x, y), convert(x + 1, y), 1);
		graph.add_edge(convert(x + 1, y), convert(x, y), 1);
	}
	int ans = graph.get_dist(sv, tv);
	cout << ans << "\n";
	return 0;
}
0