#include<bits/stdc++.h>
using namespace std;
#define int long long
#define F first
#define S second
#define pii pair<int,int>
#define pb push_back
#define vi vector<int>
#define vp vector<pii>
#define vvi vector<vi>
#define ykh mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count())
const int N=505*505;
const int INF=1e18;
int x[N], y[N], dis[505][505], dx[4]={0, 0, -1, 1}, dy[4]={-1, 1, 0, 0}, dist[505][505][5];
int n, m, a, b, c, t;
bool valid(int i,int j){
return (0<=i and i<=n and 0<=j and j<=m);
}
#define tiiii tuple<int,int,int,int>
vector<tiiii>adj[505][505][5];
signed main(){
//setio();
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
cin >> n >> m >> a >> b >> c >> t;
queue<pair<int,int>>q;
for(int i=0;i<=n;i++){
for(int j=0;j<=m;j++){
dis[i][j]=INF;
}
}
for(int i=0;i<t;i++){
cin>>x[i]>>y[i];
q.push({x[i],y[i]});
dis[x[i]][y[i]]=0;
}
while(q.size()){
auto [x,y]=q.front();
q.pop();
for(int j=0;j<4;j++){
int nx=x+dx[j],ny=y+dy[j];
if(!valid(nx,ny))continue;
if(dis[nx][ny]>dis[x][y]+1){
dis[nx][ny]=dis[x][y]+1;
q.push({nx,ny});
}
}
}
for(int i=0;i<=n;i++){
for(int j=0;j<=m;j++){
dist[i][j][0]=INF;
for(int k=1;k<=4;k++){
dist[i][j][k]=INF;
int nx=i+dx[k-1],ny=j+dy[k-1];
adj[i][j][0].pb({i,j,k,b});
adj[i][j][k].pb({i,j,0,dis[i][j]*c});
if(!valid(nx,ny))continue;
adj[i][j][k].pb({nx,ny,k,a});
adj[i][j][0].pb({nx,ny,0,c});
}
}
}
dist[x[0]][y[0]][0]=0;
priority_queue<tiiii,vector<tiiii>,greater<tiiii>>pq;
pq.push({0,x[0],y[0],0});
while(pq.size()){
auto [d,x,y,k]=pq.top();
//debug(d,x,y,k);
pq.pop();
if(d!=dist[x][y][k])
continue;
for(auto [nx,ny,nk,nc]:adj[x][y][k]){
if(dist[nx][ny][nk]>d+nc){
dist[nx][ny][nk]=d+nc;
pq.push({d+nc,nx,ny,nk});
}
}
}
cout<<dist[x[t-1]][y[t-1]][0]<<'\n';
}
/*
input:
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
143 ms |
80884 KB |
Output is correct |
2 |
Correct |
9 ms |
37724 KB |
Output is correct |
3 |
Correct |
562 ms |
200380 KB |
Output is correct |
4 |
Correct |
619 ms |
200848 KB |
Output is correct |
5 |
Correct |
125 ms |
96848 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
669 ms |
199640 KB |
Output is correct |
2 |
Correct |
625 ms |
200636 KB |
Output is correct |
3 |
Correct |
438 ms |
171956 KB |
Output is correct |
4 |
Correct |
437 ms |
201284 KB |
Output is correct |
5 |
Correct |
470 ms |
172220 KB |
Output is correct |
6 |
Correct |
479 ms |
200980 KB |
Output is correct |
7 |
Correct |
652 ms |
201152 KB |
Output is correct |
8 |
Correct |
555 ms |
200432 KB |
Output is correct |
9 |
Correct |
666 ms |
200892 KB |
Output is correct |
10 |
Correct |
112 ms |
71596 KB |
Output is correct |
11 |
Correct |
654 ms |
200380 KB |
Output is correct |
12 |
Correct |
678 ms |
199648 KB |
Output is correct |
13 |
Correct |
368 ms |
171748 KB |
Output is correct |
14 |
Correct |
649 ms |
200480 KB |
Output is correct |
15 |
Correct |
521 ms |
171468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
143 ms |
80884 KB |
Output is correct |
2 |
Correct |
9 ms |
37724 KB |
Output is correct |
3 |
Correct |
562 ms |
200380 KB |
Output is correct |
4 |
Correct |
619 ms |
200848 KB |
Output is correct |
5 |
Correct |
125 ms |
96848 KB |
Output is correct |
6 |
Correct |
669 ms |
199640 KB |
Output is correct |
7 |
Correct |
625 ms |
200636 KB |
Output is correct |
8 |
Correct |
438 ms |
171956 KB |
Output is correct |
9 |
Correct |
437 ms |
201284 KB |
Output is correct |
10 |
Correct |
470 ms |
172220 KB |
Output is correct |
11 |
Correct |
479 ms |
200980 KB |
Output is correct |
12 |
Correct |
652 ms |
201152 KB |
Output is correct |
13 |
Correct |
555 ms |
200432 KB |
Output is correct |
14 |
Correct |
666 ms |
200892 KB |
Output is correct |
15 |
Correct |
112 ms |
71596 KB |
Output is correct |
16 |
Correct |
654 ms |
200380 KB |
Output is correct |
17 |
Correct |
678 ms |
199648 KB |
Output is correct |
18 |
Correct |
368 ms |
171748 KB |
Output is correct |
19 |
Correct |
649 ms |
200480 KB |
Output is correct |
20 |
Correct |
521 ms |
171468 KB |
Output is correct |
21 |
Correct |
144 ms |
96720 KB |
Output is correct |
22 |
Correct |
723 ms |
200756 KB |
Output is correct |
23 |
Correct |
694 ms |
181492 KB |
Output is correct |
24 |
Correct |
805 ms |
195784 KB |
Output is correct |
25 |
Correct |
608 ms |
200484 KB |
Output is correct |
26 |
Correct |
609 ms |
200640 KB |
Output is correct |
27 |
Correct |
389 ms |
191952 KB |
Output is correct |
28 |
Correct |
439 ms |
192148 KB |
Output is correct |
29 |
Correct |
543 ms |
197320 KB |
Output is correct |
30 |
Correct |
427 ms |
192116 KB |
Output is correct |
31 |
Correct |
719 ms |
200100 KB |
Output is correct |
32 |
Correct |
807 ms |
200680 KB |
Output is correct |
33 |
Correct |
514 ms |
200812 KB |
Output is correct |
34 |
Correct |
796 ms |
200740 KB |
Output is correct |
35 |
Correct |
355 ms |
192332 KB |
Output is correct |