Submission #922149

#TimeUsernameProblemLanguageResultExecution timeMemory
922149tosivanmakCyberland (APIO23_cyberland)C++17
68 / 100
3028 ms125936 KiB
#include<bits/stdc++.h> using namespace std; #define ll long long const long double eps=1e-10; ll n; struct thing{ long double val; ll node; bool operator<(const thing& t)const{ return (val)>t.val; } thing(long double _val, ll _node){ val=_val,node=_node; } }; bool vis[100005][66]; long double dist[100005][66]; priority_queue<thing>q; vector<int>oarr; vector<int>hv; vector<pair<int,long double> >adj[100005]; void Dstra(ll ban, ll lvl){ for(int i=0;i<n;i++){ if(i!=ban&&dist[i][lvl]!=1e18){ q.push(thing(dist[i][lvl],i)); } } while(!q.empty()){ thing t=q.top(); q.pop(); // cout<<t.val<<'\n'; ll cur=t.node; if(vis[cur][lvl]){ continue; } vis[cur][lvl]=1; for(pair<int,long double>& u: adj[cur]){ if(u.first==ban){ continue; } if(oarr[u.first]==2&&lvl+1<=65){ if(((dist[cur][lvl]+u.second)/2.0-eps)<dist[u.first][lvl+1]&&!vis[u.first][lvl+1]){ dist[u.first][lvl+1]=(dist[cur][lvl]+u.second)/2.0; // q.push(thing(dist[u.first][cur.second+1],{u.first,cur.second+1})); } } if(!vis[u.first][lvl]){ if(dist[cur][lvl]+u.second<dist[u.first][lvl]){ dist[u.first][lvl]=dist[cur][lvl]+u.second; q.push(thing(dist[u.first][lvl],u.first)); } } } } } bool visi[100005]; void dfs(ll s, ll ban){ visi[s]=1; for(pair<int,long double>& u: adj[s]){ if(!visi[u.first]&&ban!=u.first){ dfs(u.first,ban); } } } double solve(int N, int M, int K, int H, vector<int> x, vector<int> y, vector<int> c, vector<int> arr) { oarr=arr; int m=M,k=K,h=H;n=N; k=min(k,65); for(int i=0;i<n;i++){ for(int j=0;j<66;j++){ dist[i][j]=1e18; vis[i][j]=0; } visi[i]=0; } for(int i=0;i<m;i++){ adj[x[i]].push_back({y[i],(long double)c[i]}); adj[y[i]].push_back({x[i],(long double)c[i]}); } dfs(0,h); for(int i=0;i<n;i++){ if(visi[i]&&arr[i]==0){ hv.push_back(i); } } for(auto& u: hv){ for(int i=0;i<66;i++){ dist[u][i]=0; } } dist[0][0]=0; for(int i=0;i<=65;i++){ Dstra(h,i); } // return -1; hv.clear();oarr.clear(); long double ans=1e18; for(auto& u: adj[h]){ for(int i=0;i<=k;i++){ // cout<<dist[u.first][i]<<" "<<u.second<<'\n'; ans=min(ans,dist[u.first][i]+u.second); } } // for(int i=0;i<n;i++){ // for(int j=0;j<=3;j++){ // cout<<dist[i][j]<<" "; // } // cout<<'\n'; // } for(int i=0;i<n;i++){ visi[i]=0; adj[i].clear(); for(int j=0;j<66;j++){ vis[i][j]=0; } } if(1e18-ans<=eps){ return -1; } return ans; } #ifdef LOCAL int main() { freopen("a.in","r",stdin); freopen("a.out","w",stdout); int T; assert(1 == scanf("%d", &T)); while (T--){ int N,M,K,H; assert(4 == scanf("%d %d %d\n%d", &N, &M, &K, &H)); std::vector<int> x(M); std::vector<int> y(M); std::vector<int> c(M); std::vector<int> arr(N); for (int i=0;i<N;i++) assert(1 == scanf("%d", &arr[i])); for (int i=0;i<M;i++) assert(3 == scanf("%d %d %d", &x[i], &y[i], &c[i])); printf("%.12lf\n", solve(N, M, K, H, x, y, c, arr)); } } #endif
#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...