0%

[題解]勇者冒險

a137. 勇者冒險

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <bits/stdc++.h>
#define ll long long
#define int long long
#define double long double
#define N 1005
#define Orz ios::sync_with_stdio(0),cin.tie(0)
#define INF 2e18
#define rep(i,l,r) for(int i=l;i<=r;i++)
#define rrep(i,l,r) for(int i=l;i<r;i++)
#define pii pair<int,int>
#define x first
#define y second
using namespace std;
int n,row,col,mp[N][N],dis[N][N];
int dx[4] = {0,-1,0,1},dy[4] = {1,0,-1,0};
bool visit[N][N];
typedef pair<int ,pair<int,int>> pp;

signed main(){
Orz;
cin>>row>>col;
memset(dis,0,sizeof(dis));
memset(visit,0,sizeof(visit));
memset(mp,0,sizeof(mp));
int s1,s2,e1,e2;cin>>s1>>s2>>e1>>e2;
cin>>n;
while(n--){
int a,b,c;cin>>a>>b>>c;
mp[a][b] = c;
}

//Dijkstra
priority_queue<pp,vector<pp>,greater<pp>> pq;
dis[s1][s2] = 0;
pq.push({0,{s1,s2}});

while(!pq.empty()){
int rr = pq.top().y.x,cc = pq.top().y.y;
// cout<<rr<<" "<<cc<<" "<<dis[rr][cc]<<endl;
pq.pop();
for(int i=0;i<4;i++){
int nx = rr+dx[i],ny = cc+dy[i];
if(nx<0||ny<0||nx>=row||ny>=col)continue;
if(nx == e1 && ny == e2){
cout<<dis[rr][cc]<<endl;
return 0;
}
if(mp[nx][ny] == 0)continue;
if(visit[nx][ny])continue;
dis[nx][ny] = max(dis[rr][cc],mp[nx][ny]);
pq.push({dis[nx][ny],{nx,ny}});
visit[nx][ny] = 1;
}
}
return 0;
}