/*
"TLE is like the wind, always by my side"
- Yasuo - 2022 -
*/
#include <bits/stdc++.h>
#define debug(x) cerr << #x << " " << x << "\n"
#define debugs(x) cerr << #x << " " << x << " "
#pragma GCC optimize("Ofast")
using namespace std;
struct edge
{
int endnode;
int cost;
};
struct pqnode
{
int node;
int cost;
};
struct cmp
{
bool operator()(pqnode a, pqnode b)
{
return a.cost>b.cost;
}
};
int n,m;
char a[501][501];
int mincost[250001];
vector<edge> nodes[250001];
bool visited[250001];
priority_queue<pqnode,vector<pqnode>,cmp> pq;
const int inf = 1e9;
int compress(int l, int c)
{
return m*(l-1)+c;
}
void dijkstra(int source)
{
int i,c,currnode;
for (i=1;i<=n*m;i++)
{
mincost[i]=inf;
}
pq.push({source,0});
while (!pq.empty())
{
currnode=pq.top().node;
c=pq.top().cost;
pq.pop();
if (!visited[currnode])
{
visited[currnode]=1;
mincost[currnode]=c;
for (i=0;i<nodes[currnode].size();i++)
{
if (!visited[nodes[currnode][i].endnode] && c+nodes[currnode][i].cost<mincost[nodes[currnode][i].endnode])
pq.push({nodes[currnode][i].endnode,c+nodes[currnode][i].cost});
}
}
}
}
void addedge(int node, int l, int c, char dir)
{
if (dir=='N')
{
if (l-1>0 && l-1<n+1 && c>0 && c<m+1)
nodes[node].push_back({compress(l-1,c),0});
if (l+1>0 && l+1<n+1 && c>0 && c<m+1)
nodes[node].push_back({compress(l+1,c),2});
if (l>0 && l<n+1 && c-1>0 && c-1<m+1)
nodes[node].push_back({compress(l,c-1),3});
if (l>0 && l<n+1 && c+1>0 && c+1<m+1)
nodes[node].push_back({compress(l,c+1),1});
}
if (dir=='W')
{
if (l-1>0 && l-1<n+1 && c>0 && c<m+1)
nodes[node].push_back({compress(l-1,c),1});
if (l+1>0 && l+1<n+1 && c>0 && c<m+1)
nodes[node].push_back({compress(l+1,c),3});
if (l>0 && l<n+1 && c-1>0 && c-1<m+1)
nodes[node].push_back({compress(l,c-1),0});
if (l>0 && l<n+1 && c+1>0 && c+1<m+1)
nodes[node].push_back({compress(l,c+1),2});
}
if (dir=='S')
{
if (l-1>0 && l-1<n+1 && c>0 && c<m+1)
nodes[node].push_back({compress(l-1,c),2});
if (l+1>0 && l+1<n+1 && c>0 && c<m+1)
nodes[node].push_back({compress(l+1,c),0});
if (l>0 && l<n+1 && c-1>0 && c-1<m+1)
nodes[node].push_back({compress(l,c-1),1});
if (l>0 && l<n+1 && c+1>0 && c+1<m+1)
nodes[node].push_back({compress(l,c+1),3});
}
if (dir=='E')
{
if (l-1>0 && l-1<n+1 && c>0 && c<m+1)
nodes[node].push_back({compress(l-1,c),3});
if (l+1>0 && l+1<n+1 && c>0 && c<m+1)
nodes[node].push_back({compress(l+1,c),1});
if (l>0 && l<n+1 && c-1>0 && c-1<m+1)
nodes[node].push_back({compress(l,c-1),2});
if (l>0 && l<n+1 && c+1>0 && c+1<m+1)
nodes[node].push_back({compress(l,c+1),0});
}
}
int main()
{
ifstream fin("secvp.in");
ofstream fout("secvp.out");
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int i,j;
cin >> n >> m;
for (i=1;i<=n;i++)
{
for (j=1;j<=m;j++)
{
cin >> a[i][j];
}
}
for (i=1;i<=n;i++)
{
for (j=1;j<=m;j++)
{
if (a[i][j]!='X')
addedge(compress(i,j),i,j,a[i][j]);
}
}
dijkstra(1);
if (mincost[n*m]!=inf)
cout << mincost[n*m];
else
cout << -1;
}
Compilation message
adventure.cpp: In function 'void dijkstra(int)':
adventure.cpp:62:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<edge>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
62 | for (i=0;i<nodes[currnode].size();i++)
| ~^~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
6228 KB |
Output is correct |
2 |
Correct |
3 ms |
6100 KB |
Output is correct |
3 |
Correct |
3 ms |
6228 KB |
Output is correct |
4 |
Correct |
3 ms |
6228 KB |
Output is correct |
5 |
Correct |
3 ms |
6216 KB |
Output is correct |
6 |
Correct |
3 ms |
6100 KB |
Output is correct |
7 |
Correct |
3 ms |
6100 KB |
Output is correct |
8 |
Correct |
4 ms |
6188 KB |
Output is correct |
9 |
Correct |
3 ms |
6216 KB |
Output is correct |
10 |
Correct |
3 ms |
6168 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
6228 KB |
Output is correct |
2 |
Correct |
3 ms |
6100 KB |
Output is correct |
3 |
Correct |
3 ms |
6228 KB |
Output is correct |
4 |
Correct |
3 ms |
6228 KB |
Output is correct |
5 |
Correct |
3 ms |
6216 KB |
Output is correct |
6 |
Correct |
3 ms |
6100 KB |
Output is correct |
7 |
Correct |
3 ms |
6100 KB |
Output is correct |
8 |
Correct |
4 ms |
6188 KB |
Output is correct |
9 |
Correct |
3 ms |
6216 KB |
Output is correct |
10 |
Correct |
3 ms |
6168 KB |
Output is correct |
11 |
Correct |
3 ms |
6100 KB |
Output is correct |
12 |
Correct |
4 ms |
6228 KB |
Output is correct |
13 |
Correct |
3 ms |
6220 KB |
Output is correct |
14 |
Correct |
4 ms |
6216 KB |
Output is correct |
15 |
Correct |
3 ms |
6212 KB |
Output is correct |
16 |
Correct |
3 ms |
6228 KB |
Output is correct |
17 |
Correct |
3 ms |
6216 KB |
Output is correct |
18 |
Correct |
3 ms |
6100 KB |
Output is correct |
19 |
Correct |
3 ms |
6100 KB |
Output is correct |
20 |
Correct |
3 ms |
6228 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
6100 KB |
Output is correct |
2 |
Correct |
3 ms |
6100 KB |
Output is correct |
3 |
Correct |
3 ms |
6100 KB |
Output is correct |
4 |
Correct |
3 ms |
6204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
6228 KB |
Output is correct |
2 |
Correct |
3 ms |
6220 KB |
Output is correct |
3 |
Correct |
3 ms |
6228 KB |
Output is correct |
4 |
Correct |
3 ms |
6212 KB |
Output is correct |
5 |
Correct |
4 ms |
6228 KB |
Output is correct |
6 |
Correct |
3 ms |
6228 KB |
Output is correct |
7 |
Correct |
4 ms |
6228 KB |
Output is correct |
8 |
Correct |
3 ms |
6216 KB |
Output is correct |
9 |
Correct |
3 ms |
6228 KB |
Output is correct |
10 |
Correct |
3 ms |
6228 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
6228 KB |
Output is correct |
2 |
Correct |
3 ms |
6100 KB |
Output is correct |
3 |
Correct |
3 ms |
6228 KB |
Output is correct |
4 |
Correct |
3 ms |
6228 KB |
Output is correct |
5 |
Correct |
3 ms |
6216 KB |
Output is correct |
6 |
Correct |
3 ms |
6100 KB |
Output is correct |
7 |
Correct |
3 ms |
6100 KB |
Output is correct |
8 |
Correct |
4 ms |
6188 KB |
Output is correct |
9 |
Correct |
3 ms |
6216 KB |
Output is correct |
10 |
Correct |
3 ms |
6168 KB |
Output is correct |
11 |
Correct |
3 ms |
6100 KB |
Output is correct |
12 |
Correct |
4 ms |
6228 KB |
Output is correct |
13 |
Correct |
3 ms |
6220 KB |
Output is correct |
14 |
Correct |
4 ms |
6216 KB |
Output is correct |
15 |
Correct |
3 ms |
6212 KB |
Output is correct |
16 |
Correct |
3 ms |
6228 KB |
Output is correct |
17 |
Correct |
3 ms |
6216 KB |
Output is correct |
18 |
Correct |
3 ms |
6100 KB |
Output is correct |
19 |
Correct |
3 ms |
6100 KB |
Output is correct |
20 |
Correct |
3 ms |
6228 KB |
Output is correct |
21 |
Correct |
9 ms |
6100 KB |
Output is correct |
22 |
Correct |
3 ms |
6100 KB |
Output is correct |
23 |
Correct |
3 ms |
6100 KB |
Output is correct |
24 |
Correct |
3 ms |
6204 KB |
Output is correct |
25 |
Correct |
3 ms |
6228 KB |
Output is correct |
26 |
Correct |
3 ms |
6220 KB |
Output is correct |
27 |
Correct |
3 ms |
6228 KB |
Output is correct |
28 |
Correct |
3 ms |
6212 KB |
Output is correct |
29 |
Correct |
4 ms |
6228 KB |
Output is correct |
30 |
Correct |
3 ms |
6228 KB |
Output is correct |
31 |
Correct |
4 ms |
6228 KB |
Output is correct |
32 |
Correct |
3 ms |
6216 KB |
Output is correct |
33 |
Correct |
3 ms |
6228 KB |
Output is correct |
34 |
Correct |
3 ms |
6228 KB |
Output is correct |
35 |
Correct |
8 ms |
7124 KB |
Output is correct |
36 |
Correct |
3 ms |
6228 KB |
Output is correct |
37 |
Correct |
10 ms |
7380 KB |
Output is correct |
38 |
Correct |
8 ms |
8036 KB |
Output is correct |
39 |
Correct |
70 ms |
17288 KB |
Output is correct |
40 |
Correct |
70 ms |
17288 KB |
Output is correct |
41 |
Correct |
29 ms |
17016 KB |
Output is correct |
42 |
Correct |
70 ms |
17312 KB |
Output is correct |
43 |
Correct |
86 ms |
21568 KB |
Output is correct |
44 |
Correct |
30 ms |
17056 KB |
Output is correct |