이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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)break;
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=M;
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 |
---|
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... |