#include <bits/stdc++.h>
#define a first
#define b second
#define pb push_back
using namespace std;
#define endl '\n'
typedef long long llo;
llo vis[501][501];
llo dist[501][501];
llo dp[501][501][6];
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
llo n,m;
cin>>n>>m;
llo aa,bb,cc;
cin>>aa>>bb>>cc;
llo nn;
cin>>nn;
vector<pair<llo,llo>> tt;
for(llo i=0;i<nn;i++){
llo ee,ff;
cin>>ee>>ff;
if(i<nn-1){
vis[ee][ff]=1;
}
tt.pb({ee,ff});
}
queue<pair<llo,llo>> ss;
for(llo i=0;i<=n;i++){
for(llo j=0;j<=m;j++){
if(vis[i][j]==1){
dist[i][j]=0;
ss.push({i,j});
}
else{
dist[i][j]=-1;
}
}
}
vector<llo> x={-1,1,0,0};
vector<llo> y={0,0,1,-1};
while(ss.size()){
pair<llo,llo> no=ss.front();
ss.pop();
for(llo j=0;j<4;j++){
pair<llo,llo> rr={no.a+x[j],no.b+y[j]};
if(rr.a<0 or rr.a>n or rr.b<0 or rr.b>m){
continue;
}
if(dist[rr.a][rr.b]==-1){
dist[rr.a][rr.b]=dist[no.a][no.b]+1;
ss.push(rr);
}
}
}
for(llo i=0;i<=n;i++){
for(llo j=0;j<=m;j++){
//cout<<dist[i][j]<<",";
for(llo kk=0;kk<6;kk++){
dp[i][j][kk]=1e18;
}
}
//cout<<endl;
}
dp[tt[0].a][tt[0].b][0]=0;
priority_queue<pair<pair<llo,llo>,pair<llo,llo>>> qq;
qq.push({{0,0},tt[0]});
while(qq.size()){
pair<pair<llo,llo>,pair<llo,llo>> no=qq.top();
qq.pop();
no.a.a=-no.a.a;
if(dp[no.b.a][no.b.b][no.a.b]!=no.a.a){
continue;
}
if(no.a.b==0){
if(dp[no.b.a][no.b.b][1-no.a.b]>dp[no.b.a][no.b.b][no.a.b]+dist[no.b.a][no.b.b]*cc){
dp[no.b.a][no.b.b][1-no.a.b]=dp[no.b.a][no.b.b][no.a.b]+dist[no.b.a][no.b.b]*cc;
qq.push({{-dp[no.b.a][no.b.b][1],1},no.b});
}
}
else if(no.a.b==1){
for(llo j=0;j<4;j++){
pair<llo,llo> rr={no.b.a+x[j],no.b.b+y[j]};
if(rr.a<0 or rr.a>n or rr.b<0 or rr.b>m){
continue;
}
if(dp[rr.a][rr.b][1]>dp[no.b.a][no.b.b][no.a.b]+cc){
dp[rr.a][rr.b][1]=dp[no.b.a][no.b.b][no.a.b]+cc;
qq.push({{-dp[rr.a][rr.b][1],1},rr});
}
}
for(llo j=0;j<4;j++){
pair<llo,llo> rr={no.b.a+x[j],no.b.b+y[j]};
if(rr.a<0 or rr.a>n or rr.b<0 or rr.b>m){
continue;
}
if(dp[rr.a][rr.b][j+2]>dp[no.b.a][no.b.b][no.a.b]+aa+bb){
dp[rr.a][rr.b][j+2]=dp[no.b.a][no.b.b][no.a.b]+aa+bb;
qq.push({{-dp[rr.a][rr.b][j+2],j+2},rr});
}
}
}
else{
for(llo j=no.a.b-2;j<no.a.b-1;j++){
pair<llo,llo> rr={no.b.a+x[j],no.b.b+y[j]};
if(rr.a<0 or rr.a>n or rr.b<0 or rr.b>m){
continue;
}
if(dp[rr.a][rr.b][no.a.b]>dp[no.b.a][no.b.b][no.a.b]+aa){
dp[rr.a][rr.b][no.a.b]=dp[no.b.a][no.b.b][no.a.b]+aa;
qq.push({{-dp[rr.a][rr.b][no.a.b],no.a.b},rr});
}
}
if(dp[no.b.a][no.b.b][0]>dp[no.b.a][no.b.b][no.a.b]){
dp[no.b.a][no.b.b][0]=dp[no.b.a][no.b.b][no.a.b];
qq.push({{-dp[no.b.a][no.b.b][0],0},{no.b}});
}
}
}
llo ans=1e18;
/* cout<<endl;
for(int i=0;i<=n;i++){
for(int j=0;j<=m;j++){
cout<<dp[i][j][0]<<",";
}
cout<<endl;
}*/
for(llo i=0;i<6;i++){
ans=min(ans,dp[tt.back().a][tt.back().b][i]);
}
cout<<ans<<endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
70 ms |
7688 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
275 ms |
22484 KB |
Output is correct |
4 |
Correct |
294 ms |
22404 KB |
Output is correct |
5 |
Correct |
74 ms |
7636 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
330 ms |
22636 KB |
Output is correct |
2 |
Correct |
352 ms |
22704 KB |
Output is correct |
3 |
Correct |
250 ms |
20084 KB |
Output is correct |
4 |
Correct |
248 ms |
22744 KB |
Output is correct |
5 |
Correct |
234 ms |
22720 KB |
Output is correct |
6 |
Correct |
272 ms |
24128 KB |
Output is correct |
7 |
Correct |
303 ms |
24244 KB |
Output is correct |
8 |
Correct |
296 ms |
24176 KB |
Output is correct |
9 |
Correct |
317 ms |
24304 KB |
Output is correct |
10 |
Correct |
48 ms |
10196 KB |
Output is correct |
11 |
Correct |
289 ms |
24196 KB |
Output is correct |
12 |
Correct |
306 ms |
22960 KB |
Output is correct |
13 |
Correct |
199 ms |
20996 KB |
Output is correct |
14 |
Correct |
301 ms |
24208 KB |
Output is correct |
15 |
Correct |
237 ms |
23888 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
70 ms |
7688 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
275 ms |
22484 KB |
Output is correct |
4 |
Correct |
294 ms |
22404 KB |
Output is correct |
5 |
Correct |
74 ms |
7636 KB |
Output is correct |
6 |
Correct |
330 ms |
22636 KB |
Output is correct |
7 |
Correct |
352 ms |
22704 KB |
Output is correct |
8 |
Correct |
250 ms |
20084 KB |
Output is correct |
9 |
Correct |
248 ms |
22744 KB |
Output is correct |
10 |
Correct |
234 ms |
22720 KB |
Output is correct |
11 |
Correct |
272 ms |
24128 KB |
Output is correct |
12 |
Correct |
303 ms |
24244 KB |
Output is correct |
13 |
Correct |
296 ms |
24176 KB |
Output is correct |
14 |
Correct |
317 ms |
24304 KB |
Output is correct |
15 |
Correct |
48 ms |
10196 KB |
Output is correct |
16 |
Correct |
289 ms |
24196 KB |
Output is correct |
17 |
Correct |
306 ms |
22960 KB |
Output is correct |
18 |
Correct |
199 ms |
20996 KB |
Output is correct |
19 |
Correct |
301 ms |
24208 KB |
Output is correct |
20 |
Correct |
237 ms |
23888 KB |
Output is correct |
21 |
Correct |
91 ms |
8288 KB |
Output is correct |
22 |
Correct |
406 ms |
22772 KB |
Output is correct |
23 |
Correct |
374 ms |
17744 KB |
Output is correct |
24 |
Correct |
425 ms |
20436 KB |
Output is correct |
25 |
Correct |
339 ms |
24404 KB |
Output is correct |
26 |
Correct |
405 ms |
24772 KB |
Output is correct |
27 |
Correct |
215 ms |
19656 KB |
Output is correct |
28 |
Correct |
246 ms |
20528 KB |
Output is correct |
29 |
Correct |
351 ms |
24632 KB |
Output is correct |
30 |
Correct |
230 ms |
19408 KB |
Output is correct |
31 |
Correct |
353 ms |
24368 KB |
Output is correct |
32 |
Correct |
444 ms |
28908 KB |
Output is correct |
33 |
Correct |
266 ms |
24032 KB |
Output is correct |
34 |
Correct |
479 ms |
24520 KB |
Output is correct |
35 |
Correct |
215 ms |
18884 KB |
Output is correct |