結果

問題 No.2573 moving up
ユーザー binapbinap
提出日時 2023-12-02 16:57:41
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 3,624 bytes
コンパイル時間 6,318 ms
コンパイル使用メモリ 286,264 KB
実行使用メモリ 103,452 KB
最終ジャッジ日時 2023-12-02 16:57:54
合計ジャッジ時間 12,132 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 -- -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
権限があれば一括ダウンロードができます

ソースコード

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) {};
};

template<typename T>
struct Dijkstra{
	const T INF = 1001001001001001;
	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 h, w;
	cin >> h >> w;
	vvi a(h, vi(h + w));
	Dijkstra<long long> graph(h * (h + w));
	rep(x, h) rep(y, h + w){
		int from = x * (h + w) + y;
		if(y < h + w - 1){
			int to = x * (h + w) + y + 1;
			graph.add_edge(from, to, 1);
			graph.add_edge(to, from, 1);
		}
		if(x < h - 1){
			int to = (x + 1) * (h + w) + y;
			graph.add_edge(from, to, 1);
			graph.add_edge(to, from, 1);
		}
		if(y < h + w - 1 and x < h - 1){
			int to = (x + 1) * (h + w) + y + 1;
			graph.add_edge(from, to, 1);
			graph.add_edge(to, from, 1);
		}
	}
	rep(i, w){
		int x, y;
		cin >> x >> y;
		x--; y--;
		a[x][y]++;
	}
	int cnt = 0;
	long long ans = 0;
	rep(y, h + w) for(int x = h - 1; x >= 0; x--){ 
		if(a[x][y] > 0){
			ans += graph.get_dist(x * (h + w) + y, cnt);
			cnt++;
		}
	}
	cout << ans << "\n";
	return 0;
}
0