#include #include #include #include #include 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]; // 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; } // Set Roads // Mode = 0: Build Roads on left // Mode = 1: Build Roads on right // Mode = 2: Build Roads on up // Mode = 3: Build Roads on down void SetRoads(vector vec, int Mode) { 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; } for (int i = 0; i < vec.size(); i++) { for (int j = 0; j < 13; j++) { if (Mode == 0 || Mode == 1) D2[vec[i]][j] = 1; if (Mode == 2 || Mode == 3) D1[j][vec[i]] = 1; } } for (int i = 0; i < 13; i++) { if (Mode == 0) D1[i][ 0] = 1; if (Mode == 1) D1[i][13] = 1; if (Mode == 2) D2[ 0][i] = 1; if (Mode == 3) D2[13][i] = 1; } } 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 Rectangles int NumberOfRoads = 5; int Maximum = 0; pair MaxID = make_pair(-1, -1); for (int i = 0; i < 4; i++) { vector Flag(14, false); for (int j = 0; j < NumberOfRoads; j++) { while (true) { int pos = rand() % 14; if (Flag[pos] == false) { Flag[pos] = true; break; } } } // Start Yakinamashi int CurrentScore = 0; int TIME = 400; int ti = clock(); while (clock() - ti < TIME * CLOCKS_PER_SEC / 1000) { double temp = 7000.0 * (1.0 - 1.0 * (clock() - ti) / (TIME * CLOCKS_PER_SEC / 1000)); int pos1 = rand() % 14; int pos2 = rand() % 14; swap(Flag[pos1], Flag[pos2]); vector vec; for (int j = 0; j < 14; j++) { if (Flag[j] == true) vec.push_back(j); } SetRoads(vec, i); int CandScore = GetEarnedMoney(); if (Randouble() < exp(1.0 * (CandScore - CurrentScore) / temp)) { CurrentScore = CandScore; } else { swap(Flag[pos1], Flag[pos2]); } } // Update if (Maximum < CurrentScore) { int mask = 0; for (int j = 0; j < 14; j++) { if (Flag[j] == true) mask += (1 << j); } Maximum = CurrentScore; MaxID = make_pair(i, mask); } } // Sort Lines (by distance from center) vector> Lines; for (int i = 0; i < 14; i++) { if ((MaxID.second & (1 << i)) == 0) continue; Lines.push_back(make_pair(abs(13 - 2 * i), i)); } sort(Lines.begin(), Lines.end()); // Calculate the Order of Adding Edges vector> Edges; for (int i = 0; i < Lines.size(); i++) { int to = Lines[i].second; int ord[13] = { 6, 5, 7, 4, 8, 3, 9, 2, 10, 1, 11, 0, 12 }; if (i >= 1) { for (int j = 0; j < 13; j++) ord[j] = j; if (MaxID.first == 1 || MaxID.first == 3) reverse(ord, ord + 13); } for (int j = 0; j < 13; j++) { if (MaxID.first == 0 || MaxID.first == 1) Edges.push_back(make_tuple(to, ord[j], to, ord[j] + 1)); if (MaxID.first == 2 || MaxID.first == 3) Edges.push_back(make_tuple(ord[j], to, ord[j] + 1, to)); } if (i == 0) { for (int j = 0; j < 13; j++) { if (MaxID.first == 0) Edges.push_back(make_tuple(ord[j], 0, ord[j] + 1, 0)); if (MaxID.first == 1) Edges.push_back(make_tuple(ord[j], 13, ord[j] + 1, 13)); if (MaxID.first == 2) Edges.push_back(make_tuple( 0, ord[j], 0, ord[j] + 1)); if (MaxID.first == 3) Edges.push_back(make_tuple(13, ord[j], 13, ord[j] + 1)); } } } // 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, (int)Edges.size()); // Get Acts if (i <= 50) { Ask(2, -1, -1, -1, -1); } else if (BuiltRoads == (int)Edges.size()) { 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 { Ask(1, get<0>(Edges[BuiltRoads]) + 1, get<1>(Edges[BuiltRoads]) + 1, get<2>(Edges[BuiltRoads]) + 1, get<3>(Edges[BuiltRoads]) + 1); BuiltRoads += 1; } } return 0; }