#include "cyberland.h"
#include <bits/stdc++.h>
#pragma GCC optimize "Ofast"
using namespace std;
double sub_2_5(int N, int M, int K, int H, std::vector<int> x, std::vector<int> y, std::vector<int> c, std::vector<int> arr){
struct ii{
int id;
double w;
};
struct cmp{
bool operator () (ii A, ii B){
return A.w > B.w;
}
};
priority_queue<ii, vector<ii>, cmp> pq;
vector<double> dp(N, (double)(1e15));
vector<vector<pair<int,int>>> G(N);
for(int i=0;i<M;i++){
G[x[i]].push_back({y[i], c[i]});
G[y[i]].push_back({x[i], c[i]});
}
dp[0] = 0;
pq.push({0,dp[0]});
while(pq.size()){
ii u = pq.top();
pq.pop();
if(dp[u.id] < u.w) continue;
for(pair<int,int> v: G[u.id]){
if(dp[u.id] + v.second < dp[v.first]){
dp[v.first] = dp[u.id] + v.second;
pq.push({v.first, dp[v.first]});
}
}
}
if(dp[H] == (double)(1e15)) dp[H] = -1;
return dp[H];
}
double sub_3_6(int N, int M, int K, int H, std::vector<int> x, std::vector<int> y, std::vector<int> c, std::vector<int> arr){
struct ii{
int id;
double w;
};
struct cmp{
bool operator () (ii A, ii B){
return A.w > B.w;
}
};
priority_queue<ii, vector<ii>, cmp> pq;
vector<double> dp(N, (double)(1e15));
vector<vector<pair<int,int>>> G(N);
for(int i=0;i<M;i++){
G[x[i]].push_back({y[i], c[i]});
G[y[i]].push_back({x[i], c[i]});
}
dp[0] = 0;
pq.push({0,dp[0]});
vector<bool> vis(N,false);
queue<int> Q;
Q.push(0);
vis[0] = true;
while(Q.size()){
int u = Q.front();
Q.pop();
for(pair<int,int> v: G[u]){
if(vis[v.first] == true || v.first == H) continue;
vis[v.first] = true;
Q.push(v.first);
}
}
for(int i=1;i<N;i++){
if(arr[i] == 0 && vis[i] == true){
dp[i] = 0;
pq.push({i,dp[i]});
}
}
while(pq.size()){
ii u = pq.top();
pq.pop();
if(dp[u.id] < u.w) continue;
for(pair<int,int> v: G[u.id]){
if(dp[u.id] + v.second < dp[v.first]){
dp[v.first] = dp[u.id] + v.second;
pq.push({v.first, dp[v.first]});
}
}
}
if(dp[H] == (double)(1e15)) dp[H] = -1;
return dp[H];
}
double sub_1_4_7(int N, int M, int K, int H, std::vector<int> x, std::vector<int> y, std::vector<int> c, std::vector<int> arr){
struct ii{
int id;
int div2;
double w;
};
struct cmp{
bool operator () (ii A, ii B){
return A.w > B.w;
}
};
priority_queue<ii, vector<ii>, cmp> pq;
vector<vector<double>> dp(N, vector<double> (K+2, (double)(1e15)));
vector<vector<pair<int,int>>> G(N);
for(int i=0;i<M;i++){
G[x[i]].push_back({y[i], c[i]});
G[y[i]].push_back({x[i], c[i]});
}
dp[0][0] = 0;
pq.push({0,0,dp[0][0]});
vector<bool> vis(N,false);
queue<int> Q;
Q.push(0);
vis[0] = true;
while(Q.size()){
int u = Q.front();
Q.pop();
for(pair<int,int> v: G[u]){
if(vis[v.first] == true || v.first == H) continue;
vis[v.first] = true;
Q.push(v.first);
}
}
for(int i=1;i<N;i++){
if(arr[i] == 0 && vis[i] == true){
dp[i][0] = 0;
pq.push({i,0,dp[i][0]});
}
}
while(pq.size()){
ii u = pq.top();
pq.pop();
if(dp[u.id][u.div2] < u.w) continue;
if(u.id == H) continue;
for(pair<int,int> v: G[u.id]){
if(dp[u.id][u.div2] + v.second < dp[v.first][u.div2]){
dp[v.first][u.div2] = dp[u.id][u.div2] + v.second;
pq.push({v.first, u.div2, dp[v.first][u.div2]});
}
double tmp = (dp[u.id][u.div2] + v.second) / 2.0;
if(u.div2 + 1 <= K && arr[v.first] == 2){
if(tmp < dp[v.first][u.div2+1]){
dp[v.first][u.div2+1] = tmp;
pq.push({v.first, u.div2+1, dp[v.first][u.div2+1]});
}
}
}
}
double ans = (double)(1e15);
for(int i=0;i<=K;i++) ans = min(ans, dp[H][i]);
if(ans == (double)(1e15)) ans = -1;
return ans;
}
double sub_8(int N, int M, int K, int H, std::vector<int> x, std::vector<int> y, std::vector<int> c, std::vector<int> arr){
struct ii{
int id;
int div2;
double w;
};
struct cmp{
bool operator () (ii A, ii B){
return A.w > B.w;
}
};
priority_queue<ii, vector<ii>, cmp> pq;
vector<vector<double>> dp(N, vector<double> (K+2, (double)(1e15)));
vector<vector<int>> vis_go(N, vector<int> (K+2, 0));
vector<vector<pair<int,int>>> G(N);
for(int i=0;i<M;i++){
G[x[i]].push_back({y[i], c[i]});
G[y[i]].push_back({x[i], c[i]});
}
dp[0][0] = 0;
pq.push({0,0,dp[0][0]});
vis_go[0][0] = 1;
vector<bool> vis(N,false);
queue<int> Q;
Q.push(0);
vis[0] = true;
while(Q.size()){
int u = Q.front();
Q.pop();
for(pair<int,int> v: G[u]){
if(vis[v.first] == true || v.first == H) continue;
vis[v.first] = true;
Q.push(v.first);
}
}
for(int i=1;i<N;i++){
if(arr[i] == 0 && vis[i] == true){
dp[i][0] = 0;
vis_go[i][0] = 1;
pq.push({i,0,dp[i][0]});
}
}
while(pq.size()){
ii u = pq.top();
pq.pop();
if(dp[u.id][u.div2] < u.w) continue;
if(u.id == H) continue;
for(pair<int,int> v: G[u.id]){
if(dp[u.id][u.div2] + v.second < dp[v.first][u.div2] && vis_go[v.first][u.div2+1] < 35){
dp[v.first][u.div2] = dp[u.id][u.div2] + v.second;
vis_go[v.first][u.div2]++;
pq.push({v.first, u.div2, dp[v.first][u.div2]});
}
double tmp = (dp[u.id][u.div2] + v.second) / 2.0;
if(u.div2 + 1 <= K && arr[v.first] == 2){
if(tmp < dp[v.first][u.div2+1] && vis_go[v.first][u.div2+1] < 35){
dp[v.first][u.div2+1] = tmp;
vis_go[v.first][u.div2+1]++;
pq.push({v.first, u.div2+1, dp[v.first][u.div2+1]});
}
}
}
}
double ans = (double)(1e15);
for(int i=0;i<=K;i++) ans = min(ans, dp[H][i]);
if(ans == (double)(1e15)) ans = -1;
return ans;
}
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 check_sub_2_5 = 0;
for(int i=0;i<N;i++) check_sub_2_5 += (arr[i] == 1);
if(check_sub_2_5 == N) return sub_2_5(N,M,K,H,x,y,c,arr);
int check_sub_3_6 = 0;
for(int i=0;i<N;i++) check_sub_3_6 += (arr[i] != 2);
if(check_sub_3_6 == N) return sub_3_6(N,M,K,H,x,y,c,arr);
if(K <= 30) return sub_1_4_7(N,M,K,H,x,y,c,arr);
return sub_8(N,M,min(K,100),H,x,y,c,arr);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
468 KB |
Correct. |
2 |
Correct |
29 ms |
392 KB |
Correct. |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
30 ms |
460 KB |
Correct. |
2 |
Correct |
30 ms |
468 KB |
Correct. |
3 |
Correct |
26 ms |
456 KB |
Correct. |
4 |
Correct |
26 ms |
444 KB |
Correct. |
5 |
Correct |
27 ms |
452 KB |
Correct. |
6 |
Correct |
25 ms |
1520 KB |
Correct. |
7 |
Correct |
30 ms |
1432 KB |
Correct. |
8 |
Correct |
13 ms |
2644 KB |
Correct. |
9 |
Correct |
26 ms |
368 KB |
Correct. |
10 |
Correct |
31 ms |
500 KB |
Correct. |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
28 ms |
432 KB |
Correct. |
2 |
Correct |
26 ms |
448 KB |
Correct. |
3 |
Correct |
25 ms |
480 KB |
Correct. |
4 |
Correct |
29 ms |
396 KB |
Correct. |
5 |
Correct |
30 ms |
376 KB |
Correct. |
6 |
Correct |
7 ms |
1364 KB |
Correct. |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
272 ms |
27344 KB |
Correct. |
2 |
Correct |
302 ms |
1068 KB |
Correct. |
3 |
Correct |
255 ms |
1144 KB |
Correct. |
4 |
Correct |
273 ms |
1168 KB |
Correct. |
5 |
Correct |
244 ms |
484 KB |
Correct. |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
500 KB |
Correct. |
2 |
Correct |
26 ms |
420 KB |
Correct. |
3 |
Correct |
29 ms |
596 KB |
Correct. |
4 |
Correct |
25 ms |
1492 KB |
Correct. |
5 |
Correct |
24 ms |
340 KB |
Correct. |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
35 ms |
484 KB |
Correct. |
2 |
Correct |
23 ms |
500 KB |
Correct. |
3 |
Correct |
45 ms |
9092 KB |
Correct. |
4 |
Correct |
15 ms |
1252 KB |
Correct. |
5 |
Correct |
25 ms |
380 KB |
Correct. |
6 |
Correct |
26 ms |
476 KB |
Correct. |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
334 ms |
1532 KB |
Correct. |
2 |
Correct |
49 ms |
2360 KB |
Correct. |
3 |
Correct |
774 ms |
29696 KB |
Correct. |
4 |
Correct |
561 ms |
8020 KB |
Correct. |
5 |
Correct |
1244 ms |
50000 KB |
Correct. |
6 |
Correct |
543 ms |
41240 KB |
Correct. |
7 |
Correct |
534 ms |
8140 KB |
Correct. |
8 |
Correct |
532 ms |
1760 KB |
Correct. |
9 |
Correct |
267 ms |
2076 KB |
Correct. |
10 |
Correct |
272 ms |
1648 KB |
Correct. |
11 |
Correct |
549 ms |
968 KB |
Correct. |
12 |
Correct |
319 ms |
1592 KB |
Correct. |
13 |
Correct |
299 ms |
1704 KB |
Correct. |
14 |
Correct |
488 ms |
11084 KB |
Correct. |
15 |
Correct |
542 ms |
3352 KB |
Correct. |
16 |
Correct |
263 ms |
1540 KB |
Correct. |
17 |
Correct |
344 ms |
1532 KB |
Correct. |
18 |
Correct |
329 ms |
1512 KB |
Correct. |
19 |
Correct |
775 ms |
1580 KB |
Correct. |
20 |
Correct |
27 ms |
744 KB |
Correct. |
21 |
Correct |
20 ms |
1056 KB |
Correct. |
22 |
Correct |
35 ms |
3272 KB |
Correct. |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1233 ms |
5020 KB |
Correct. |
2 |
Correct |
187 ms |
8544 KB |
Correct. |
3 |
Correct |
949 ms |
137352 KB |
Correct. |
4 |
Incorrect |
1082 ms |
8692 KB |
Wrong Answer. |
5 |
Halted |
0 ms |
0 KB |
- |