-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path929.cpp
More file actions
executable file
·73 lines (62 loc) · 1.63 KB
/
929.cpp
File metadata and controls
executable file
·73 lines (62 loc) · 1.63 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <bits/stdc++.h>
using namespace std;
int INF=INT_MAX;
int main()
{
int T;
cin>>T;
while(T--)
{
// cout<<INF<<endl;
int n,m;
cin>>n>>m;
int boba;
int arr[n+10][m+10];
for(int i=0; i<n; i++)
{
for(int j=0; j<m; j++)
{
cin>>boba;
arr[i][j]=boba;
}
}
using PII=pair<int,int>;
vector<vector<PII>> adj;
adj.resize(n*m+1,vector<PII>(0));
for(int i=0; i<n; i++)
for(int j=0; j<m; j++)
{
for(int aa=-1; aa<=1; aa++)
for(int bb=-1; bb<=1; bb++)
if(abs(aa)!=abs(bb))
{
if(i+aa<0||i+aa>n-1||j+bb<0||j+bb>m-1)continue;
// cout<<i<<" "<<j<<" "<<aa<<" "<<bb<<endl;
adj[i*m+j+1].push_back({(i+aa)*m+j+bb+1,arr[i+aa][j+bb]});
}
}
int const N=n*m+9;
vector<int> d(N,INF);
d[1]=arr[0][0];
priority_queue<PII,vector<PII>,greater<PII>> q;
q.push(make_pair(arr[0][0],1));
while(!q.empty())
{
int v=q.top().second;
int d_v=q.top().first;
q.pop();
if(d[v]!=d_v)continue;
for(auto u:adj[v])
{
int to=u.first;
int len=u.second;
if(d[v]+len<d[to])
{
d[to]=d[v]+len;
q.push({d[to],to});
}
}
}
cout<<d[n*m]<<endl;
}
}