Submission #1035300

# Submission time Handle Problem Language Result Execution time Memory
1035300 2024-07-26T09:04:17 Z CSQ31 Board Game (JOI24_boardgame) C++17
54 / 100
4000 ms 7096 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];
int main()
{
    owo
    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

    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]]);
    }
    //for(int i=0;i<n;i++)cout<<bdist[i]<<" "<<rdist[i]<<'\n';
    sort(all(f));
    for(int i=0;i<k;i++){
        ll c = 2*(k-1) - i;
        //cout<<"iteration: "<<i<<'\n';
        //cout<<sum<<" "<<c<<'\n';
        vector<ll>dist(n,1e18);
        priority_queue<PII,vector<PII>,greater<PII>>pq;
        for(int v:ones){ 
            //need to have at least more than one game started
            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]);
        }
        //cout<<'\n';
        if(i!=k-1)sum+=f[i];
    }
    for(int i=0;i<n;i++)cout<<ans[i]<<'\n';
}
# Verdict Execution time Memory Grader output
1 Correct 10 ms 1972 KB Output is correct
2 Correct 730 ms 4720 KB Output is correct
3 Correct 2165 ms 7096 KB Output is correct
4 Correct 21 ms 6100 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 542 ms 1880 KB Output is correct
2 Execution timed out 4022 ms 4716 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 200 ms 1952 KB Output is correct
2 Execution timed out 4065 ms 4720 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 1628 KB Output is correct
2 Correct 3 ms 1912 KB Output is correct
3 Correct 30 ms 1896 KB Output is correct
4 Correct 9 ms 1884 KB Output is correct
5 Correct 4 ms 1812 KB Output is correct
6 Correct 3 ms 1628 KB Output is correct
7 Correct 183 ms 1628 KB Output is correct
8 Correct 516 ms 1928 KB Output is correct
9 Correct 2 ms 1884 KB Output is correct
10 Correct 2 ms 1884 KB Output is correct
11 Correct 4 ms 1756 KB Output is correct
12 Correct 3 ms 1884 KB Output is correct
13 Correct 4 ms 1888 KB Output is correct
14 Correct 2 ms 1880 KB Output is correct
15 Correct 6 ms 1884 KB Output is correct
16 Correct 11 ms 1904 KB Output is correct
17 Correct 24 ms 1880 KB Output is correct
18 Correct 2 ms 1884 KB Output is correct
19 Correct 3 ms 1772 KB Output is correct
20 Correct 11 ms 1900 KB Output is correct
21 Correct 277 ms 1884 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 27 ms 6096 KB Output is correct
2 Correct 23 ms 6052 KB Output is correct
3 Correct 22 ms 6160 KB Output is correct
4 Correct 32 ms 6176 KB Output is correct
5 Correct 30 ms 5924 KB Output is correct
6 Correct 21 ms 5144 KB Output is correct
7 Correct 14 ms 3672 KB Output is correct
8 Correct 17 ms 4192 KB Output is correct
9 Correct 18 ms 4192 KB Output is correct
10 Correct 28 ms 5936 KB Output is correct
11 Correct 29 ms 5940 KB Output is correct
12 Correct 25 ms 6100 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 27 ms 6096 KB Output is correct
2 Correct 23 ms 6052 KB Output is correct
3 Correct 22 ms 6160 KB Output is correct
4 Correct 32 ms 6176 KB Output is correct
5 Correct 30 ms 5924 KB Output is correct
6 Correct 21 ms 5144 KB Output is correct
7 Correct 14 ms 3672 KB Output is correct
8 Correct 17 ms 4192 KB Output is correct
9 Correct 18 ms 4192 KB Output is correct
10 Correct 28 ms 5936 KB Output is correct
11 Correct 29 ms 5940 KB Output is correct
12 Correct 25 ms 6100 KB Output is correct
13 Correct 65 ms 6088 KB Output is correct
14 Correct 109 ms 6092 KB Output is correct
15 Correct 427 ms 5932 KB Output is correct
16 Correct 211 ms 6092 KB Output is correct
17 Correct 186 ms 5508 KB Output is correct
18 Correct 287 ms 5580 KB Output is correct
19 Correct 358 ms 5316 KB Output is correct
20 Correct 186 ms 6184 KB Output is correct
21 Correct 21 ms 4044 KB Output is correct
22 Correct 93 ms 4204 KB Output is correct
23 Correct 154 ms 4164 KB Output is correct
24 Correct 39 ms 6052 KB Output is correct
25 Correct 155 ms 6104 KB Output is correct
26 Correct 281 ms 6076 KB Output is correct
27 Correct 19 ms 4208 KB Output is correct
28 Correct 97 ms 4208 KB Output is correct
29 Correct 183 ms 4200 KB Output is correct
30 Correct 27 ms 6092 KB Output is correct
31 Correct 144 ms 6172 KB Output is correct
32 Correct 296 ms 6088 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 1628 KB Output is correct
2 Correct 3 ms 1912 KB Output is correct
3 Correct 30 ms 1896 KB Output is correct
4 Correct 9 ms 1884 KB Output is correct
5 Correct 4 ms 1812 KB Output is correct
6 Correct 3 ms 1628 KB Output is correct
7 Correct 183 ms 1628 KB Output is correct
8 Correct 516 ms 1928 KB Output is correct
9 Correct 2 ms 1884 KB Output is correct
10 Correct 2 ms 1884 KB Output is correct
11 Correct 4 ms 1756 KB Output is correct
12 Correct 3 ms 1884 KB Output is correct
13 Correct 4 ms 1888 KB Output is correct
14 Correct 2 ms 1880 KB Output is correct
15 Correct 6 ms 1884 KB Output is correct
16 Correct 11 ms 1904 KB Output is correct
17 Correct 24 ms 1880 KB Output is correct
18 Correct 2 ms 1884 KB Output is correct
19 Correct 3 ms 1772 KB Output is correct
20 Correct 11 ms 1900 KB Output is correct
21 Correct 277 ms 1884 KB Output is correct
22 Correct 33 ms 4204 KB Output is correct
23 Correct 1575 ms 4208 KB Output is correct
24 Correct 20 ms 4208 KB Output is correct
25 Correct 347 ms 4208 KB Output is correct
26 Correct 664 ms 4216 KB Output is correct
27 Execution timed out 4032 ms 4716 KB Time limit exceeded
28 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 10 ms 1972 KB Output is correct
2 Correct 730 ms 4720 KB Output is correct
3 Correct 2165 ms 7096 KB Output is correct
4 Correct 21 ms 6100 KB Output is correct
5 Correct 542 ms 1880 KB Output is correct
6 Execution timed out 4022 ms 4716 KB Time limit exceeded
7 Halted 0 ms 0 KB -