#include <bits/stdc++.h>
using namespace std;
int N, M;
vector<int> graph[205];
vector<int> rgraph[205];
int u[50005], v[50005], p[2][50005];
bool used[50005];
long long dist[2][205], cst[50005], rev[50005], rdist[2][205];
bool vis[50005];
long long ans = LLONG_MAX>>4;
queue<int> qu;
void dijkstra(int s, int t){
for(int i = 0; i<=N; i++){
dist[t][i] = ans;
vis[i] = 0;
}
dist[t][s] = 0;
for(int k = 0; k<N; k++){
int n = 0;
for(int i = 1; i<=N; i++){
if(!vis[i] && dist[t][i] < dist[t][n]){
n = i;
}
}
vis[n] = 1;
if(!n){
break;
}
for(int e : graph[n]){
if(u[e] == n && dist[t][v[e]] > dist[t][u[e]] + cst[e]){
dist[t][v[e]] = dist[t][u[e]] + cst[e];
p[t][v[e]] = e;
qu.push(v[e]);
}
}
}
}
void rdijkstra(int s, int t){
for(int i = 1; i<=N; i++){
rdist[t][i] = ans;
}
rdist[t][s] = 0;
qu.push(s);
while(qu.size()){
int n = qu.front();
qu.pop();
for(int e : rgraph[n]){
if(rdist[t][u[e]] > rdist[t][v[e]] + cst[e]){
rdist[t][u[e]] = rdist[t][v[e]] + cst[e];
//p[t][u[e]] = u[e];
qu.push(u[e]);
}
}
}
}
int main(){
cin.sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> N >> M;
for(int i = 1; i<=M; i++){
cin >> u[i] >> v[i] >> cst[i] >> rev[i];
graph[u[i]].push_back(i);
rgraph[v[i]].push_back(i);
}
dijkstra(1, 0);
dijkstra(N, 1);
rdijkstra(1, 0);
rdijkstra(N, 1);
ans = dist[0][N] + dist[1][1];
int crnt = N;
while(p[0][crnt] && crnt != 1){
used[p[0][crnt]] = 1;
crnt = u[p[0][crnt]];
}
crnt = 1;
while(p[1][crnt] && crnt != N){
used[p[1][crnt]] = 1;
crnt = u[p[1][crnt]];
}
for(int e = 1; e<=M; e++){
if(!used[e]){
ans = min(ans, dist[0][N] + dist[1][v[e]] + rdist[0][u[e]] + cst[e] + rev[e]);
ans = min(ans, dist[1][1] + dist[0][v[e]] + rdist[1][u[e]] + cst[e] + rev[e]);
ans = min(ans, dist[0][v[e]] + dist[1][v[e]] + rdist[0][u[e]] + rdist[1][u[e]] + rev[e] + 2*cst[e]);
}
}
for(int e = 1; e<=M; e++){
if(used[e]){
swap(u[e], v[e]);
graph[u[e]].push_back(e);
dijkstra(1, 0);
dijkstra(N, 1);
graph[u[e]].pop_back();
swap(u[e], v[e]);
ans = min(ans, dist[0][N] + dist[1][1] + rev[e]);
}
}
if(ans >= LLONG_MAX>>4){
ans = -1;
}
cout << ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
9 ms |
504 KB |
Output is correct |
4 |
Correct |
9 ms |
504 KB |
Output is correct |
5 |
Correct |
5 ms |
504 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
376 KB |
Output is correct |
9 |
Correct |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
62 ms |
1144 KB |
Output is correct |
11 |
Correct |
82 ms |
1400 KB |
Output is correct |
12 |
Correct |
79 ms |
1272 KB |
Output is correct |
13 |
Correct |
5 ms |
376 KB |
Output is correct |
14 |
Correct |
8 ms |
376 KB |
Output is correct |
15 |
Correct |
7 ms |
376 KB |
Output is correct |
16 |
Correct |
7 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
37 ms |
2296 KB |
Output is correct |
2 |
Correct |
34 ms |
2168 KB |
Output is correct |
3 |
Correct |
37 ms |
2168 KB |
Output is correct |
4 |
Correct |
7 ms |
508 KB |
Output is correct |
5 |
Correct |
9 ms |
376 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
376 KB |
Output is correct |
9 |
Correct |
29 ms |
2168 KB |
Output is correct |
10 |
Correct |
28 ms |
2168 KB |
Output is correct |
11 |
Correct |
35 ms |
2168 KB |
Output is correct |
12 |
Correct |
33 ms |
2168 KB |
Output is correct |
13 |
Correct |
34 ms |
2168 KB |
Output is correct |
14 |
Correct |
32 ms |
2296 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
504 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
21 ms |
1784 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
27 ms |
2168 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
25 ms |
2168 KB |
Output is correct |
9 |
Correct |
25 ms |
2168 KB |
Output is correct |
10 |
Correct |
26 ms |
2296 KB |
Output is correct |
11 |
Correct |
27 ms |
2168 KB |
Output is correct |
12 |
Correct |
26 ms |
2168 KB |
Output is correct |
13 |
Correct |
5 ms |
376 KB |
Output is correct |
14 |
Correct |
5 ms |
376 KB |
Output is correct |
15 |
Correct |
5 ms |
376 KB |
Output is correct |
16 |
Correct |
5 ms |
376 KB |
Output is correct |
17 |
Correct |
5 ms |
376 KB |
Output is correct |
18 |
Correct |
5 ms |
376 KB |
Output is correct |
19 |
Correct |
29 ms |
2168 KB |
Output is correct |
20 |
Correct |
27 ms |
2168 KB |
Output is correct |
21 |
Correct |
27 ms |
2164 KB |
Output is correct |
22 |
Correct |
27 ms |
2168 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
9 ms |
504 KB |
Output is correct |
4 |
Correct |
9 ms |
504 KB |
Output is correct |
5 |
Correct |
5 ms |
504 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
376 KB |
Output is correct |
9 |
Correct |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
62 ms |
1144 KB |
Output is correct |
11 |
Correct |
82 ms |
1400 KB |
Output is correct |
12 |
Correct |
79 ms |
1272 KB |
Output is correct |
13 |
Correct |
5 ms |
376 KB |
Output is correct |
14 |
Correct |
8 ms |
376 KB |
Output is correct |
15 |
Correct |
7 ms |
376 KB |
Output is correct |
16 |
Correct |
7 ms |
376 KB |
Output is correct |
17 |
Correct |
37 ms |
2296 KB |
Output is correct |
18 |
Correct |
34 ms |
2168 KB |
Output is correct |
19 |
Correct |
37 ms |
2168 KB |
Output is correct |
20 |
Correct |
7 ms |
508 KB |
Output is correct |
21 |
Correct |
9 ms |
376 KB |
Output is correct |
22 |
Correct |
5 ms |
376 KB |
Output is correct |
23 |
Correct |
5 ms |
376 KB |
Output is correct |
24 |
Correct |
5 ms |
376 KB |
Output is correct |
25 |
Correct |
29 ms |
2168 KB |
Output is correct |
26 |
Correct |
28 ms |
2168 KB |
Output is correct |
27 |
Correct |
35 ms |
2168 KB |
Output is correct |
28 |
Correct |
33 ms |
2168 KB |
Output is correct |
29 |
Correct |
34 ms |
2168 KB |
Output is correct |
30 |
Correct |
32 ms |
2296 KB |
Output is correct |
31 |
Correct |
6 ms |
504 KB |
Output is correct |
32 |
Correct |
5 ms |
376 KB |
Output is correct |
33 |
Correct |
21 ms |
1784 KB |
Output is correct |
34 |
Correct |
5 ms |
376 KB |
Output is correct |
35 |
Correct |
27 ms |
2168 KB |
Output is correct |
36 |
Correct |
5 ms |
376 KB |
Output is correct |
37 |
Correct |
5 ms |
376 KB |
Output is correct |
38 |
Correct |
25 ms |
2168 KB |
Output is correct |
39 |
Correct |
25 ms |
2168 KB |
Output is correct |
40 |
Correct |
26 ms |
2296 KB |
Output is correct |
41 |
Correct |
27 ms |
2168 KB |
Output is correct |
42 |
Correct |
26 ms |
2168 KB |
Output is correct |
43 |
Correct |
5 ms |
376 KB |
Output is correct |
44 |
Correct |
5 ms |
376 KB |
Output is correct |
45 |
Correct |
5 ms |
376 KB |
Output is correct |
46 |
Correct |
5 ms |
376 KB |
Output is correct |
47 |
Correct |
5 ms |
376 KB |
Output is correct |
48 |
Correct |
5 ms |
376 KB |
Output is correct |
49 |
Correct |
29 ms |
2168 KB |
Output is correct |
50 |
Correct |
27 ms |
2168 KB |
Output is correct |
51 |
Correct |
27 ms |
2164 KB |
Output is correct |
52 |
Correct |
27 ms |
2168 KB |
Output is correct |
53 |
Correct |
34 ms |
2168 KB |
Output is correct |
54 |
Correct |
38 ms |
2168 KB |
Output is correct |
55 |
Correct |
35 ms |
2296 KB |
Output is correct |
56 |
Correct |
8 ms |
508 KB |
Output is correct |
57 |
Correct |
7 ms |
376 KB |
Output is correct |
58 |
Correct |
132 ms |
3448 KB |
Output is correct |
59 |
Correct |
147 ms |
3832 KB |
Output is correct |
60 |
Correct |
204 ms |
4984 KB |
Output is correct |
61 |
Correct |
128 ms |
3320 KB |
Output is correct |
62 |
Correct |
153 ms |
3704 KB |
Output is correct |
63 |
Correct |
196 ms |
4728 KB |
Output is correct |
64 |
Correct |
271 ms |
21164 KB |
Output is correct |
65 |
Correct |
275 ms |
21808 KB |
Output is correct |
66 |
Correct |
343 ms |
28272 KB |
Output is correct |
67 |
Correct |
21 ms |
1908 KB |
Output is correct |
68 |
Correct |
31 ms |
2172 KB |
Output is correct |
69 |
Correct |
30 ms |
2172 KB |
Output is correct |
70 |
Correct |
36 ms |
2296 KB |
Output is correct |
71 |
Correct |
34 ms |
2296 KB |
Output is correct |
72 |
Correct |
35 ms |
2168 KB |
Output is correct |
73 |
Correct |
35 ms |
2296 KB |
Output is correct |
74 |
Correct |
34 ms |
2168 KB |
Output is correct |