Submission #1185444

#TimeUsernameProblemLanguageResultExecution timeMemory
1185444SalihSahinVoting Cities (NOI22_votingcity)C++20
100 / 100
911 ms5276 KiB
#include "bits/stdc++.h"
#define pb push_back
#define int long long
using namespace std;

const int inf = 1e16;

int32_t main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);

    int n, e, k;
    cin>>n>>e>>k;
    vector<pair<int, int> > adj[n];
    vector<int> vote(n);
    for(int i = 0; i < k; i++){
        int x;
        cin>>x;
        vote[x] = 1;
    }

    for(int i = 0; i < e; i++){
        int u, v, c;
        cin>>u>>v>>c;
        adj[u].pb({v, c});
    }

    priority_queue<array<int, 3> > pq;
    vector<vector<int> > dis(n, vector<int>(32, inf)), vis(n, vector<int>(32));
    
    int q;
    cin>>q;
    while(q--){
        int s;
        cin>>s;

        array<int, 5> p;
        int bt = 0;
        for(int i = 0; i < 5; i++){
            cin>>p[i];
            if(p[i] != -1) bt += (1 << i);
        }
        
        for(int i = 0; i < 32; i++){
            if((bt & i) != i) continue;
            int add = 0;
            for(int j = 0; j < 5; j++){
                if(i & (1 << j)) add += p[j];
            }
            pq.push({-add, s, i});
            dis[s][i] = add;
        }

        int ans = inf;
        while(!pq.empty()){
            int node = pq.top()[1], b = pq.top()[2];
            pq.pop();

            if(vote[node]){
                ans = min(dis[node][b], ans);
            }

            if(vis[node][b] || ans != inf) continue;
            vis[node][b] = 1;

            for(auto itr: adj[node]){
                int c = itr.second;

                for(int sec = 0; sec < 5; sec++){
                    if(b & (1 << sec)){
                        if(dis[itr.first][b - (1 << sec)] > dis[node][b] + (c - (c / 10 * (sec + 1)))){
                            dis[itr.first][b - (1 << sec)] = dis[node][b] + (c - (c / 10 * (sec + 1)));
                            pq.push({-dis[itr.first][b - (1 << sec)], itr.first, b - (1 << sec)});
                        }
                    }
                }

                if(dis[itr.first][b] > dis[node][b] + c){
                    dis[itr.first][b] = dis[node][b] + c;
                    pq.push({-dis[itr.first][b], itr.first, b});
                }
            }
        }

        for(int i = 0; i < n; i++){
            for(int j = 0; j < 32; j++){
                dis[i][j] = inf;
                vis[i][j] = 0;
            }
        }

        if(ans == inf) cout<<-1<<"\n";
        else cout<<ans<<"\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...