-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path929(179).cpp
More file actions
75 lines (65 loc) · 1.45 KB
/
929(179).cpp
File metadata and controls
75 lines (65 loc) · 1.45 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
74
75
#include<bits/stdc++.h>
#define Fin freopen("input.txt","r",stdin)
#define Fout freopen("output.txt","w",stdout)
#define FasterIO ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define Faster cin.tie(0);cout.tie(0);
#define infinity INT_MAX
#define MAX 1001
using namespace std;
int G[MAX][MAX];
int dist[MAX][MAX];
bool visited[MAX][MAX];
int r,c;
int fx[] = {-1,0,+1,0};
int fy[] = {0,-1,0,+1};
int sx = 0,sy = 0;
int tt;
int i,j;
using pii = pair<int,pair<int,int> >;
void dijkstra(){
priority_queue< pii, vector<pii>, greater<pii> >pq;
dist[sx][sy] = G[sx][sy];
pq.push({dist[sx][sy],{sx,sy}});
while(!pq.empty()){
pii p = pq.top();
pq.pop();
int cx = p.second.first;
int cy = p.second.second;
//this part made it rank 179
if( (cx == r-1 && cy == c-1) || (r ==1 && c == 1) ){
return ;
}
visited[cx][cy] = true;
int tcx,tcy;
for(i = 0;i<4;i++){
tcx = cx + fx[i];
tcy = cy + fy[i];
if(tcx >= 0 && tcy >= 0 && tcx < r && tcy <c && !visited[tcx][tcy]){
if(dist[tcx][tcy] > G[tcx][tcy]+dist[cx][cy]){
dist[tcx][tcy] = G[tcx][tcy]+dist[cx][cy];
pq.push({dist[tcx][tcy],{tcx,tcy}});
}
}
}
}
}
int main(){
FasterIO;
cin>>tt;
while(tt--){
cin>>r;
cin>>c;
short x;
for(i =0;i<r;i++){
for(j = 0;j<c;j++){
cin>>x;
G[i][j] = x;
visited[i][j] = false;
dist[i][j] = infinity;
}
}
dijkstra();
cout<< dist[r-1][c-1] <<endl;
}
return 0;
}