Submission #212403

#TimeUsernameProblemLanguageResultExecution timeMemory
212403mode149256Mecho (IOI09_mecho)C++14
100 / 100
249 ms6400 KiB
/*input 7 3 TTTTTTT TGGGGGT TGGGGGT MGGGGGD TGGGGGT TGGGGGT TGHHGGT */ #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; typedef complex<ld> cd; typedef pair<int, int> pi; typedef pair<ll, ll> pl; typedef pair<ld, ld> pd; typedef vector<int> vi; typedef vector<vi> vii; typedef vector<ld> vd; typedef vector<ll> vl; typedef vector<vl> vll; typedef vector<pi> vpi; typedef vector<vpi> vpii; typedef vector<pl> vpl; typedef vector<cd> vcd; typedef vector<pd> vpd; typedef vector<bool> vb; typedef vector<vb> vbb; typedef std::string str; typedef std::vector<str> vs; #define x first #define y second #define debug(...) cout<<"["<<#__VA_ARGS__<<": "<<__VA_ARGS__<<"]\n" const int MOD = 1000000007; const ll INF = std::numeric_limits<ll>::max(); const int MX = 100101; const ld PI = 3.14159265358979323846264338327950288419716939937510582097494L; template<typename T> pair<T, T> operator+(const pair<T, T> &a, const pair<T, T> &b) { return pair<T, T>(a.x + b.x, a.y + b.y); } template<typename T> pair<T, T> operator-(const pair<T, T> &a, const pair<T, T> &b) { return pair<T, T>(a.x - b.x, a.y - b.y); } template<typename T> T operator*(const pair<T, T> &a, const pair<T, T> &b) { return (a.x * b.x + a.y * b.y); } template<typename T> T operator^(const pair<T, T> &a, const pair<T, T> &b) { return (a.x * b.y - a.y * b.x); } template<typename T> void print(vector<T> vec, string name = "") { cout << name; for (auto u : vec) cout << u << ' '; cout << '\n'; } using vc = vector<char>; int N, S; vector<vc> sk; vii hiveDis; int dx[] = {0, 1, 0, -1}; int dy[] = { -1, 0, 1, 0}; pi dxy[] = { {dx[0], dy[0]}, {dx[1], dy[1]}, {dx[2], dy[2]}, {dx[3], dy[3]} }; pi mecho; pi home; vpi hives; bool valid(pi a) { return a.x >= 0 and a.y >= 0 and a.x < N and a.y < N; } bool gali(int time) { vii dis(N, vi(N, MOD)); dis[mecho.x][mecho.y] = 0; queue<pi> q; q.push(mecho); if (time >= hiveDis[mecho.x][mecho.y]) return false; while (q.size()) { pi c = q.front(); q.pop(); for (int i = 0; i < 4; ++i) { pi n = c + dxy[i]; if (valid(n) and sk[n.x][n.y] != 'T') { if (sk[n.x][n.y] == 'D') return true; int newDis = dis[c.x][c.y] + 1; if (time + newDis / S < hiveDis[n.x][n.y] and newDis < dis[n.x][n.y]) { dis[n.x][n.y] = newDis; q.push(n); } } } } return false; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> N >> S; sk = vector<vc>(N, vc(N)); hiveDis = vii(N, vi(N, MOD)); for (int i = 0; i < N; ++i) { for (int j = 0; j < N; ++j) { cin >> sk[i][j]; if (sk[i][j] == 'M') mecho = {i, j}; else if (sk[i][j] == 'D') home = {i, j}; else if (sk[i][j] == 'H') hives.emplace_back(i, j); } } queue<pi> q; for (auto u : hives) { hiveDis[u.x][u.y] = 0; q.push(u); } while (q.size()) { pi c = q.front(); q.pop(); for (int i = 0; i < 4; ++i) { pi nc = c + dxy[i]; if (valid(nc) and sk[nc.x][nc.y] != 'T' and nc != home) { if (hiveDis[c.x][c.y] + 1 < hiveDis[nc.x][nc.y]) { hiveDis[nc.x][nc.y] = hiveDis[c.x][c.y] + 1; q.push(nc); } } } } if (!gali(0)) { return !printf("-1\n"); } int l = 0; int h = MOD; int m; while (l < h) { m = (l + h + 1) / 2; if (gali(m)) l = m; else h = m - 1; } printf("%d\n", l); } /* Look for: * special cases (n=1?) * overflow (ll vs int?) * the exact constraints (multiple sets are too slow for n=10^6 :( ) * array bounds */
#Verdict Execution timeMemoryGrader output
Fetching results...