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 "cyberland.h"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll INF=1e13;
double solve(int N, int M, int K, int H, std::vector<int> x, std::vector<int> y, std::vector<int> c, std::vector<int> arr) {
int n=N, m=M, k=K, h=H;
if(n<=3){
if(h==0) return 0;
vector<vector<double>> ct(n, vector<double> (n, -1));
for(int i=0; i<m; i++){
ct[x[i]][y[i]]=c[i];
ct[y[i]][x[i]]=c[i];
}
double res=-1;
if(ct[0][h]>=0){
res=ct[0][h];
}
if(n<3) return res;
if(ct[0][3-h]>=0 && ct[3-h][h]>=0){
if(arr[3-h]==1){
if(res<-0.5){
res=ct[0][3-h]+ct[3-h][h];
}
else{
res=min(res, ct[0][3-h]+ct[3-h][h]);
}
}
else if(arr[3-h]==0){
if(res<-0.5){
res=ct[3-h][h];
}
else{
res=min(res, ct[3-h][h]);
}
}
else{
if(k>0){
if(res<-0.5){
res=(double)ct[0][3-h]/2+ct[3-h][h];
}
else{
res=min(res, (double)ct[0][3-h]/2+ct[3-h][h]);
}
}
else{
if(res<-0.5){
res=ct[0][3-h]+ct[3-h][h];
}
else{
res=min(res, ct[0][3-h]+ct[3-h][h]);
}
}
}
}
return res;
}
double res=-1;
vector<vector<pair<int, double>>> adj(n);
for(int i=0; i<m; i++){
adj[x[i]].push_back({y[i], c[i]});
adj[y[i]].push_back({x[i], c[i]});
}
vector<double> d(n, INF);
vector<bool> ch(n, false);
priority_queue<pair<double, int>> pq;
priority_queue<int> pq1;
d[0]=0;
pq1.push(0);
while(!pq1.empty()){
int t=pq1.top(); pq1.pop();
if(ch[t] || t==h) continue;
ch[t]=true;
if(arr[t]==0){
d[t]=0;
}
for(auto x: adj[t]){
pq1.push(x.first);
}
}
for(int i=0; i<n; i++){
if(d[i]==0){
pq.push({0, i});
// cout << "-----\n";
}
}
ch.clear();
ch.resize(n, false);
pq.push({0, 0});
while(!pq.empty()){
int t=pq.top().second; pq.pop();
if(ch[t]) continue;
ch[t]=true;
for(auto x: adj[t]){
if(d[t]+x.second<d[x.first]){
// cout << t << ' ' << x.first << ' ' << d[t]+x.second << '\n';
pq.push({-(d[t]+x.second), x.first});
d[x.first]=d[t]+x.second;
}
}
}
if(d[h]==INF){
return -1;
}
// for(auto x: d) cout << x << ' ';
// cout << '\n';
return d[h];
}
Compilation message (stderr)
cyberland.cpp: In function 'double solve(int, int, int, int, std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>)':
cyberland.cpp:63:12: warning: unused variable 'res' [-Wunused-variable]
63 | double res=-1;
| ^~~
# | 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... |