#include <bits/stdc++.h>
using namespace std;
#define int long long
set<pair<int,pair<int,int>>>pq;
vector<pair<int,char>>link[201];
vector<pair<int,char>>rev[201];
int dist[201][201];
int vis[201][201];
signed main()
{
memset(dist,62,sizeof(dist));
int N,M,s,t;
cin >> N>>M>>s>>t;
int i;
for(i=0;i<M;i++)
{
int a,b;
char d;
cin >>a >> b >> d;
link[a].push_back({b,d});
rev[b].push_back({a,d});
}
for(i=1;i<=N;i++)
pq.insert({0,{i,i}});
while(pq.size())
{
auto r=*pq.begin();
pq.erase(r);
if(vis[r.second.first][r.second.second])
continue;
dist[r.second.first][r.second.second]=r.first;
vis[r.second.first][r.second.second]=1;
for(i=1;i<=N;i++)
{
if(dist[r.second.first][r.second.second]+dist[r.second.second][i]<dist[r.second.first][i])
{
pq.erase({dist[r.second.first][i],{r.second.first,i}});
dist[r.second.first][i]=dist[r.second.first][r.second.second]+dist[r.second.second][i];
pq.insert({dist[r.second.first][i],{r.second.first,i}});
}
if(dist[r.second.first][r.second.second]+dist[i][r.second.first]<dist[i][r.second.second])
{
pq.erase({dist[i][r.second.second],{i,r.second.second}});
dist[i][r.second.second]=dist[r.second.first][r.second.second]+dist[i][r.second.first];
pq.insert({dist[i][r.second.second],{i,r.second.second}});
}
}
for(i=0;i<rev[r.second.first].size();i++)
{
int j;
for(j=0;j<link[r.second.second].size();j++)
{
auto r1=rev[r.second.first][i];
auto r2=link[r.second.second][j];
if(r1.second>=r2.second||r2.second-r1.second>2)
continue;
if(dist[r.second.first][r.second.second]+2<dist[r1.first][r2.first])
{
pq.erase({dist[r1.first][r2.first],{r1.first,r2.first}});
dist[r1.first][r2.first]=dist[r.second.first][r.second.second]+2;
pq.insert({dist[r1.first][r2.first],{r1.first,r2.first}});
}
}
}
}
if(!vis[s][t])
cout <<-1;
else
cout <<dist[s][t];
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:48:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, char> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
48 | for(i=0;i<rev[r.second.first].size();i++)
| ~^~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:51:22: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, char> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
51 | for(j=0;j<link[r.second.second].size();j++)
| ~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
588 KB |
Output is correct |
2 |
Correct |
1 ms |
588 KB |
Output is correct |
3 |
Correct |
1 ms |
588 KB |
Output is correct |
4 |
Correct |
1 ms |
588 KB |
Output is correct |
5 |
Correct |
1 ms |
588 KB |
Output is correct |
6 |
Correct |
1 ms |
588 KB |
Output is correct |
7 |
Correct |
1 ms |
588 KB |
Output is correct |
8 |
Correct |
1 ms |
588 KB |
Output is correct |
9 |
Correct |
1 ms |
588 KB |
Output is correct |
10 |
Correct |
1 ms |
588 KB |
Output is correct |
11 |
Correct |
1 ms |
588 KB |
Output is correct |
12 |
Correct |
1 ms |
588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
588 KB |
Output is correct |
2 |
Correct |
1 ms |
588 KB |
Output is correct |
3 |
Correct |
1 ms |
588 KB |
Output is correct |
4 |
Correct |
1 ms |
588 KB |
Output is correct |
5 |
Correct |
1 ms |
588 KB |
Output is correct |
6 |
Correct |
1 ms |
588 KB |
Output is correct |
7 |
Correct |
1 ms |
588 KB |
Output is correct |
8 |
Correct |
1 ms |
588 KB |
Output is correct |
9 |
Correct |
1 ms |
588 KB |
Output is correct |
10 |
Correct |
1 ms |
588 KB |
Output is correct |
11 |
Correct |
1 ms |
588 KB |
Output is correct |
12 |
Correct |
1 ms |
588 KB |
Output is correct |
13 |
Correct |
1 ms |
588 KB |
Output is correct |
14 |
Correct |
1 ms |
588 KB |
Output is correct |
15 |
Correct |
1 ms |
588 KB |
Output is correct |
16 |
Correct |
1 ms |
588 KB |
Output is correct |
17 |
Correct |
1 ms |
588 KB |
Output is correct |
18 |
Correct |
1 ms |
588 KB |
Output is correct |
19 |
Correct |
1 ms |
588 KB |
Output is correct |
20 |
Correct |
1 ms |
588 KB |
Output is correct |
21 |
Correct |
1 ms |
588 KB |
Output is correct |
22 |
Correct |
1 ms |
588 KB |
Output is correct |
23 |
Correct |
1 ms |
588 KB |
Output is correct |
24 |
Correct |
1 ms |
588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
588 KB |
Output is correct |
2 |
Correct |
1 ms |
588 KB |
Output is correct |
3 |
Correct |
1 ms |
588 KB |
Output is correct |
4 |
Correct |
1 ms |
588 KB |
Output is correct |
5 |
Correct |
1 ms |
588 KB |
Output is correct |
6 |
Correct |
1 ms |
588 KB |
Output is correct |
7 |
Correct |
1 ms |
588 KB |
Output is correct |
8 |
Correct |
1 ms |
588 KB |
Output is correct |
9 |
Correct |
1 ms |
588 KB |
Output is correct |
10 |
Correct |
1 ms |
588 KB |
Output is correct |
11 |
Correct |
1 ms |
588 KB |
Output is correct |
12 |
Correct |
1 ms |
588 KB |
Output is correct |
13 |
Correct |
1 ms |
588 KB |
Output is correct |
14 |
Correct |
1 ms |
588 KB |
Output is correct |
15 |
Correct |
1 ms |
588 KB |
Output is correct |
16 |
Correct |
1 ms |
588 KB |
Output is correct |
17 |
Correct |
1 ms |
588 KB |
Output is correct |
18 |
Correct |
1 ms |
588 KB |
Output is correct |
19 |
Correct |
1 ms |
588 KB |
Output is correct |
20 |
Correct |
1 ms |
588 KB |
Output is correct |
21 |
Correct |
1 ms |
588 KB |
Output is correct |
22 |
Correct |
1 ms |
588 KB |
Output is correct |
23 |
Correct |
1 ms |
588 KB |
Output is correct |
24 |
Correct |
1 ms |
588 KB |
Output is correct |
25 |
Correct |
1 ms |
588 KB |
Output is correct |
26 |
Correct |
4 ms |
716 KB |
Output is correct |
27 |
Correct |
8 ms |
716 KB |
Output is correct |
28 |
Correct |
1 ms |
716 KB |
Output is correct |
29 |
Correct |
5 ms |
844 KB |
Output is correct |
30 |
Correct |
21 ms |
844 KB |
Output is correct |
31 |
Correct |
1 ms |
588 KB |
Output is correct |
32 |
Correct |
1 ms |
588 KB |
Output is correct |
33 |
Correct |
3 ms |
716 KB |
Output is correct |
34 |
Correct |
1 ms |
588 KB |
Output is correct |
35 |
Correct |
21 ms |
844 KB |
Output is correct |
36 |
Correct |
6 ms |
844 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
588 KB |
Output is correct |
2 |
Correct |
1 ms |
588 KB |
Output is correct |
3 |
Correct |
1 ms |
588 KB |
Output is correct |
4 |
Correct |
1 ms |
588 KB |
Output is correct |
5 |
Correct |
1 ms |
588 KB |
Output is correct |
6 |
Correct |
1 ms |
588 KB |
Output is correct |
7 |
Correct |
1 ms |
588 KB |
Output is correct |
8 |
Correct |
1 ms |
588 KB |
Output is correct |
9 |
Correct |
1 ms |
588 KB |
Output is correct |
10 |
Correct |
1 ms |
588 KB |
Output is correct |
11 |
Correct |
1 ms |
588 KB |
Output is correct |
12 |
Correct |
1 ms |
588 KB |
Output is correct |
13 |
Correct |
1 ms |
588 KB |
Output is correct |
14 |
Correct |
1 ms |
588 KB |
Output is correct |
15 |
Correct |
1 ms |
588 KB |
Output is correct |
16 |
Correct |
1 ms |
588 KB |
Output is correct |
17 |
Correct |
1 ms |
588 KB |
Output is correct |
18 |
Correct |
1 ms |
588 KB |
Output is correct |
19 |
Correct |
1 ms |
588 KB |
Output is correct |
20 |
Correct |
1 ms |
588 KB |
Output is correct |
21 |
Correct |
1 ms |
588 KB |
Output is correct |
22 |
Correct |
1 ms |
588 KB |
Output is correct |
23 |
Correct |
1 ms |
588 KB |
Output is correct |
24 |
Correct |
1 ms |
588 KB |
Output is correct |
25 |
Correct |
1 ms |
588 KB |
Output is correct |
26 |
Correct |
4 ms |
716 KB |
Output is correct |
27 |
Correct |
8 ms |
716 KB |
Output is correct |
28 |
Correct |
1 ms |
716 KB |
Output is correct |
29 |
Correct |
5 ms |
844 KB |
Output is correct |
30 |
Correct |
21 ms |
844 KB |
Output is correct |
31 |
Correct |
1 ms |
588 KB |
Output is correct |
32 |
Correct |
1 ms |
588 KB |
Output is correct |
33 |
Correct |
3 ms |
716 KB |
Output is correct |
34 |
Correct |
1 ms |
588 KB |
Output is correct |
35 |
Correct |
21 ms |
844 KB |
Output is correct |
36 |
Correct |
6 ms |
844 KB |
Output is correct |
37 |
Correct |
3 ms |
716 KB |
Output is correct |
38 |
Correct |
37 ms |
1356 KB |
Output is correct |
39 |
Correct |
3 ms |
716 KB |
Output is correct |
40 |
Correct |
1 ms |
716 KB |
Output is correct |
41 |
Correct |
35 ms |
1356 KB |
Output is correct |
42 |
Correct |
1 ms |
716 KB |
Output is correct |
43 |
Correct |
1 ms |
716 KB |
Output is correct |
44 |
Correct |
2 ms |
716 KB |
Output is correct |
45 |
Correct |
34 ms |
1356 KB |
Output is correct |
46 |
Correct |
35 ms |
1392 KB |
Output is correct |
47 |
Correct |
3 ms |
844 KB |
Output is correct |
48 |
Correct |
49 ms |
1356 KB |
Output is correct |
49 |
Correct |
1 ms |
716 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
46 ms |
2096 KB |
Output is correct |
2 |
Correct |
50 ms |
2148 KB |
Output is correct |
3 |
Correct |
44 ms |
2124 KB |
Output is correct |
4 |
Correct |
46 ms |
2124 KB |
Output is correct |
5 |
Correct |
42 ms |
2096 KB |
Output is correct |
6 |
Correct |
43 ms |
2096 KB |
Output is correct |
7 |
Correct |
40 ms |
2072 KB |
Output is correct |
8 |
Correct |
43 ms |
2124 KB |
Output is correct |
9 |
Correct |
44 ms |
2124 KB |
Output is correct |
10 |
Correct |
40 ms |
2072 KB |
Output is correct |
11 |
Correct |
42 ms |
1996 KB |
Output is correct |
12 |
Correct |
43 ms |
1996 KB |
Output is correct |
13 |
Correct |
39 ms |
2120 KB |
Output is correct |
14 |
Correct |
7 ms |
1140 KB |
Output is correct |
15 |
Correct |
10 ms |
1100 KB |
Output is correct |
16 |
Correct |
1 ms |
844 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
588 KB |
Output is correct |
2 |
Correct |
1 ms |
588 KB |
Output is correct |
3 |
Correct |
1 ms |
588 KB |
Output is correct |
4 |
Correct |
1 ms |
588 KB |
Output is correct |
5 |
Correct |
1 ms |
588 KB |
Output is correct |
6 |
Correct |
1 ms |
588 KB |
Output is correct |
7 |
Correct |
1 ms |
588 KB |
Output is correct |
8 |
Correct |
1 ms |
588 KB |
Output is correct |
9 |
Correct |
1 ms |
588 KB |
Output is correct |
10 |
Correct |
1 ms |
588 KB |
Output is correct |
11 |
Correct |
1 ms |
588 KB |
Output is correct |
12 |
Correct |
1 ms |
588 KB |
Output is correct |
13 |
Correct |
1 ms |
588 KB |
Output is correct |
14 |
Correct |
1 ms |
588 KB |
Output is correct |
15 |
Correct |
1 ms |
588 KB |
Output is correct |
16 |
Correct |
1 ms |
588 KB |
Output is correct |
17 |
Correct |
1 ms |
588 KB |
Output is correct |
18 |
Correct |
1 ms |
588 KB |
Output is correct |
19 |
Correct |
1 ms |
588 KB |
Output is correct |
20 |
Correct |
1 ms |
588 KB |
Output is correct |
21 |
Correct |
1 ms |
588 KB |
Output is correct |
22 |
Correct |
1 ms |
588 KB |
Output is correct |
23 |
Correct |
1 ms |
588 KB |
Output is correct |
24 |
Correct |
1 ms |
588 KB |
Output is correct |
25 |
Correct |
1 ms |
588 KB |
Output is correct |
26 |
Correct |
4 ms |
716 KB |
Output is correct |
27 |
Correct |
8 ms |
716 KB |
Output is correct |
28 |
Correct |
1 ms |
716 KB |
Output is correct |
29 |
Correct |
5 ms |
844 KB |
Output is correct |
30 |
Correct |
21 ms |
844 KB |
Output is correct |
31 |
Correct |
1 ms |
588 KB |
Output is correct |
32 |
Correct |
1 ms |
588 KB |
Output is correct |
33 |
Correct |
3 ms |
716 KB |
Output is correct |
34 |
Correct |
1 ms |
588 KB |
Output is correct |
35 |
Correct |
21 ms |
844 KB |
Output is correct |
36 |
Correct |
6 ms |
844 KB |
Output is correct |
37 |
Correct |
3 ms |
716 KB |
Output is correct |
38 |
Correct |
37 ms |
1356 KB |
Output is correct |
39 |
Correct |
3 ms |
716 KB |
Output is correct |
40 |
Correct |
1 ms |
716 KB |
Output is correct |
41 |
Correct |
35 ms |
1356 KB |
Output is correct |
42 |
Correct |
1 ms |
716 KB |
Output is correct |
43 |
Correct |
1 ms |
716 KB |
Output is correct |
44 |
Correct |
2 ms |
716 KB |
Output is correct |
45 |
Correct |
34 ms |
1356 KB |
Output is correct |
46 |
Correct |
35 ms |
1392 KB |
Output is correct |
47 |
Correct |
3 ms |
844 KB |
Output is correct |
48 |
Correct |
49 ms |
1356 KB |
Output is correct |
49 |
Correct |
1 ms |
716 KB |
Output is correct |
50 |
Correct |
46 ms |
2096 KB |
Output is correct |
51 |
Correct |
50 ms |
2148 KB |
Output is correct |
52 |
Correct |
44 ms |
2124 KB |
Output is correct |
53 |
Correct |
46 ms |
2124 KB |
Output is correct |
54 |
Correct |
42 ms |
2096 KB |
Output is correct |
55 |
Correct |
43 ms |
2096 KB |
Output is correct |
56 |
Correct |
40 ms |
2072 KB |
Output is correct |
57 |
Correct |
43 ms |
2124 KB |
Output is correct |
58 |
Correct |
44 ms |
2124 KB |
Output is correct |
59 |
Correct |
40 ms |
2072 KB |
Output is correct |
60 |
Correct |
42 ms |
1996 KB |
Output is correct |
61 |
Correct |
43 ms |
1996 KB |
Output is correct |
62 |
Correct |
39 ms |
2120 KB |
Output is correct |
63 |
Correct |
7 ms |
1140 KB |
Output is correct |
64 |
Correct |
10 ms |
1100 KB |
Output is correct |
65 |
Correct |
1 ms |
844 KB |
Output is correct |
66 |
Correct |
3 ms |
972 KB |
Output is correct |
67 |
Correct |
149 ms |
3272 KB |
Output is correct |
68 |
Correct |
145 ms |
3296 KB |
Output is correct |
69 |
Correct |
188 ms |
3280 KB |
Output is correct |
70 |
Correct |
5 ms |
1036 KB |
Output is correct |
71 |
Correct |
7 ms |
844 KB |
Output is correct |
72 |
Correct |
106 ms |
3304 KB |
Output is correct |
73 |
Correct |
101 ms |
3276 KB |
Output is correct |
74 |
Correct |
10 ms |
972 KB |
Output is correct |
75 |
Correct |
137 ms |
3304 KB |
Output is correct |
76 |
Correct |
9 ms |
1100 KB |
Output is correct |
77 |
Correct |
2 ms |
844 KB |
Output is correct |
78 |
Correct |
8 ms |
972 KB |
Output is correct |
79 |
Correct |
130 ms |
3128 KB |
Output is correct |
80 |
Correct |
160 ms |
3276 KB |
Output is correct |
81 |
Correct |
11 ms |
844 KB |
Output is correct |
82 |
Correct |
10 ms |
972 KB |
Output is correct |
83 |
Correct |
104 ms |
3384 KB |
Output is correct |