Submission #1035291

# Submission time Handle Problem Language Result Execution time Memory
1035291 2024-07-26T08:53:44 Z CSQ31 Board Game (JOI24_boardgame) C++17
3 / 100
4000 ms 7092 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){
            for(int x:adj[i]){
                if(rdist[x] > 1){
                    rdist[x] = 1;
                    q.pb(x);
                }

            }
        }
    }
    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'){
            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 11 ms 1884 KB Output is correct
2 Correct 866 ms 4708 KB Output is correct
3 Correct 2293 ms 7092 KB Output is correct
4 Correct 21 ms 6100 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 554 ms 1964 KB Output is correct
2 Execution timed out 4070 ms 4720 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 199 ms 1956 KB Output is correct
2 Execution timed out 4038 ms 4716 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 1640 KB Output is correct
2 Correct 3 ms 1880 KB Output is correct
3 Correct 31 ms 1908 KB Output is correct
4 Incorrect 9 ms 1884 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 27 ms 6100 KB Output is correct
2 Correct 23 ms 5976 KB Output is correct
3 Correct 22 ms 6184 KB Output is correct
4 Correct 31 ms 6040 KB Output is correct
5 Correct 30 ms 5932 KB Output is correct
6 Correct 20 ms 5152 KB Output is correct
7 Incorrect 15 ms 3724 KB Output isn't correct
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 27 ms 6100 KB Output is correct
2 Correct 23 ms 5976 KB Output is correct
3 Correct 22 ms 6184 KB Output is correct
4 Correct 31 ms 6040 KB Output is correct
5 Correct 30 ms 5932 KB Output is correct
6 Correct 20 ms 5152 KB Output is correct
7 Incorrect 15 ms 3724 KB Output isn't correct
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 1640 KB Output is correct
2 Correct 3 ms 1880 KB Output is correct
3 Correct 31 ms 1908 KB Output is correct
4 Incorrect 9 ms 1884 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 11 ms 1884 KB Output is correct
2 Correct 866 ms 4708 KB Output is correct
3 Correct 2293 ms 7092 KB Output is correct
4 Correct 21 ms 6100 KB Output is correct
5 Correct 554 ms 1964 KB Output is correct
6 Execution timed out 4070 ms 4720 KB Time limit exceeded
7 Halted 0 ms 0 KB -