結果

問題 No.614 壊れたキャンパス
ユーザー Sebastian KingSebastian King
提出日時 2017-12-14 00:37:30
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 1,703 ms / 2,000 ms
コード長 2,079 bytes
コンパイル時間 2,123 ms
コンパイル使用メモリ 181,484 KB
実行使用メモリ 167,772 KB
最終ジャッジ日時 2024-05-08 08:06:04
合計ジャッジ時間 7,898 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 56 ms
144,000 KB
testcase_01 AC 56 ms
143,892 KB
testcase_02 AC 55 ms
144,000 KB
testcase_03 AC 55 ms
144,000 KB
testcase_04 AC 56 ms
144,020 KB
testcase_05 AC 55 ms
144,000 KB
testcase_06 AC 55 ms
144,000 KB
testcase_07 AC 55 ms
144,000 KB
testcase_08 AC 225 ms
152,340 KB
testcase_09 AC 1,703 ms
167,772 KB
testcase_10 AC 314 ms
153,856 KB
testcase_11 AC 245 ms
151,424 KB
testcase_12 AC 257 ms
151,440 KB
testcase_13 AC 249 ms
151,552 KB
testcase_14 AC 235 ms
152,340 KB
testcase_15 AC 285 ms
153,344 KB
testcase_16 AC 258 ms
152,960 KB
testcase_17 AC 227 ms
162,688 KB
testcase_18 AC 208 ms
164,204 KB
testcase_19 AC 204 ms
165,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int, int> PII;

const int MM = 1e9 + 7;
const double eps = 1e-8;
const int MAXN = 2e6 + 10;

int n, m;

void prework(){

}

void read(){

}

ll K;
int S, T;
vector<int> E[MAXN];
vector<PII> F[MAXN];
vector<ll> D[MAXN];
const ll INF = 1ll << 60;

unordered_map<int, int> tag1, tag2;

void solve(int casi){
//	cout << "Case #" << casi << ": ";
	cin >> n >> m >> K >> S >> T;
	E[1].push_back(S);
	E[n].push_back(T);
	for (int i = 1; i <= m; i++){
		int x, y, z;
		cin >> x >> y >> z;
		E[x].push_back(y);
		E[x+1].push_back(z);
		F[x].push_back(PII(y, z));
	}
	sort(E[1].begin(), E[1].end());
	E[1].resize(unique(E[1].begin(), E[1].end()) - E[1].begin());
	for (auto &x : E[1]){
		D[1].push_back(abs(x - S));
	}
	if (n == 1){
		cout << abs(S - T) << endl;
		return ;
	}
	for (int i = 1; i < n; i++){
		tag1.clear();
		tag2.clear();
		sort(E[i+1].begin(), E[i+1].end());
		E[i+1].resize(unique(E[i+1].begin(), E[i+1].end()) - E[i+1].begin());
		sort(F[i].begin(), F[i].end());
		D[i+1].resize(E[i+1].size());
		int n2 = E[i+1].size(), n1 = E[i].size();
		for (int j = 0; j < E[i].size(); j++)
			tag1[E[i][j]] = j;
		for (int j = 0; j < E[i + 1].size(); j++)
			tag2[E[i + 1][j]] = j;
		for (int j = 0; j < n2; j++)
			D[i+1][j] = INF;
		for (auto &x : F[i]){
			D[i+1][tag2[x.second]] = min(D[i+1][tag2[x.second]], D[i][tag1[x.first]]/* + abs(x.first - x.second)*/);
		}
		for (int j = 1; j < n2; j++){
			D[i+1][j] = min(D[i+1][j], D[i+1][j-1] + abs(E[i+1][j] - E[i+1][j-1]));
		}
		for (int j = n2 - 2; j >= 0; j--){
			D[i+1][j] = min(D[i+1][j], D[i+1][j+1] + abs(E[i+1][j] - E[i+1][j+1]));
		}
	/*	cout << "===========   " << i << endl;
		for (int j = 0; j < n2; j++)
			cout << E[i+1][j] << ' ' << D[i+1][j] << endl;
	*/
	}
	if (D[n][tag2[T]] >= INF)
		D[n][tag2[T]] = -1;
	cout << D[n][tag2[T]] << endl;
}

void printans(){

}


int main(){
	std::ios::sync_with_stdio(false);
	prework();
	int T = 1;
//	cin>>T;
	for(int i = 1; i <= T; i++){
		read();
		solve(i);
		printans();
	}
	return 0;
}

0