#include <bits/stdc++.h>
#define ll long long
using namespace std;
const ll N=1e6+9,M=2e18+9,mod=1e9+7;
ll vis[N],dis0[N],dish[N],nd;
priority_queue<pair<ll,ll>>q;
vector<pair<ll,ll>>v[N];
double solve(int n, int m, int k, int h, vector<int> x, vector<int>y, vector<int> c, vector<int> arr)
{
for(ll i=0;i<n;++i)v[i].clear();
for(ll i=0;i<m;++i)
{
v[x[i]].push_back({c[i],y[i]});
v[y[i]].push_back({c[i],x[i]});
}
for(ll i=0;i<n;++i)dis0[i]=M,dish[i]=M;
dis0[0]=0,dish[h]=0;
q.push({0,0});
for(ll i=0;i<n;++i)vis[i]=0;
while(q.size())
{
nd=q.top().second;
q.pop();
if(vis[nd])continue;
vis[nd]=1;
if(nd==h)continue;
for(auto i:v[nd])
{
if(dis0[nd]+i.first<dis0[i.second])
{
dis0[i.second]=dis0[nd]+i.first;
q.push({-dis0[i.second],i.second});
}
}
}
while(q.size())q.pop();
q.push({0,h});
for(ll i=0;i<n;++i)vis[i]=0;
while(q.size())
{
nd=q.top().second;
q.pop();
if(vis[nd])continue;
vis[nd]=1;
for(auto i:v[nd])
{
if(dish[nd]+i.first<dish[i.second])
{
dish[i.second]=dish[nd]+i.first;
q.push({-dish[i.second],i.second});
}
}
}
ll ans=dis0[h];
for(ll i=0;i<n;++i)
{
if(max(dis0[i],dish[i])==M)continue;
//cout<<i<<' '<<dis0[i]<<' '<<dish[i]<<'\n';
if(!arr[i])ans=min(ans,dish[i]);
else ans=min(ans,dis0[i]+dish[i]);
}
if(ans==M)ans=-1;
return ans;
}
/*
int main()
{
ios::sync_with_stdio(0);
//cin.tie(0);cout.tie(0);
int T;
cin>>T;
while (T--){
int n,m,K,H;
cin>>n>>m>>K>>H;
vector<int> x(m);
vector<int> y(m);
vector<int> c(m);
vector<int> arr(n);
for (int i=0;i<n;i++)cin>>arr[i];
for (int i=0;i<m;i++)
cin>>x[i]>>y[i]>>c[i];
cout<<solve(n, m, K, H, x, y, c, arr)<<'\n';
}
}
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
20 ms |
29272 KB |
Wrong Answer. |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
24 ms |
29528 KB |
Correct. |
2 |
Correct |
28 ms |
29532 KB |
Correct. |
3 |
Correct |
28 ms |
29520 KB |
Correct. |
4 |
Correct |
30 ms |
29528 KB |
Correct. |
5 |
Correct |
28 ms |
29444 KB |
Correct. |
6 |
Correct |
27 ms |
32344 KB |
Correct. |
7 |
Correct |
32 ms |
32348 KB |
Correct. |
8 |
Correct |
18 ms |
33112 KB |
Correct. |
9 |
Correct |
25 ms |
29404 KB |
Correct. |
10 |
Correct |
28 ms |
29276 KB |
Correct. |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
28 ms |
29272 KB |
Correct. |
2 |
Correct |
32 ms |
29272 KB |
Correct. |
3 |
Correct |
29 ms |
29528 KB |
Correct. |
4 |
Correct |
27 ms |
29276 KB |
Correct. |
5 |
Correct |
27 ms |
29420 KB |
Correct. |
6 |
Correct |
11 ms |
32092 KB |
Correct. |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
29 ms |
35920 KB |
Wrong Answer. |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
24 ms |
29528 KB |
Correct. |
2 |
Correct |
26 ms |
29536 KB |
Correct. |
3 |
Correct |
25 ms |
29532 KB |
Correct. |
4 |
Correct |
27 ms |
32604 KB |
Correct. |
5 |
Correct |
22 ms |
29276 KB |
Correct. |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
25 ms |
29556 KB |
Correct. |
2 |
Correct |
26 ms |
29528 KB |
Correct. |
3 |
Correct |
40 ms |
37812 KB |
Correct. |
4 |
Correct |
20 ms |
32360 KB |
Correct. |
5 |
Correct |
26 ms |
29416 KB |
Correct. |
6 |
Correct |
25 ms |
29532 KB |
Correct. |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
32 ms |
29528 KB |
Wrong Answer. |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
25 ms |
29528 KB |
Wrong Answer. |
2 |
Halted |
0 ms |
0 KB |
- |