#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pi;
typedef long long ll;
typedef vector<int> vi;
#define mp make_pair
#define f first
#define s second
#define sz(x) (int)(x).size()
const ll INF = 1e18;
const int MOD = 1e9+7;
const int mx = 505;
vi dx = vi{1, 0, -1, 0};
vi dy = vi{0, 1, 0, -1};
ll walking[mx][mx];
ll kicking[mx][mx][4];
int H, W;
ll A, B, C;
int N;
pi ST[100005];
bool isGood(pi a){
return (0 <= a.f) && (a.f <= H) && (0 <= a.s) && (a.s <= W);
}
int closest_dist[mx][mx];
void genClosestDist(){
for(int i = 0; i <= H; i++){
for(int j = 0; j <= W; j++){
closest_dist[i][j] = MOD;
}
}
queue<pair<int, pi>> q;
for(int i = 1; i <= N; i++){
closest_dist[ST[i].f][ST[i].s] = 0;
q.push(mp(0, ST[i]));
}
while(sz(q)){
pair<int, pi> a = q.front(); q.pop();
pi cur_pos = a.s;
int dist = a.f;
if(closest_dist[cur_pos.f][cur_pos.s] < dist) continue;
for(int d = 0; d < 4; d++){
pi new_pos = mp(cur_pos.f+dx[d], cur_pos.s+dy[d]);
if(!isGood(new_pos)) continue;
int new_dist = dist+1;
if(closest_dist[new_pos.f][new_pos.s] > new_dist){
closest_dist[new_pos.f][new_pos.s] = new_dist;
q.push(mp(new_dist, new_pos));
}
}
}
}
int main(){
cin >> H >> W;
cin >> A >> B >> C;
cin >> N;
for(int i = 1; i <= N; i++){
cin >> ST[i].f >> ST[i].s;
}
genClosestDist();
for(int i = 0; i <= H; i++){
for(int j = 0; j <= W; j++){
for(int d = 0; d < 4; d++){
kicking[i][j][d] = INF;
}
walking[i][j] = INF;
}
}
priority_queue<pair<ll, vi>, vector<pair<ll, vi>>, greater<pair<ll, vi>>> pq;
walking[ST[1].f][ST[1].s] = 0;
pq.push(mp(0, vi{0, ST[1].f, ST[1].s})); //type 0 is walking, 1 is kicking
while(sz(pq)){
ll dist = pq.top().f;
vi state = pq.top().s;
pi cur_pos = mp(state[1], state[2]);
pq.pop();
if(state[0] == 0){ //walking
if(walking[cur_pos.f][cur_pos.s] < dist) continue;
for(int d = 0; d < 4; d++){
pi new_pos = mp(cur_pos.f+dx[d], cur_pos.s+dy[d]);
ll new_dist = dist+C;
if(!isGood(new_pos)) continue;
if(walking[new_pos.f][new_pos.s] <= new_dist) continue;
walking[new_pos.f][new_pos.s] = new_dist;
pq.push(mp(new_dist, vi{0, new_pos.f, new_pos.s}));
}
for(int d = 0; d < 4; d++){
ll new_dist = dist+B;
if(kicking[cur_pos.f][cur_pos.s][d] > new_dist){
kicking[cur_pos.f][cur_pos.s][d] = new_dist;
pq.push(mp(new_dist, vi{1, cur_pos.f, cur_pos.s, d}));
}
}
}
else{
int cur_dir = state[3];
if(kicking[cur_pos.f][cur_pos.s][cur_dir] < dist) continue;
pi new_pos = mp(cur_pos.f+dx[cur_dir], cur_pos.s+dy[cur_dir]);
if(isGood(new_pos)){
ll new_dist = dist+A;
if(kicking[new_pos.f][new_pos.s][cur_dir] > new_dist){
kicking[new_pos.f][new_pos.s][cur_dir] = new_dist;
pq.push(mp(new_dist, vi{1, new_pos.f, new_pos.s, cur_dir}));
}
}
ll new_dist = dist+C*closest_dist[cur_pos.f][cur_pos.s];
if(walking[cur_pos.f][cur_pos.s] > new_dist){
walking[cur_pos.f][cur_pos.s] = new_dist;
pq.push(mp(new_dist, vi{0, cur_pos.f, cur_pos.s}));
}
}
}
cout << walking[ST[N].f][ST[N].s] << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
145 ms |
8200 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
657 ms |
23504 KB |
Output is correct |
4 |
Correct |
646 ms |
23464 KB |
Output is correct |
5 |
Correct |
169 ms |
6880 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
819 ms |
25244 KB |
Output is correct |
2 |
Correct |
805 ms |
25756 KB |
Output is correct |
3 |
Correct |
567 ms |
21288 KB |
Output is correct |
4 |
Correct |
587 ms |
23480 KB |
Output is correct |
5 |
Correct |
600 ms |
23468 KB |
Output is correct |
6 |
Correct |
536 ms |
23464 KB |
Output is correct |
7 |
Correct |
794 ms |
26764 KB |
Output is correct |
8 |
Correct |
664 ms |
26652 KB |
Output is correct |
9 |
Correct |
843 ms |
27576 KB |
Output is correct |
10 |
Correct |
118 ms |
10288 KB |
Output is correct |
11 |
Correct |
769 ms |
26736 KB |
Output is correct |
12 |
Correct |
839 ms |
26268 KB |
Output is correct |
13 |
Correct |
415 ms |
21316 KB |
Output is correct |
14 |
Correct |
835 ms |
26792 KB |
Output is correct |
15 |
Correct |
648 ms |
23696 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
145 ms |
8200 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
657 ms |
23504 KB |
Output is correct |
4 |
Correct |
646 ms |
23464 KB |
Output is correct |
5 |
Correct |
169 ms |
6880 KB |
Output is correct |
6 |
Correct |
819 ms |
25244 KB |
Output is correct |
7 |
Correct |
805 ms |
25756 KB |
Output is correct |
8 |
Correct |
567 ms |
21288 KB |
Output is correct |
9 |
Correct |
587 ms |
23480 KB |
Output is correct |
10 |
Correct |
600 ms |
23468 KB |
Output is correct |
11 |
Correct |
536 ms |
23464 KB |
Output is correct |
12 |
Correct |
794 ms |
26764 KB |
Output is correct |
13 |
Correct |
664 ms |
26652 KB |
Output is correct |
14 |
Correct |
843 ms |
27576 KB |
Output is correct |
15 |
Correct |
118 ms |
10288 KB |
Output is correct |
16 |
Correct |
769 ms |
26736 KB |
Output is correct |
17 |
Correct |
839 ms |
26268 KB |
Output is correct |
18 |
Correct |
415 ms |
21316 KB |
Output is correct |
19 |
Correct |
835 ms |
26792 KB |
Output is correct |
20 |
Correct |
648 ms |
23696 KB |
Output is correct |
21 |
Correct |
158 ms |
7028 KB |
Output is correct |
22 |
Correct |
903 ms |
23484 KB |
Output is correct |
23 |
Correct |
836 ms |
18104 KB |
Output is correct |
24 |
Correct |
953 ms |
18704 KB |
Output is correct |
25 |
Correct |
726 ms |
23536 KB |
Output is correct |
26 |
Correct |
823 ms |
23584 KB |
Output is correct |
27 |
Correct |
626 ms |
13264 KB |
Output is correct |
28 |
Correct |
520 ms |
13988 KB |
Output is correct |
29 |
Correct |
734 ms |
18840 KB |
Output is correct |
30 |
Correct |
485 ms |
13628 KB |
Output is correct |
31 |
Correct |
758 ms |
26908 KB |
Output is correct |
32 |
Correct |
937 ms |
26892 KB |
Output is correct |
33 |
Correct |
588 ms |
23460 KB |
Output is correct |
34 |
Correct |
1006 ms |
24348 KB |
Output is correct |
35 |
Correct |
454 ms |
13976 KB |
Output is correct |