結果

問題 No.124 門松列(3)
ユーザー koyumeishikoyumeishi
提出日時 2015-03-12 02:35:32
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 9 ms / 5,000 ms
コード長 3,058 bytes
コンパイル時間 809 ms
コンパイル使用メモリ 86,704 KB
実行使用メモリ 5,676 KB
最終ジャッジ日時 2023-09-06 23:18:32
合計ジャッジ時間 2,105 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 6 ms
5,200 KB
testcase_04 AC 9 ms
5,676 KB
testcase_05 AC 9 ms
5,540 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 1 ms
4,384 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 1 ms
4,376 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 4 ms
4,376 KB
testcase_24 AC 6 ms
4,380 KB
testcase_25 AC 6 ms
4,376 KB
testcase_26 AC 3 ms
4,384 KB
testcase_27 AC 2 ms
4,376 KB
testcase_28 AC 9 ms
5,288 KB
testcase_29 AC 9 ms
5,364 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <cstdio>
#include <sstream>
#include <map>
#include <string>
#include <algorithm>
#include <queue>
#include <cmath>
#include <deque>

using namespace std;

typedef struct{
	int to;
	int cost;
}edge;


//using some techniques writen in wikipedia, SLF and LLL
void SPFA(vector<vector<edge>> &G, vector<int> &dist, int s){
	const int INF = 1e8;
	fill(dist.begin(), dist.end(), INF);

	vector<int> cnt(dist.size(), 0);

	dist[s] = 0;
	deque<int> Q;

	long long sum = 0;

	auto my_push = [&](int node){
		//SLF
		if(Q.size() == 0 || dist[Q.front()] > dist[node]){
			Q.push_front(node);
		}else{
			Q.push_back(node);
		}
		cnt[node]++;
	};
	auto my_pop = [&]() -> int{
		int ret = Q.front();
		cnt[ret]--;
		Q.pop_front();
		return ret;
	};

	auto adjust = [&](){
		//LLL
		double ave = 1.0*sum / Q.size();
		if(Q.size() <= 1) return;
		while( dist[Q.front()] > ave ){
			Q.push_back( Q.front() );
			Q.pop_front();
		}
	};
	
	my_push(s);

	while(!Q.empty()){
		int pos = my_pop();
		sum -= dist[pos];

		for(edge& E : G[pos]){
			if(dist[ E.to ] > dist[ pos ] + E.cost ){

				if(cnt[E.to] != 0){
					sum -= dist[ E.to ];
				}

				dist[ E.to ] = dist[ pos ] + E.cost;
				
				sum += dist[E.to];

				if(cnt[ E.to ] == 0){
					my_push( E.to );

					adjust();
				}
			}

		}
		
	}

}
 
void add_edge(vector<vector<edge> > &G, int from, int to, int cost){
	G[from].push_back( (edge){to, cost} );
	//G[to].push_back( (edge){from, cost} );
}


bool is_kadomatsu_sec(int a, int b, int c){
	if(a==b || b==c || c==a) return false;
	if(b<a && b<c) return true;
	if(b>a && b>c) return true;
	return false;
}

int x[] = {0,1,0,-1};
int y[] = {-1,0,1,0};

int main(){
	int w,h;
	cin >> w >> h;
	vector<vector<int> > v(h, vector<int>(w));
	for(int i=0; i<h; i++){
		for(int j=0; j<w; j++){
			cin >> v[i][j];
		}
	}

	vector<vector<edge> > G(w*h*4);
	for(int i=0; i<h; i++){
		for(int j=0; j<w; j++){
			
			for(int k=0; k<4; k++){
				int xx = j+x[k];
				int yy = i+y[k];
				if(xx<0 || xx>=w) continue;
				if(yy<0 || yy>=h) continue;

				for(int l=0; l<4; l++){
					if(l==k) continue;
					int xxx = j+x[l];
					int yyy = i+y[l];
					if(xxx<0 || xxx>=w) continue;
					if(yyy<0 || yyy>=h) continue;

					if(is_kadomatsu_sec(v[yy][xx], v[i][j], v[yyy][xxx])){
						add_edge(G, i*w+j + w*h*k, yyy*w+xxx + w*h*((l+2)%4), 1);
					}
				}
				
			}
			
		}
	}

	const int INF = 10000000;

	int ans = INF;

	vector<int> dist(w*h*4);
	
	for(int i=0; i<4; i++){
		int x1 = 0+x[i];
		int y1 = 0+y[i];
		if(x1<0 || x1>=w) continue;
		if(y1<0 || y1>=h) continue;
		
		for(int j=0; j<4; j++){
			int x2 = x1+x[j];
			int y2 = y1+y[j];
			if(x2<0 || x2>=w) continue;
			if(y2<0 || y2>=h) continue;
			if(x2==0 && y2==0) continue;
			
			
			if(is_kadomatsu_sec(v[0][0], v[y1][x1], v[y2][x2])){
				
				SPFA(G, dist, y2*w+x2 + w*h*((j+2)%4));
				for(int k=0; k<4; k++){
					ans = min(ans, dist[ (h-1)*w+(w-1) + w*h*k ]+2);
				}
			}
			
		}
	}

	if(ans < INF) cout << ans << endl;
	else cout << -1 << endl;
	return 0;
}
0