This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
#define ll long long
struct thing{
double val; pair<ll,ll>node;
bool operator<(const thing& t)const{
return val>t.val;
}
thing(ll _val, pair<ll,ll>_node){
val=_val,node=_node;
}
};
bool vis[100005][32];
double dist[100005][32];
priority_queue<thing>q;
vector<int>oarr;
vector<int>hv;
vector<pair<int,double> >adj[100005];
void Dstra(ll ban){
for(auto& u: hv){
for(int i=0;i<32;i++){
dist[u][i]=0;
q.push(thing(0,{u,i}));
}
}
dist[0][0]=0;
q.push({0,{0,0}});
while(!q.empty()){
thing t=q.top();
q.pop();
pair<ll,ll>cur=t.node;
if(vis[cur.first][cur.second]){
continue;
}
vis[cur.first][cur.second]=1;
for(pair<int,double>& u: adj[cur.first]){
if(u.first==ban){
continue;
}
if(!vis[u.first][cur.second]){
if(dist[cur.first][cur.second]+u.second<dist[u.first][cur.second]){
dist[u.first][cur.second]=dist[cur.first][cur.second]+u.second;
q.push(thing(dist[u.first][cur.second],{u.first,cur.second}));
}
}
if(oarr[u.first]==2&&cur.second+1<=31){
if((dist[cur.first][cur.second]+u.second)/2.0<dist[u.first][cur.second+1]&&!vis[u.first][cur.second+1]){
dist[u.first][cur.second+1]=(dist[cur.first][cur.second]+u.second)/2.0;
q.push(thing(dist[u.first][cur.second+1],{u.first,cur.second+1}));
}
}
}
}
}
bool visi[100005];
void dfs(ll s, ll ban){
visi[s]=1;
for(pair<int,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 n=N,m=M,k=K,h=H;
for(int i=0;i<n;i++){
for(int j=0;j<32;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],(double)c[i]});
adj[y[i]].push_back({x[i],(double)c[i]});
}
dfs(0,h);
for(int i=0;i<n;i++){
if(visi[i]&&arr[i]==0){
hv.push_back(i);
}
}
Dstra(h);
// return -1;
hv.clear();oarr.clear();
double ans=1e18;
for(auto& u: adj[h]){
for(int i=0;i<=k;i++){
if(dist[u.first][i]!=1e18){
// cout<<dist[u.first][i]<<" "<<u.second<<'\n';
ans=min(ans,dist[u.first][i]+u.second);
}
}
}
for(int i=0;i<n;i++){
visi[i]=0;
adj[i].clear();
for(int j=0;j<32;j++){
vis[i][j]=0;
}
}
if(ans==1e18){
return -1;
}
return ans;
}
#ifdef LOCAL
int main() {
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 time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |