#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
typedef long long ll;
const int MX=510, inf=2e9;
const int dx[4]={0,1,0,-1};
const int dy[4]={1,0,-1,0};
int h, w;
ll a, b, c;
int dist[MX][MX];
int st, ed;
inline int f(int x, int y){
return x*(w+1)+y;
}
inline bool valid(int x, int y){
return 0<=x && x<=h && 0<=y && y<=w;
}
void init(){
for(int x=0; x<=h; x++)
for(int y=0; y<=w; y++)
dist[x][y]=inf;
queue<pii> Q;
int n, x, y;
cin>>n;
for(int i=1; i<=n; i++){
cin>>x>>y;
dist[x][y]=0;
Q.push({x,y});
if(i==1) st=f(x,y);
if(i==n) ed=f(x,y);
}
while(!Q.empty()){
int x,y; tie(x,y)=Q.front(); Q.pop();
for(int k=0; k<4; k++){
int z=x+dx[k], w=y+dy[k];
if(!valid(z,w) || dist[z][w]<inf) continue;
dist[z][w]=dist[x][y]+1;
Q.push({z,w});
}
}
}
struct edge {
int to; ll cost;
bool operator < (const edge &e) const {
return cost>e.cost;
}
};
ll ans[MX*MX*3];
vector<edge> G[MX*MX*3]; // ground, _, |
ll solve(){
int S=(h+1)*(w+1), S2=S+S;
fill(ans, ans+3*S+1, inf*1e7);
for(int x=0; x<=h; x++){
for(int y=0; y<=w; y++){
int v=f(x,y);
G[v].push_back({v+S, b});
G[v].push_back({v+S2, b});
G[v+S].push_back({v, dist[x][y]*c});
G[v+S2].push_back({v, dist[x][y]*c});
for(int k=0; k<4; k++){
int w=x+dx[k], z=y+dy[k], u=f(w,z);
if(!valid(w,z)) continue;
G[v].push_back({u, c});
if(k%2==0) G[v+S2].push_back({u+S2, a});
else G[v+S].push_back({u+S, a});
}
}
}
priority_queue<edge> Q;
Q.push({st, 0}); ans[st]=0;
while(!Q.empty()){
int v=Q.top().to; ll d=Q.top().cost; Q.pop();
if(ans[v]!=d) continue;
for(edge &e: G[v]){
int x=e.to; ll c=e.cost;
if(ans[x]<=c+d) continue;
ans[x]=c+d;
Q.push({x,ans[x]});
}
}
return ans[ed];
}
int main(){
ios::sync_with_stdio(0); cin.tie(0);
cin>>h>>w>>a>>b>>c;
init();
cout<<solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
160 ms |
39924 KB |
Output is correct |
2 |
Correct |
21 ms |
39924 KB |
Output is correct |
3 |
Correct |
609 ms |
104624 KB |
Output is correct |
4 |
Correct |
732 ms |
104716 KB |
Output is correct |
5 |
Correct |
173 ms |
104716 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
628 ms |
104752 KB |
Output is correct |
2 |
Correct |
610 ms |
104948 KB |
Output is correct |
3 |
Correct |
456 ms |
104948 KB |
Output is correct |
4 |
Correct |
489 ms |
104948 KB |
Output is correct |
5 |
Correct |
494 ms |
104948 KB |
Output is correct |
6 |
Correct |
534 ms |
104948 KB |
Output is correct |
7 |
Correct |
647 ms |
104948 KB |
Output is correct |
8 |
Correct |
605 ms |
104948 KB |
Output is correct |
9 |
Correct |
642 ms |
104948 KB |
Output is correct |
10 |
Correct |
96 ms |
104948 KB |
Output is correct |
11 |
Correct |
683 ms |
104948 KB |
Output is correct |
12 |
Correct |
658 ms |
104948 KB |
Output is correct |
13 |
Correct |
396 ms |
104948 KB |
Output is correct |
14 |
Correct |
704 ms |
104980 KB |
Output is correct |
15 |
Correct |
596 ms |
104980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
160 ms |
39924 KB |
Output is correct |
2 |
Correct |
21 ms |
39924 KB |
Output is correct |
3 |
Correct |
609 ms |
104624 KB |
Output is correct |
4 |
Correct |
732 ms |
104716 KB |
Output is correct |
5 |
Correct |
173 ms |
104716 KB |
Output is correct |
6 |
Correct |
628 ms |
104752 KB |
Output is correct |
7 |
Correct |
610 ms |
104948 KB |
Output is correct |
8 |
Correct |
456 ms |
104948 KB |
Output is correct |
9 |
Correct |
489 ms |
104948 KB |
Output is correct |
10 |
Correct |
494 ms |
104948 KB |
Output is correct |
11 |
Correct |
534 ms |
104948 KB |
Output is correct |
12 |
Correct |
647 ms |
104948 KB |
Output is correct |
13 |
Correct |
605 ms |
104948 KB |
Output is correct |
14 |
Correct |
642 ms |
104948 KB |
Output is correct |
15 |
Correct |
96 ms |
104948 KB |
Output is correct |
16 |
Correct |
683 ms |
104948 KB |
Output is correct |
17 |
Correct |
658 ms |
104948 KB |
Output is correct |
18 |
Correct |
396 ms |
104948 KB |
Output is correct |
19 |
Correct |
704 ms |
104980 KB |
Output is correct |
20 |
Correct |
596 ms |
104980 KB |
Output is correct |
21 |
Correct |
175 ms |
104980 KB |
Output is correct |
22 |
Correct |
709 ms |
105064 KB |
Output is correct |
23 |
Correct |
638 ms |
105064 KB |
Output is correct |
24 |
Correct |
716 ms |
105064 KB |
Output is correct |
25 |
Correct |
702 ms |
105064 KB |
Output is correct |
26 |
Correct |
698 ms |
105200 KB |
Output is correct |
27 |
Correct |
546 ms |
105200 KB |
Output is correct |
28 |
Correct |
531 ms |
105200 KB |
Output is correct |
29 |
Correct |
612 ms |
105200 KB |
Output is correct |
30 |
Correct |
559 ms |
105200 KB |
Output is correct |
31 |
Correct |
729 ms |
107808 KB |
Output is correct |
32 |
Correct |
754 ms |
108640 KB |
Output is correct |
33 |
Correct |
601 ms |
108692 KB |
Output is correct |
34 |
Correct |
733 ms |
108736 KB |
Output is correct |
35 |
Correct |
468 ms |
108736 KB |
Output is correct |