Submission #1206953

#TimeUsernameProblemLanguageResultExecution timeMemory
1206953mychecksedadBoard Game (JOI24_boardgame)C++20
0 / 100
4116 ms465952 KiB
/* Author : Mychecksdead */ #include<bits/stdc++.h> using namespace std; #define ll long long int #define MOD (1000000000+7) #define MOD1 (998244353) #define pb push_back #define all(x) x.begin(), x.end() #define en cout << '\n' #define ff first #define ss second #define pii pair<int,int> #define vi vector<int> const int N = 1e6+100, M = 50000+10, K = 52, MX = 30; int n, m, k, a[N], is[N]; vi g[N]; ll DP[N]; array<ll, 3> res[N][2]; // 2 dif results for players for 2...k map<int, int> DIST[M]; bitset<M> vis[M]; void solve(){ cin >> n >> m >> k; for(int i = 1; i <= m; ++i){ int u, v; cin >> u >> v; g[u].pb(v); g[v].pb(u); } string s; cin >> s; for(int i = 1; i <= n; ++i){ if(s[i-1] == '0') is[i] = 1; else is[i] = 0; } for(int i = 1; i <= k; ++i){ cin >> a[i]; } vector<int> dist(n + 1, N); vector<pii> dist2(n + 1, pii{N, N}); queue<int> q; priority_queue<array<int, 3>> qq; for(int i = 1; i <= n; ++i){ if(is[i] == 0){ dist[i] = 0; q.push(i); } } while(!q.empty()){ int v = q.front(); q.pop(); for(int u: g[v]){ if(dist[u] == N){ dist[u] = dist[v] + 1; q.push(u); } } } for(int i = 1; i <= n; ++i){ if(is[i] == 0){ for(int u: g[i]){ if(is[u] == 0){ dist2[i] = {0, 0}; qq.push({0, 0, i}); break; } } } } vector<bool> VIS(n + 1); while(!qq.empty()){ int v = qq.top()[2]; qq.pop(); if(VIS[v]) continue; VIS[v] = 1; for(int u: g[v]){ int new_dist = dist2[v].ff + (is[v] == 0 ? 0 : 1); if(dist2[u].ff > new_dist){ dist2[u] = {new_dist, dist2[v].ss}; if(is[v] == 0){ dist2[u].ss++; } qq.push({-dist2[u].ff, -(int)(is[u]), u}); } } } for(int i = 0; i <= n; ++i){ DP[i] = 0; } int cnt_1 = 0, cnt_2 = 0; vector<vi> slope_1(n + 1); for(int i = 2; i <= k; ++i){ // first one res[i][0] = {1, dist[a[i]], 2}; // at first, dist[a[i]], then just +2 +2 +2 +2... res[i][1] = {dist2[a[i]].ss, dist2[a[i]].ff + dist2[a[i]].ss, 1}; // we will wait for dist2[a[i]].ss phases to take it, then +1 +1 +1 +1... if(res[i][1][0] == 0){ // all to 1 cnt_1++; DP[1] += 1; // cerr << " one\n"; }else{ if(res[i][0][1] == 0){ res[i][0][1] = 2; } int k1 = res[i][0][1], m1 = 2; int k2 = res[i][1][1], m2 = 1; int y = res[i][1][0]; int x = k2 - k1 - y + 2;// first time tp-1 crosses tp-2 // so: k1, k1+2, k1+4.....k1+2*tm-2, k2 + (x-y), k2+(x-y)+1 .... // cerr << x << ' ' << k1 << ' ' << k2 << ' ' << y << '\n'; if(x <= 1){ cnt_1++; DP[1] += k2; }else{ cnt_2++; DP[1] += k1; if(x <= n + 1) slope_1[x].pb(k2 + (x - y) - (k1 + 2 * x - 2)); // switch... } } } for(int i = 2; i <= n; ++i){ DP[i] = DP[i - 1] + cnt_2 * 2 + cnt_1; for(int val: slope_1[i]){ cnt_2--; cnt_1++; DP[i] += val; } } // for(int i = 1; i <= n; ++i){ // cerr << DP[i] << '\n'; // } queue<pii> Q; // phase, node Q.push({a[1], 0}); DIST[a[1]][0] = 0; vis[a[1]][0] = 1; while(!Q.empty()){ int v = Q.front().ff, phase = Q.front().ss; Q.pop(); for(int u: g[v]){ int p = (is[v] == 0 && v != a[1] ? phase + 1 : phase); if(p == n) continue; if(!vis[u][p] && DIST[u].size() < 100){ DIST[u][p] = DIST[v][phase] + 1; vis[u][p] = 1; Q.push({u, p}); } } } // for(int i = 1; i <= n; ++i){ // cerr << i << ": " << DP[i] << '\n'; // } // en; for(int i = 1; i <= n; ++i){ ll ans = 1e18; for(int k = 0; k <= n; ++k) if(vis[i][k]) ans = min((ll)DIST[i][k] + DP[k], ans); cout << ans << '\n'; } } int main(){ cin.tie(0); ios::sync_with_stdio(0); int tt = 1, aa; // freopen("in.txt", "r", stdin); // freopen("out.txt", "w", stdout); while(tt--){ solve(); en; } cerr<<"time taken : "<<(float)clock()/CLOCKS_PER_SEC<<" seconds\n"; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...