#include <bits/stdc++.h>
using namespace std;
#define TRACE(x) cerr << #x << " :: " << x << endl
#define _ << " " <<
#define SZ(x) (int)(x).size()
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define RFOR(i,a,b) for (int i=(a);i>=(b);--i)
const int MX_N = 205;
const int MX_M = 5e4+5;
const long long INF = 1e18;
struct Edge { int v, c, d, i; };
int N, M;
vector<Edge> al[2][MX_N];
long long dist[4][MX_N];
void dijkstra(int s, vector<Edge>* al, long long* dist, int blocked) {
fill(dist+1,dist+1+N,INF);
dist[s] = 0;
bool done[N+1];
fill(done+1,done+1+N,0);
while (true) {
int u = -1;
FOR(v,1,N) if (!done[v] && (u == -1 || dist[v] < dist[u])) u = v;
if (u == -1) break;
done[u] = 1;
for (auto e : al[u]) if (e.i != blocked) {
if (dist[u] + e.c < dist[e.v]) {
dist[e.v] = dist[u] + e.c;
}
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> N >> M;
if (M <= 1000) {
FOR(i,1,M){
int U, V, C, D; cin >> U >> V >> C >> D;
al[0][U].push_back({V,C,D,i});
}
dijkstra(1,al[0],dist[0],-1);
dijkstra(N,al[0],dist[1],-1);
long long ans = min(INF, (long long) dist[0][N] + dist[1][1]);
FOR(u,1,N){
for (auto& e : al[0][u]) {
al[0][e.v].push_back({u,e.c,e.d,M+1});
dijkstra(1,al[0],dist[0],e.i);
dijkstra(N,al[0],dist[1],e.i);
al[0][e.v].pop_back();
ans = min(ans, (long long) dist[0][N] + dist[1][1] + e.d);
}
}
cout << (ans == INF ? -1 : ans) << '\n';
} else {
FOR(i,1,M){
int U, V, C, D; cin >> U >> V >> C >> D;
al[0][U].push_back({V,C,D,i});
al[1][V].push_back({U,C,D,M+i});
}
dijkstra(1,al[0],dist[0],-1);
dijkstra(1,al[1],dist[1],-1);
dijkstra(N,al[0],dist[2],-1);
dijkstra(N,al[1],dist[3],-1);
long long ans = min(INF, (long long) dist[0][N] + dist[2][1]);
FOR(u,1,N){
for (auto& e : al[0][u]) {
//TRACE(e.i _ dist[0][N] _ dist[2][e.v] _ dist[1][u] _ e.d);
//TRACE(e.i _ dist[0][e.v] _ dist[3][u] _ dist[2][1] _ e.d);
ans = min(ans, (long long) dist[0][N] + dist[2][e.v] + e.c + dist[1][u] + e.d);
ans = min(ans, (long long) dist[0][e.v] + e.c + dist[3][u] + dist[2][1] + e.d);
ans = min(ans, (long long) dist[0][e.v] + e.c + dist[3][u] + dist[2][e.v] + e.c + dist[1][u] + e.d);
}
}
cout << (ans == INF ? -1 : ans) << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
184 ms |
384 KB |
Output is correct |
2 |
Correct |
48 ms |
504 KB |
Output is correct |
3 |
Correct |
192 ms |
460 KB |
Output is correct |
4 |
Correct |
202 ms |
384 KB |
Output is correct |
5 |
Correct |
14 ms |
384 KB |
Output is correct |
6 |
Correct |
47 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
8 ms |
384 KB |
Output is correct |
10 |
Correct |
198 ms |
456 KB |
Output is correct |
11 |
Correct |
199 ms |
384 KB |
Output is correct |
12 |
Correct |
199 ms |
460 KB |
Output is correct |
13 |
Correct |
185 ms |
384 KB |
Output is correct |
14 |
Correct |
185 ms |
384 KB |
Output is correct |
15 |
Correct |
187 ms |
508 KB |
Output is correct |
16 |
Correct |
184 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
29 ms |
3840 KB |
Output is correct |
2 |
Correct |
28 ms |
3836 KB |
Output is correct |
3 |
Correct |
30 ms |
4088 KB |
Output is correct |
4 |
Correct |
6 ms |
512 KB |
Output is correct |
5 |
Correct |
189 ms |
516 KB |
Output is correct |
6 |
Correct |
105 ms |
384 KB |
Output is correct |
7 |
Correct |
78 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
29 ms |
3968 KB |
Output is correct |
10 |
Correct |
28 ms |
3968 KB |
Output is correct |
11 |
Correct |
28 ms |
4096 KB |
Output is correct |
12 |
Correct |
28 ms |
4096 KB |
Output is correct |
13 |
Correct |
29 ms |
4096 KB |
Output is correct |
14 |
Correct |
30 ms |
4352 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
186 ms |
384 KB |
Output is correct |
2 |
Correct |
47 ms |
384 KB |
Output is correct |
3 |
Correct |
24 ms |
2816 KB |
Output is correct |
4 |
Correct |
47 ms |
384 KB |
Output is correct |
5 |
Correct |
26 ms |
3712 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
4 ms |
384 KB |
Output is correct |
8 |
Correct |
26 ms |
3712 KB |
Output is correct |
9 |
Correct |
27 ms |
3712 KB |
Output is correct |
10 |
Incorrect |
27 ms |
3840 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
184 ms |
384 KB |
Output is correct |
2 |
Correct |
48 ms |
504 KB |
Output is correct |
3 |
Correct |
192 ms |
460 KB |
Output is correct |
4 |
Correct |
202 ms |
384 KB |
Output is correct |
5 |
Correct |
14 ms |
384 KB |
Output is correct |
6 |
Correct |
47 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
8 ms |
384 KB |
Output is correct |
10 |
Correct |
198 ms |
456 KB |
Output is correct |
11 |
Correct |
199 ms |
384 KB |
Output is correct |
12 |
Correct |
199 ms |
460 KB |
Output is correct |
13 |
Correct |
185 ms |
384 KB |
Output is correct |
14 |
Correct |
185 ms |
384 KB |
Output is correct |
15 |
Correct |
187 ms |
508 KB |
Output is correct |
16 |
Correct |
184 ms |
384 KB |
Output is correct |
17 |
Correct |
29 ms |
3840 KB |
Output is correct |
18 |
Correct |
28 ms |
3836 KB |
Output is correct |
19 |
Correct |
30 ms |
4088 KB |
Output is correct |
20 |
Correct |
6 ms |
512 KB |
Output is correct |
21 |
Correct |
189 ms |
516 KB |
Output is correct |
22 |
Correct |
105 ms |
384 KB |
Output is correct |
23 |
Correct |
78 ms |
384 KB |
Output is correct |
24 |
Correct |
5 ms |
384 KB |
Output is correct |
25 |
Correct |
29 ms |
3968 KB |
Output is correct |
26 |
Correct |
28 ms |
3968 KB |
Output is correct |
27 |
Correct |
28 ms |
4096 KB |
Output is correct |
28 |
Correct |
28 ms |
4096 KB |
Output is correct |
29 |
Correct |
29 ms |
4096 KB |
Output is correct |
30 |
Correct |
30 ms |
4352 KB |
Output is correct |
31 |
Correct |
186 ms |
384 KB |
Output is correct |
32 |
Correct |
47 ms |
384 KB |
Output is correct |
33 |
Correct |
24 ms |
2816 KB |
Output is correct |
34 |
Correct |
47 ms |
384 KB |
Output is correct |
35 |
Correct |
26 ms |
3712 KB |
Output is correct |
36 |
Correct |
5 ms |
384 KB |
Output is correct |
37 |
Correct |
4 ms |
384 KB |
Output is correct |
38 |
Correct |
26 ms |
3712 KB |
Output is correct |
39 |
Correct |
27 ms |
3712 KB |
Output is correct |
40 |
Incorrect |
27 ms |
3840 KB |
Output isn't correct |
41 |
Halted |
0 ms |
0 KB |
- |