結果

問題 No.5016 Worst Mayor
ユーザー e869120e869120
提出日時 2023-04-29 16:51:52
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 271 ms / 2,000 ms
コード長 9,059 bytes
コンパイル時間 1,208 ms
コンパイル使用メモリ 91,516 KB
実行使用メモリ 26,280 KB
スコア 23,920,918,639
平均クエリ数 400.00
最終ジャッジ日時 2023-04-29 16:53:26
合計ジャッジ時間 16,961 ms
ジャッジサーバーID
(参考情報)
judge12 / judge16
純コード判定しない問題か言語
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 244 ms
25,320 KB
testcase_01 AC 231 ms
25,768 KB
testcase_02 AC 235 ms
25,508 KB
testcase_03 AC 234 ms
25,652 KB
testcase_04 AC 234 ms
25,852 KB
testcase_05 AC 235 ms
25,952 KB
testcase_06 AC 235 ms
25,972 KB
testcase_07 AC 238 ms
25,556 KB
testcase_08 AC 236 ms
25,948 KB
testcase_09 AC 239 ms
26,100 KB
testcase_10 AC 234 ms
25,900 KB
testcase_11 AC 234 ms
26,136 KB
testcase_12 AC 237 ms
25,364 KB
testcase_13 AC 236 ms
25,700 KB
testcase_14 AC 235 ms
25,760 KB
testcase_15 AC 234 ms
25,612 KB
testcase_16 AC 232 ms
25,752 KB
testcase_17 AC 232 ms
25,856 KB
testcase_18 AC 241 ms
25,344 KB
testcase_19 AC 233 ms
25,760 KB
testcase_20 AC 266 ms
25,532 KB
testcase_21 AC 236 ms
25,532 KB
testcase_22 AC 239 ms
25,944 KB
testcase_23 AC 233 ms
26,228 KB
testcase_24 AC 236 ms
25,484 KB
testcase_25 AC 233 ms
25,592 KB
testcase_26 AC 237 ms
25,440 KB
testcase_27 AC 232 ms
25,756 KB
testcase_28 AC 234 ms
25,904 KB
testcase_29 AC 237 ms
25,300 KB
testcase_30 AC 236 ms
25,644 KB
testcase_31 AC 241 ms
25,504 KB
testcase_32 AC 235 ms
25,504 KB
testcase_33 AC 271 ms
26,280 KB
testcase_34 AC 263 ms
25,564 KB
testcase_35 AC 240 ms
25,692 KB
testcase_36 AC 235 ms
25,884 KB
testcase_37 AC 237 ms
26,000 KB
testcase_38 AC 239 ms
25,952 KB
testcase_39 AC 260 ms
25,980 KB
testcase_40 AC 235 ms
25,936 KB
testcase_41 AC 235 ms
26,140 KB
testcase_42 AC 246 ms
25,584 KB
testcase_43 AC 237 ms
25,328 KB
testcase_44 AC 230 ms
25,968 KB
testcase_45 AC 233 ms
25,356 KB
testcase_46 AC 233 ms
25,572 KB
testcase_47 AC 241 ms
25,360 KB
testcase_48 AC 240 ms
25,784 KB
testcase_49 AC 240 ms
25,764 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <tuple>
#include <cmath>
#include <algorithm>
using namespace std;

// Input Information
int DEBUG = 1;
int N, T;
int A[3009], B[3009], C[3009], D[3009];
int CurrentMoney, DebugMoney = 1000000;
int CurrentCorps, DebugCorps = 1;

// Other Variables
int Build[1009];
int Money[2000009];
int Cost[199][199];
int D1[15][16];
int D2[16][15];

// Edge List
tuple<int, int, int, int> Edge[95];
tuple<int, int, int, int> EdgeList[95] = {
make_tuple(0, 1, 1, 1),
make_tuple(1, 1, 2, 1),
make_tuple(1, 2, 2, 2),
make_tuple(1, 8, 2, 8),
make_tuple(1, 10, 2, 10),
make_tuple(1, 11, 2, 11),
make_tuple(1, 12, 2, 12),
make_tuple(2, 3, 3, 3),
make_tuple(2, 6, 3, 6),
make_tuple(2, 8, 3, 8),
make_tuple(2, 10, 3, 10),
make_tuple(2, 12, 3, 12),
make_tuple(2, 13, 3, 13),
make_tuple(3, 3, 4, 3),
make_tuple(3, 6, 4, 6),
make_tuple(3, 7, 4, 7),
make_tuple(3, 8, 4, 8),
make_tuple(3, 13, 4, 13),
make_tuple(4, 2, 5, 2),
make_tuple(4, 6, 5, 6),
make_tuple(4, 8, 5, 8),
make_tuple(5, 3, 6, 3),
make_tuple(5, 5, 6, 5),
make_tuple(5, 6, 6, 6),
make_tuple(5, 8, 6, 8),
make_tuple(5, 9, 6, 9),
make_tuple(5, 10, 6, 10),
make_tuple(6, 1, 7, 1),
make_tuple(6, 2, 7, 2),
make_tuple(6, 3, 7, 3),
make_tuple(6, 5, 7, 5),
make_tuple(6, 8, 7, 8),
make_tuple(6, 10, 7, 10),
make_tuple(7, 4, 8, 4),
make_tuple(7, 5, 8, 5),
make_tuple(7, 8, 8, 8),
make_tuple(7, 10, 8, 10),
make_tuple(8, 3, 9, 3),
make_tuple(8, 4, 9, 4),
make_tuple(8, 11, 9, 11),
make_tuple(9, 0, 10, 0),
make_tuple(9, 1, 10, 1),
make_tuple(9, 2, 10, 2),
make_tuple(9, 11, 10, 11),
make_tuple(10, 0, 11, 0),
make_tuple(10, 3, 11, 3),
make_tuple(10, 12, 11, 12),
make_tuple(11, 2, 12, 2),
make_tuple(11, 12, 12, 12),
make_tuple(12, 3, 13, 3),
make_tuple(12, 11, 13, 11),
make_tuple(1, 1, 1, 2),
make_tuple(1, 2, 1, 3),
make_tuple(1, 9, 1, 10),
make_tuple(1, 10, 1, 11),
make_tuple(1, 11, 1, 12),
make_tuple(1, 12, 1, 13),
make_tuple(2, 2, 2, 3),
make_tuple(2, 9, 2, 10),
make_tuple(3, 2, 3, 3),
make_tuple(3, 7, 3, 8),
make_tuple(3, 8, 3, 9),
make_tuple(3, 9, 3, 10),
make_tuple(3, 12, 3, 13),
make_tuple(4, 2, 4, 3),
make_tuple(4, 6, 4, 7),
make_tuple(5, 2, 5, 3),
make_tuple(5, 5, 5, 6),
make_tuple(5, 9, 5, 10),
make_tuple(6, 1, 6, 2),
make_tuple(6, 2, 6, 3),
make_tuple(6, 8, 6, 9),
make_tuple(7, 0, 7, 1),
make_tuple(7, 3, 7, 4),
make_tuple(8, 4, 8, 5),
make_tuple(8, 10, 8, 11),
make_tuple(8, 11, 8, 12),
make_tuple(9, 0, 9, 1),
make_tuple(9, 2, 9, 3),
make_tuple(9, 3, 9, 4),
make_tuple(10, 1, 10, 2),
make_tuple(10, 2, 10, 3),
make_tuple(10, 9, 10, 10),
make_tuple(10, 10, 10, 11),
make_tuple(10, 11, 10, 12),
make_tuple(10, 12, 10, 13),
make_tuple(11, 2, 11, 3),
make_tuple(11, 3, 11, 4),
make_tuple(12, 2, 12, 3),
make_tuple(12, 10, 12, 11),
make_tuple(12, 11, 12, 12),
make_tuple(13, 1, 13, 2),
make_tuple(13, 2, 13, 3),
make_tuple(13, 10, 13, 11),
make_tuple(13, 11, 13, 12)
};

// Initialize Money
void Initialize() {
	for (int i = 0; i <= 1000; i++) {
		for (int j = 0; j < 1000; j++) {
			Money[1000 * i + 223 * j] = 60 * j;
		}
	}
	for (int i = 1; i <= 1000; i++) {
		Build[i] = (int)(10000000.0 / sqrt(1.0 * i));
	}
}

// Get Total Earned Money
int GetEarnedMoney() {
	for (int i = 0; i < 199; i++) {
		for (int j = 0; j < 199; j++) Cost[i][j] = 1000000;
		Cost[i][i] = 0;
	}

	// Initialize Cost
	for (int i = 0; i < 13; i++) {
		for (int j = 0; j < 14; j++) {
			int a1 = (i + 0) * 14 + (j + 0);
			int a2 = (i + 1) * 14 + (j + 0);
			if (D1[i][j] == 1) {
				Cost[a1][a2] = 223;
				Cost[a2][a1] = 223;
			}
			else {
				Cost[a1][a2] = 1000;
				Cost[a2][a1] = 1000;
			}
		}
	}
	for (int i = 0; i < 14; i++) {
		for (int j = 0; j < 13; j++) {
			int a1 = (i + 0) * 14 + (j + 0);
			int a2 = (i + 0) * 14 + (j + 1);
			if (D2[i][j] == 1) {
				Cost[a1][a2] = 223;
				Cost[a2][a1] = 223;
			}
			else {
				Cost[a1][a2] = 1000;
				Cost[a2][a1] = 1000;
			}
		}
	}

	// Warshall-Floyd
	for (int k = 0; k < 196; k++) {
		for (int i = 0; i < 196; i++) {
			for (int j = 0; j < 196; j++) Cost[i][j] = min(Cost[i][j], Cost[i][k] + Cost[k][j]);
		}
	}
	int sum = 0;
	for (int i = 1; i <= N; i++) {
		int pos1 = (A[i] - 1) * 14 + (B[i] - 1);
		int pos2 = (C[i] - 1) * 14 + (D[i] - 1);
		sum += Money[Cost[pos1][pos2]];
	}
	return sum;
}

void Ask(int a1, int a2, int a3, int a4, int a5) {
	if (DEBUG == 1) {
		if (a1 == 1) cout << a1 << " " << a2 << " " << a3 << " " << a4 << " " << a5 << endl;
		if (a1 == 2) cout << a1 << endl;
		if (a1 == 3) cout << a1 << endl;
	}
	if (DEBUG == 2) {
		if (a1 == 1) {
			if (a2 > a4) { swap(a2, a4); swap(a3, a5); }
			if (a3 > a5) { swap(a2, a4); swap(a3, a5); }
			if (a3 == a5) D1[a2 - 1][a3 - 1] = 1;
			if (a2 == a4) D2[a2 - 1][a3 - 1] = 1;
			DebugMoney -= Build[DebugCorps];
		}
		if (a1 == 2) {
			DebugCorps += 1;
		}
		if (a1 == 3) {
			DebugMoney += 50000;
		}
		DebugMoney += GetEarnedMoney();
	}
}

double Randouble() {
	double s = 0, t = 1;
	for (int i = 0; i < 3; i++) {
		t /= 1024.0;
		s += 1.0 * (rand() % 1024) * t;
	}
	return s;
}

int main() {
	// Input
	Initialize();
	cin >> N >> T;
	for (int i = 1; i <= N; i++) cin >> A[i] >> B[i] >> C[i] >> D[i];

	// Brute Force
	int MaxScore = 0;
	for (int i = 0; i < 8; i++) {
		for (int j = 0; j < 13; j++) {
			for (int k = 0; k < 14; k++) D1[j][k] = 0;
			for (int k = 0; k < 14; k++) D2[k][j] = 0;
		}
		for (int j = 0; j < 95; j++) {
			int px1 = get<0>(EdgeList[j]);
			int py1 = get<1>(EdgeList[j]);
			int px2 = get<2>(EdgeList[j]);
			int py2 = get<3>(EdgeList[j]);
			if (px1 == px2) D2[px1][min(py1, py2)] = 1;
			if (py1 == py2) D1[min(px1, px2)][py1] = 1;
		}
		int CandScore = GetEarnedMoney();
		if (MaxScore < CandScore) {
			MaxScore = CandScore;
			for (int j = 0; j < 95; j++) Edge[j] = EdgeList[j];
		}

		// Rotate
		for (int j = 0; j < 95; j++) {
			int px1 = get<0>(EdgeList[j]);
			int py1 = get<1>(EdgeList[j]);
			int px2 = get<2>(EdgeList[j]);
			int py2 = get<3>(EdgeList[j]);
			EdgeList[j] = make_tuple(13 - py1, px1, 13 - py2, px2);
		}
		if (i % 4 == 3) {
			for (int j = 0; j < 95; j++) {
				int px1 = get<0>(EdgeList[j]);
				int py1 = get<1>(EdgeList[j]);
				int px2 = get<2>(EdgeList[j]);
				int py2 = get<3>(EdgeList[j]);
				EdgeList[j] = make_tuple(13 - px1, py1, 13 - px2, py2);
			}
		}
	}

	// Initialize Shortest Distance
	vector<bool> Used(95, false);
	vector<vector<int>> Shortest(196, vector<int>(196, 1000000));
	for (int i = 0; i < 196; i++) Shortest[i][i] = 0;
	for (int i = 0; i < 13; i++) {
		for (int j = 0; j < 14; j++) {
			Shortest[i * 14 + j][(i + 1) * 14 + j] = 1000;
			Shortest[(i + 1) * 14 + j][i * 14 + j] = 1000;
			Shortest[j * 14 + i][j * 14 + (i + 1)] = 1000;
			Shortest[j * 14 + (i + 1)][j * 14 + i] = 1000;
		}
	}
	for (int k = 0; k < 196; k++) {
		for (int i = 0; i < 196; i++) {
			for (int j = 0; j < 196; j++) Shortest[i][j] = min(Shortest[i][j], Shortest[i][k] + Shortest[k][j]);
		}
	}

	// Reset D1, D2 for Debug
	for (int i = 0; i < 13; i++) {
		for (int j = 0; j < 14; j++) D1[i][j] = 0;
	}
	for (int i = 0; i < 14; i++) {
		for (int j = 0; j < 13; j++) D2[i][j] = 0;
	}

	// Answer Query
	int BuiltRoads = 0;
	for (int i = 1; i <= T; i++) {
		if (DEBUG == 1) {
			cin >> CurrentMoney >> CurrentCorps;
			if (CurrentMoney == -1 && CurrentCorps == -1) break;
		}
		if (DEBUG == 2) {
			CurrentMoney = DebugMoney;
			CurrentCorps = DebugCorps;
		}
		fprintf(stderr, "Turn =% 4d, Money=% 10d, Corps =% 3d, Roads =% 3d/%d\n", i, CurrentMoney, CurrentCorps, BuiltRoads, 95);

		// Get Acts
		if (i <= 50) {
			Ask(2, -1, -1, -1, -1);
		}
		else if (BuiltRoads == 95) {
			Ask(3, -1, -1, -1, -1);
		}
		else if (BuiltRoads >= 3 && CurrentMoney < Build[CurrentCorps]) {
			Ask(2, -1, -1, -1, -1);
		}
		else if (BuiltRoads <= 2 && CurrentMoney < Build[CurrentCorps]) {
			Ask(3, -1, -1, -1, -1);
		}
		else {
			int CurrentScore = -1;
			int CurrentID = 0;
			for (int j = 0; j < 95; j++) {
				if (Used[j] == true) continue;
				int sum = 0;
				int edge1 = get<0>(Edge[j]) * 14 + get<1>(Edge[j]);
				int edge2 = get<2>(Edge[j]) * 14 + get<3>(Edge[j]);
				for (int k = 1; k <= N; k++) {
					int pos1 = (A[k] - 1) * 14 + (B[k] - 1);
					int pos2 = (C[k] - 1) * 14 + (D[k] - 1);
					int v1 = Shortest[pos1][pos2];
					int v2 = Shortest[pos1][edge1] + 223 + Shortest[edge2][pos2];
					int v3 = Shortest[pos1][edge2] + 223 + Shortest[edge1][pos2];
					sum += Money[min({ v1, v2, v3 })];
				}
				if (CurrentScore < sum) {
					CurrentScore = sum;
					CurrentID = j;
				}
			}
			Ask(1, get<0>(Edge[CurrentID]) + 1, get<1>(Edge[CurrentID]) + 1, get<2>(Edge[CurrentID]) + 1, get<3>(Edge[CurrentID]) + 1);
			BuiltRoads += 1;
			Used[CurrentID] = true;
			int edgeA = get<0>(Edge[CurrentID]) * 14 + get<1>(Edge[CurrentID]);
			int edgeB = get<2>(Edge[CurrentID]) * 14 + get<3>(Edge[CurrentID]);
			for (int j = 0; j < 196; j++) {
				for (int k = 0; k < 196; k++) {
					int v1 = Shortest[j][k];
					int v2 = Shortest[j][edgeA] + 223 + Shortest[edgeB][k];
					int v3 = Shortest[j][edgeB] + 223 + Shortest[edgeA][k];
					Shortest[j][k] = min({ v1,v2,v3 });
				}
			}
		}
	}
	return 0;
}
0