Submission #1035406

# Submission time Handle Problem Language Result Execution time Memory
1035406 2024-07-26T10:19:03 Z CSQ31 Board Game (JOI24_boardgame) C++17
100 / 100
1743 ms 85900 KB
#pragma GCC optimize("Ofast") 
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define fi first
#define se second
#define sz(a) (int)(a.size())
#define all(a) a.begin(),a.end()
#define lb lower_bound
#define ub upper_bound
#define owo ios_base::sync_with_stdio(0);cin.tie(0);
#define debug(...) fprintf(stderr, __VA_ARGS__),fflush(stderr)
#define time__(d) for(long blockTime = 0; (blockTime == 0 ? (blockTime=clock()) != 0 : false);\
debug("%s time : %.4fs\n", d, (double)(clock() - blockTime) / CLOCKS_PER_SEC))
typedef long long int ll;
typedef long double ld;
typedef pair<ll,ll> PII;
typedef pair<int,int> pii;
typedef vector<vector<int>> vii;
typedef vector<vector<ll>> VII;
ll gcd(ll a,ll b){if(!b)return a;else return gcd(b,a%b);}
const int MAXN = 5e4+50;
vector<int>adj[MAXN];
ll ans[MAXN];
ll dp[200][MAXN];
int main()
{
    int n,m,k;
    cin>>n>>m>>k;
    for(int i=0;i<n;i++)ans[i] = 1e18;
    for(int i=0;i<m;i++){
        int u,v;
        cin>>u>>v;
        u--;
        v--;
        adj[u].pb(v);
        adj[v].pb(u);
    }

    string s;
    cin>>s;
    vector<int>T(k);
    for(int i=0;i<k;i++){cin>>T[i];T[i]--;}
    vector<ll>rdist(n,1e9),bdist(n,1e9),ndist(n,1e9),ones;
    deque<int>q;
    if(true){
        queue<int>q;
        q.push(T[0]);
        vector<ll>d(n,n);
        d[T[0]] = 0;
        while(!q.empty()){
            int v = q.front();
            q.pop();
            ndist[v] = ans[v] = d[v];
            if(v != T[0] && s[v] == '1'){
                ones.pb(v);
                continue;
            }
            for(int x:adj[v]){
                if(d[x] > d[v]+1){
                    d[x] = d[v]+1;
                    q.push(x);
                }
            }
        }
    } 
    for(int i=0;i<n;i++){
        if(s[i] == '0')continue;
        int cnt = 0;
        for(int x:adj[i])cnt += s[x]=='1';
        if(cnt){
            rdist[i] = 0;
            q.pb(i);
        }
    }
    while(!q.empty()){
        int v = q.front();
        q.pop_front();
        for(int x:adj[v]){
            if(rdist[x] == 1e9){
                if(s[x] == '1'){
                    rdist[x] = rdist[v];
                    q.push_front(x);
                }else{
                    rdist[x] = rdist[v]+1;
                    q.push_back(x);
                }
            }
        }
    } //min dist to a red node
    for(int i=0;i<n;i++){if(s[i] == '1')rdist[i]++;}
    for(int i=0;i<n;i++){
        if(s[i] == '1'){
            for(int x:adj[i]){
                if(bdist[x] > 1){
                    bdist[x] = 1;
                    q.pb(x);
                }
            }
        }
    }
    while(!q.empty()){
        int v = q.front();
        q.pop_front();
        for(int x:adj[v]){
            if(bdist[x] == 1e9){
                bdist[x] = bdist[v]+1;
                q.push_back(x);
            }
        }
    } //min dist to a black node

   // for(int i=0;i<n;i++)cout<<bdist[i]<<" "<<rdist[i]<<'\n';
    if(k-1 <= 300){ //half of the sqrt sol
        ll sum = 0;
        vector<ll>f;
        for(int i=1;i<k;i++){
            sum += bdist[T[i]];
            f.pb(rdist[T[i]] - bdist[T[i]]);
        }
        sort(all(f));
        for(int i=0;i<k;i++){
            ll c = 2*(k-1) - i;
            vector<ll>dist(n,1e18);
            priority_queue<PII,vector<PII>,greater<PII>>pq;
            for(int v:ones){ 
                for(int x:adj[v]){
                    ll cost = ndist[v] + sum + 1;
                    if(s[x] == '1')cost += c;
                    if(dist[x] > cost){
                        dist[x] = cost;
                        pq.push({dist[x],x});
                    }
                }
            }
    
            while(!pq.empty()){
                int v = pq.top().se;
                ll d = pq.top().fi;
                pq.pop();
                if(d != dist[v])continue;
                //cout<<v<<" "<<d<<'\n';
                for(int x:adj[v]){
                    if(s[x] == '0'&& dist[x] > d+1){
                        dist[x] = d+1;
                        pq.push({dist[x],x});
                    }
                    if(s[x] == '1' && dist[x] > d+c+1){
                        dist[x] = d+c+1;
                        pq.push({dist[x],x});
                    }
                }
            }
            for(int j=0;j<n;j++){
                //cout<<dist[j]<<" ";
                if(s[j] == '1')ans[j] = min(ans[j],dist[j]-c);
                else ans[j] = min(ans[j],dist[j]);
            }
            if(i!=k-1)sum+=f[i];
        }
        for(int i=0;i<n;i++)cout<<ans[i]<<'\n';
        return 0;
    }
    vector<int>stops(n,1e9);
    stops[T[0]] = 0;
    q.pb(T[0]);
    while(!q.empty()){
        int v = q.front();
        q.pop_front();
        for(int x:adj[v]){
            if(s[x] == '0' && stops[x] > stops[v]){
                stops[x] = stops[v];
                q.push_front(x);
            }
            if(s[x] == '1' && stops[x] > stops[v]+1){
                stops[x] = stops[v]+1;
                q.pb(x);
            }
        }
    }
    for(int i=0;i<200;i++){
        for(int j=0;j<n;j++)dp[i][j] = 1e18;
    }
    dp[0][T[0]] = 0;
    queue<array<int,3>>qq;
    qq.push({0,0,T[0]});
    while(!qq.empty()){
        int v = qq.front()[2];
        int len = qq.front()[1];
        int num = qq.front()[0];
        qq.pop();
        //cout<<v<<" "<<len<<" "<<num-stops[v]<<" "<<dp[num-stops[v]][v]<<'\n';
        for(int x:adj[v]){
            if(s[x] == '0'){
                int d = num - stops[x];
                if(d < 200 && dp[d][x] > len + 1){
                    dp[d][x] = len + 1;
                    qq.push({num,len+1,x});
                }
            }else{
                int d = num+1 - stops[x];
                if(d < 200 && dp[d][x] > len + 1){
                    dp[d][x] = len + 1;
                    qq.push({num+1,len+1,x});
                }
            }
        }
    }
    vector<ll>cost(n+2,0),inc(n+2,0);
    for(int i=1;i<k;i++){
        int v = T[i];
        ll a = bdist[v];
        ll b = rdist[v];
        cost[1] += min(a,b);
        //a + 2*(x-1) > b + (x-1)
        //x >= b-a+1
        //x = b-a+1
        ll diff = b-a+1;
        //cout<<v<<" "<<a<<" "<<b<<" "<<diff<<'\n';
        if(diff >= 2){
            inc[2] += 2;
            if(diff <= n){
                inc[diff] -= 2;
                cost[diff] -= 2*(diff-2);
                cost[diff] -= min(a,b);

                cost[diff] += b + (diff-1);
                inc[diff+1] ++;
                
            }
        }else inc[2]++;
    }

    for(int i=1;i<=n;i++)inc[i] += inc[i-1];
    for(int i=1;i<=n;i++)cost[i] += cost[i-1] + inc[i];

    //for(int i=0;i<=n;i++)cout<<cost[i]<<" ";
   // cout<<'\n';
    for(int i=0;i<n;i++){
        if(i==T[0])continue;
        ans[i] = 1e18;
        for(int j=0;j<200;j++){
            int len = j + stops[i];
            if(s[i] == '1')len--;
            if(len > n)continue;
            //cout<<i<<" "<<len<<" "<<dp[j][i]<<" "<<cost[len]<<'\n';
            ans[i] = min(ans[i],dp[j][i] + cost[len]);
        }
    }

    for(int i=0;i<n;i++)cout<<ans[i]<<'\n';
}
# Verdict Execution time Memory Grader output
1 Correct 7 ms 7516 KB Output is correct
2 Correct 52 ms 52580 KB Output is correct
3 Correct 91 ms 85448 KB Output is correct
4 Correct 36 ms 6088 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 22 ms 7512 KB Output is correct
2 Correct 326 ms 52776 KB Output is correct
3 Correct 336 ms 6088 KB Output is correct
4 Correct 34 ms 6088 KB Output is correct
5 Correct 550 ms 85448 KB Output is correct
6 Correct 55 ms 6016 KB Output is correct
7 Correct 74 ms 5156 KB Output is correct
8 Correct 43 ms 5728 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 7512 KB Output is correct
2 Correct 305 ms 52840 KB Output is correct
3 Correct 632 ms 85200 KB Output is correct
4 Correct 906 ms 84932 KB Output is correct
5 Correct 952 ms 85448 KB Output is correct
6 Correct 44 ms 5840 KB Output is correct
7 Correct 104 ms 5832 KB Output is correct
8 Correct 983 ms 85384 KB Output is correct
9 Correct 147 ms 20048 KB Output is correct
10 Correct 22 ms 3552 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 1628 KB Output is correct
2 Correct 4 ms 1884 KB Output is correct
3 Correct 28 ms 7512 KB Output is correct
4 Correct 9 ms 1884 KB Output is correct
5 Correct 3 ms 1628 KB Output is correct
6 Correct 4 ms 1628 KB Output is correct
7 Correct 10 ms 4956 KB Output is correct
8 Correct 12 ms 7540 KB Output is correct
9 Correct 3 ms 1884 KB Output is correct
10 Correct 3 ms 1880 KB Output is correct
11 Correct 3 ms 1880 KB Output is correct
12 Correct 4 ms 1884 KB Output is correct
13 Correct 4 ms 1712 KB Output is correct
14 Correct 2 ms 1880 KB Output is correct
15 Correct 7 ms 1872 KB Output is correct
16 Correct 11 ms 1884 KB Output is correct
17 Correct 25 ms 1892 KB Output is correct
18 Correct 4 ms 1624 KB Output is correct
19 Correct 3 ms 1880 KB Output is correct
20 Correct 11 ms 1892 KB Output is correct
21 Correct 15 ms 7524 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 40 ms 5320 KB Output is correct
2 Correct 37 ms 6092 KB Output is correct
3 Correct 35 ms 5884 KB Output is correct
4 Correct 42 ms 6092 KB Output is correct
5 Correct 42 ms 5836 KB Output is correct
6 Correct 34 ms 5152 KB Output is correct
7 Correct 24 ms 3672 KB Output is correct
8 Correct 24 ms 4052 KB Output is correct
9 Correct 24 ms 4012 KB Output is correct
10 Correct 39 ms 5932 KB Output is correct
11 Correct 40 ms 5928 KB Output is correct
12 Correct 35 ms 6088 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 40 ms 5320 KB Output is correct
2 Correct 37 ms 6092 KB Output is correct
3 Correct 35 ms 5884 KB Output is correct
4 Correct 42 ms 6092 KB Output is correct
5 Correct 42 ms 5836 KB Output is correct
6 Correct 34 ms 5152 KB Output is correct
7 Correct 24 ms 3672 KB Output is correct
8 Correct 24 ms 4052 KB Output is correct
9 Correct 24 ms 4012 KB Output is correct
10 Correct 39 ms 5932 KB Output is correct
11 Correct 40 ms 5928 KB Output is correct
12 Correct 35 ms 6088 KB Output is correct
13 Correct 81 ms 6020 KB Output is correct
14 Correct 120 ms 6092 KB Output is correct
15 Correct 436 ms 6092 KB Output is correct
16 Correct 239 ms 6088 KB Output is correct
17 Correct 195 ms 5392 KB Output is correct
18 Correct 293 ms 5584 KB Output is correct
19 Correct 364 ms 5316 KB Output is correct
20 Correct 193 ms 6092 KB Output is correct
21 Correct 26 ms 4028 KB Output is correct
22 Correct 111 ms 4200 KB Output is correct
23 Correct 166 ms 4200 KB Output is correct
24 Correct 53 ms 6044 KB Output is correct
25 Correct 169 ms 6092 KB Output is correct
26 Correct 304 ms 5840 KB Output is correct
27 Correct 27 ms 4196 KB Output is correct
28 Correct 97 ms 4204 KB Output is correct
29 Correct 187 ms 4200 KB Output is correct
30 Correct 42 ms 6348 KB Output is correct
31 Correct 167 ms 6088 KB Output is correct
32 Correct 314 ms 6112 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 1628 KB Output is correct
2 Correct 4 ms 1884 KB Output is correct
3 Correct 28 ms 7512 KB Output is correct
4 Correct 9 ms 1884 KB Output is correct
5 Correct 3 ms 1628 KB Output is correct
6 Correct 4 ms 1628 KB Output is correct
7 Correct 10 ms 4956 KB Output is correct
8 Correct 12 ms 7540 KB Output is correct
9 Correct 3 ms 1884 KB Output is correct
10 Correct 3 ms 1880 KB Output is correct
11 Correct 3 ms 1880 KB Output is correct
12 Correct 4 ms 1884 KB Output is correct
13 Correct 4 ms 1712 KB Output is correct
14 Correct 2 ms 1880 KB Output is correct
15 Correct 7 ms 1872 KB Output is correct
16 Correct 11 ms 1884 KB Output is correct
17 Correct 25 ms 1892 KB Output is correct
18 Correct 4 ms 1624 KB Output is correct
19 Correct 3 ms 1880 KB Output is correct
20 Correct 11 ms 1892 KB Output is correct
21 Correct 15 ms 7524 KB Output is correct
22 Correct 42 ms 4200 KB Output is correct
23 Correct 366 ms 52696 KB Output is correct
24 Correct 28 ms 4204 KB Output is correct
25 Correct 349 ms 4148 KB Output is correct
26 Correct 401 ms 52392 KB Output is correct
27 Correct 389 ms 52840 KB Output is correct
28 Correct 375 ms 52328 KB Output is correct
29 Correct 493 ms 52456 KB Output is correct
30 Correct 34 ms 4208 KB Output is correct
31 Correct 359 ms 52844 KB Output is correct
32 Correct 454 ms 3464 KB Output is correct
33 Correct 243 ms 35976 KB Output is correct
34 Correct 234 ms 4196 KB Output is correct
35 Correct 282 ms 4136 KB Output is correct
36 Correct 402 ms 4196 KB Output is correct
37 Correct 270 ms 52076 KB Output is correct
38 Correct 281 ms 51560 KB Output is correct
39 Correct 296 ms 4196 KB Output is correct
40 Correct 382 ms 4160 KB Output is correct
41 Correct 577 ms 4148 KB Output is correct
42 Correct 460 ms 52436 KB Output is correct
43 Correct 445 ms 52328 KB Output is correct
44 Correct 469 ms 52328 KB Output is correct
45 Correct 459 ms 52324 KB Output is correct
46 Correct 477 ms 52320 KB Output is correct
47 Correct 459 ms 52324 KB Output is correct
48 Correct 471 ms 52328 KB Output is correct
49 Correct 452 ms 52420 KB Output is correct
50 Correct 511 ms 52584 KB Output is correct
51 Correct 21 ms 4204 KB Output is correct
52 Correct 35 ms 4196 KB Output is correct
53 Correct 189 ms 4196 KB Output is correct
54 Correct 262 ms 52840 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 7516 KB Output is correct
2 Correct 52 ms 52580 KB Output is correct
3 Correct 91 ms 85448 KB Output is correct
4 Correct 36 ms 6088 KB Output is correct
5 Correct 22 ms 7512 KB Output is correct
6 Correct 326 ms 52776 KB Output is correct
7 Correct 336 ms 6088 KB Output is correct
8 Correct 34 ms 6088 KB Output is correct
9 Correct 550 ms 85448 KB Output is correct
10 Correct 55 ms 6016 KB Output is correct
11 Correct 74 ms 5156 KB Output is correct
12 Correct 43 ms 5728 KB Output is correct
13 Correct 18 ms 7512 KB Output is correct
14 Correct 305 ms 52840 KB Output is correct
15 Correct 632 ms 85200 KB Output is correct
16 Correct 906 ms 84932 KB Output is correct
17 Correct 952 ms 85448 KB Output is correct
18 Correct 44 ms 5840 KB Output is correct
19 Correct 104 ms 5832 KB Output is correct
20 Correct 983 ms 85384 KB Output is correct
21 Correct 147 ms 20048 KB Output is correct
22 Correct 22 ms 3552 KB Output is correct
23 Correct 2 ms 1628 KB Output is correct
24 Correct 4 ms 1884 KB Output is correct
25 Correct 28 ms 7512 KB Output is correct
26 Correct 9 ms 1884 KB Output is correct
27 Correct 3 ms 1628 KB Output is correct
28 Correct 4 ms 1628 KB Output is correct
29 Correct 10 ms 4956 KB Output is correct
30 Correct 12 ms 7540 KB Output is correct
31 Correct 3 ms 1884 KB Output is correct
32 Correct 3 ms 1880 KB Output is correct
33 Correct 3 ms 1880 KB Output is correct
34 Correct 4 ms 1884 KB Output is correct
35 Correct 4 ms 1712 KB Output is correct
36 Correct 2 ms 1880 KB Output is correct
37 Correct 7 ms 1872 KB Output is correct
38 Correct 11 ms 1884 KB Output is correct
39 Correct 25 ms 1892 KB Output is correct
40 Correct 4 ms 1624 KB Output is correct
41 Correct 3 ms 1880 KB Output is correct
42 Correct 11 ms 1892 KB Output is correct
43 Correct 15 ms 7524 KB Output is correct
44 Correct 40 ms 5320 KB Output is correct
45 Correct 37 ms 6092 KB Output is correct
46 Correct 35 ms 5884 KB Output is correct
47 Correct 42 ms 6092 KB Output is correct
48 Correct 42 ms 5836 KB Output is correct
49 Correct 34 ms 5152 KB Output is correct
50 Correct 24 ms 3672 KB Output is correct
51 Correct 24 ms 4052 KB Output is correct
52 Correct 24 ms 4012 KB Output is correct
53 Correct 39 ms 5932 KB Output is correct
54 Correct 40 ms 5928 KB Output is correct
55 Correct 35 ms 6088 KB Output is correct
56 Correct 81 ms 6020 KB Output is correct
57 Correct 120 ms 6092 KB Output is correct
58 Correct 436 ms 6092 KB Output is correct
59 Correct 239 ms 6088 KB Output is correct
60 Correct 195 ms 5392 KB Output is correct
61 Correct 293 ms 5584 KB Output is correct
62 Correct 364 ms 5316 KB Output is correct
63 Correct 193 ms 6092 KB Output is correct
64 Correct 26 ms 4028 KB Output is correct
65 Correct 111 ms 4200 KB Output is correct
66 Correct 166 ms 4200 KB Output is correct
67 Correct 53 ms 6044 KB Output is correct
68 Correct 169 ms 6092 KB Output is correct
69 Correct 304 ms 5840 KB Output is correct
70 Correct 27 ms 4196 KB Output is correct
71 Correct 97 ms 4204 KB Output is correct
72 Correct 187 ms 4200 KB Output is correct
73 Correct 42 ms 6348 KB Output is correct
74 Correct 167 ms 6088 KB Output is correct
75 Correct 314 ms 6112 KB Output is correct
76 Correct 42 ms 4200 KB Output is correct
77 Correct 366 ms 52696 KB Output is correct
78 Correct 28 ms 4204 KB Output is correct
79 Correct 349 ms 4148 KB Output is correct
80 Correct 401 ms 52392 KB Output is correct
81 Correct 389 ms 52840 KB Output is correct
82 Correct 375 ms 52328 KB Output is correct
83 Correct 493 ms 52456 KB Output is correct
84 Correct 34 ms 4208 KB Output is correct
85 Correct 359 ms 52844 KB Output is correct
86 Correct 454 ms 3464 KB Output is correct
87 Correct 243 ms 35976 KB Output is correct
88 Correct 234 ms 4196 KB Output is correct
89 Correct 282 ms 4136 KB Output is correct
90 Correct 402 ms 4196 KB Output is correct
91 Correct 270 ms 52076 KB Output is correct
92 Correct 281 ms 51560 KB Output is correct
93 Correct 296 ms 4196 KB Output is correct
94 Correct 382 ms 4160 KB Output is correct
95 Correct 577 ms 4148 KB Output is correct
96 Correct 460 ms 52436 KB Output is correct
97 Correct 445 ms 52328 KB Output is correct
98 Correct 469 ms 52328 KB Output is correct
99 Correct 459 ms 52324 KB Output is correct
100 Correct 477 ms 52320 KB Output is correct
101 Correct 459 ms 52324 KB Output is correct
102 Correct 471 ms 52328 KB Output is correct
103 Correct 452 ms 52420 KB Output is correct
104 Correct 511 ms 52584 KB Output is correct
105 Correct 21 ms 4204 KB Output is correct
106 Correct 35 ms 4196 KB Output is correct
107 Correct 189 ms 4196 KB Output is correct
108 Correct 262 ms 52840 KB Output is correct
109 Correct 663 ms 85376 KB Output is correct
110 Correct 799 ms 85392 KB Output is correct
111 Correct 859 ms 5880 KB Output is correct
112 Correct 429 ms 6120 KB Output is correct
113 Correct 959 ms 85048 KB Output is correct
114 Correct 1743 ms 5904 KB Output is correct
115 Correct 901 ms 5844 KB Output is correct
116 Correct 644 ms 6192 KB Output is correct
117 Correct 552 ms 69944 KB Output is correct
118 Correct 176 ms 20052 KB Output is correct
119 Correct 177 ms 20212 KB Output is correct
120 Correct 473 ms 66088 KB Output is correct
121 Correct 379 ms 85900 KB Output is correct
122 Correct 496 ms 6096 KB Output is correct
123 Correct 510 ms 85272 KB Output is correct
124 Correct 489 ms 85184 KB Output is correct
125 Correct 503 ms 85200 KB Output is correct
126 Correct 454 ms 6000 KB Output is correct
127 Correct 644 ms 6112 KB Output is correct
128 Correct 930 ms 6124 KB Output is correct
129 Correct 831 ms 84940 KB Output is correct
130 Correct 836 ms 85040 KB Output is correct
131 Correct 815 ms 84872 KB Output is correct
132 Correct 839 ms 84932 KB Output is correct
133 Correct 844 ms 84940 KB Output is correct
134 Correct 818 ms 85192 KB Output is correct
135 Correct 897 ms 85056 KB Output is correct
136 Correct 871 ms 84940 KB Output is correct
137 Correct 858 ms 84940 KB Output is correct
138 Correct 898 ms 85128 KB Output is correct
139 Correct 863 ms 85280 KB Output is correct
140 Correct 39 ms 6092 KB Output is correct
141 Correct 69 ms 6120 KB Output is correct
142 Correct 908 ms 85040 KB Output is correct
143 Correct 402 ms 85764 KB Output is correct