# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
922150 | tosivanmak | 사이버랜드 (APIO23_cyberland) | C++17 | 0 ms | 0 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const 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][72];
double dist[100005][72];
priority_queue<thing>q;
vector<int>oarr;
vector<int>hv;
vector<pair<int,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,double>& u: adj[cur]){
if(u.first==ban){
continue;
}
if(oarr[u.first]==2&&lvl+1<=70){
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,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,70);
for(int i=0;i<n;i++){
for(int j=0;j<72;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);
}
}
for(auto& u: hv){
for(int i=0;i<72;i++){
dist[u][i]=0;
}
}
dist[0][0]=0;
for(int i=0;i<=70;i++){
Dstra(h,i);
}
// return -1;
hv.clear();oarr.clear();
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<72;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));
}
}