This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <iostream>
#include <vector>
#include <queue>
#define pii pair<int, int>
#define piii pair<int, pii>
#define ff first
#define ss second
#define ee ss.ff
#define rr ss.ss
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx,avx2,fma")
using namespace std;
const int INF = (int)1e9 + 7;
vector<int> gph[202][8];
vector<int> rgph[202][8];
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
// freopen("in.txt", "r", stdin);
int n, m, s, e; cin >> n >> m >> s >> e; --s; --e;
for(int i = 0; i < m; ++i)
{
int x, y; char c; cin >> x >> y >> c; --x; --y;
int z;
if(c == '<') z = 0;
if(c == '>') z = 1;
if(c == '{') z = 2;
if(c == '}') z = 3;
if(c == '[') z = 4;
if(c == ']') z = 5;
if(c == '(') z = 6;
if(c == ')') z = 7;
gph[x][z].push_back(y);
rgph[y][z].push_back(x);
}
int C[n][n];
for(int i = 0; i < n; ++i) for(int j = 0; j < n; ++j) C[i][j] = INF;
priority_queue<piii, vector<piii>, greater<piii>> Q;
for(int i = 0; i < n; ++i) Q.push({0, {i, i}});
while(Q.size())
{
auto x = Q.top(); Q.pop();
if(C[x.ee][x.rr] != INF) continue;
C[x.ee][x.rr] = x.ff;
for(int i = 0; i < 8; i += 2)
for(auto j : rgph[x.ee][i])
for(auto k : gph[x.rr][i + 1])
Q.push({x.ff + 2, {j, k}});
for(int i = 0; i < n; ++i)
{
if(C[i][x.rr] > C[x.ee][x.rr] + C[i][x.ee])
Q.push({C[x.ee][x.rr] + C[i][x.ee], {i, x.rr}});
if(C[x.ee][i] > C[x.ee][x.rr] + C[x.rr][i])
Q.push({C[x.ee][x.rr] + C[x.rr][i], {x.ee, i}});
}
}
cout << (C[s][e] == INF ? -1 : C[s][e]);
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:31:13: warning: 'z' may be used uninitialized in this function [-Wmaybe-uninitialized]
31 | int z;
| ^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |