// O O O O O O O O O O O O O O O OO O OO O OO O O O TTCH O TTTCH O TTTCH O O O O
#pragma GCC optimize("Ofast")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("fast-math")
#pragma GCC target("sse,sse2,sse3,ssse3,popcnt,abm,mmx")
#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <stdio.h>
#include <cstdio>
#include <math.h>
#include <cmath>
#include <string>
#include <cstring>
#include <queue>
#include <deque>
// #include <random>
#include <iomanip>
#include <bitset>
#include <cassert>
using namespace std;
#define y1 y11
#define double long double
#define less less228
#define left left228
#define right right228
#define list list228
template<typename T> void uin(T &a, T b) {
if (b < a) a = b;
}
template<typename T> void uax(T &a, T b) {
if (b > a) a = b;
}
// random_device rnd;
// template<typename T> void shuffle(vector< T > &v) {
// for (int i = 1; i < (int)v.size(); ++i) {
// swap(v[rnd() % i], v[i]);
// }
// for (int i = (int)v.size() - 1; i; --i) {
// swap(v[rnd() % i], v[i]);
// }
// }
const int N = 228;
const int M = 50 * 1000 + 228;
const int INF = 1e9 + 228;
struct Edge
{
int u, v, c, d;
Edge() {}
Edge(int _u, int _v, int _c, int _d) {u = _u, v = _v, c = _c, d = _d;}
};
int n, m;
int U[M], V[M], C[M], D[M];
bool taken[M];
int from1[N], fromn[N], to1[N], ton[N];
int d[N], pr[N];
vector<int> g[N];
priority_queue< pair<int, int> > q;
int kekos(int s, int t) {
for (int i = 1; i <= n; ++i) {
d[i] = INF;
pr[i] = -1;
}
d[s] = 0;
q.push({0, s});
while (!q.empty()) {
pair<int, int> P = q.top();
q.pop();
int v = P.second;
for (int i : g[v]) {
if (d[v] + C[i] < d[V[i]]) {
d[V[i]] = d[v] + C[i];
pr[V[i]] = i;
q.push({-d[V[i]], V[i]});
}
}
}
int x = d[t];
if (x == INF) return x;
while (t != s) {
taken[pr[t]] = 1;
t = U[pr[t]];
}
return x;
}
int dij(int s, int t) {
for (int i = 1; i <= n; ++i) {
d[i] = INF;
}
d[s] = 0;
q.push({0, s});
while (!q.empty()) {
pair<int, int> P = q.top();
q.pop();
int v = P.second;
for (int i : g[v]) {
if (d[v] + C[i] < d[V[i]]) {
d[V[i]] = d[v] + C[i];
q.push({-d[V[i]], V[i]});
}
}
}
return d[t];
}
void reset() {
for (int i = 1; i <= n; ++i) {
g[i].clear();
}
for (int i = 0; i < m; ++i) {
g[U[i]].push_back(i);
}
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m;
vector< Edge > edges;
for (int i = 0; i < m; ++i) {
cin >> U[i] >> V[i] >> C[i] >> D[i];
edges.emplace_back(U[i], V[i], C[i], D[i]);
g[U[i]].emplace_back(i);
}
int path1 = kekos(1, n);
int path2 = kekos(n, 1);
int answer = 2 * INF;
if (path1 != INF && path2 != INF) {
uin(answer, path1 + path2);
}
dij(1, 0);
for (int i = 1; i <= n; ++i) {
from1[i] = d[i];
}
dij(n, 0);
for (int i = 1; i <= n; ++i) {
fromn[i] = d[i];
}
for (int i = 0; i < m; ++i) {
swap(U[i], V[i]);
}
reset();
dij(1, 0);
for (int i = 1; i <= n; ++i) {
to1[i] = d[i];
}
dij(n, 0);
for (int i = 1; i <= n; ++i) {
ton[i] = d[i];
}
for (int i = 0; i < m; ++i) {
swap(U[i], V[i]);
}
reset();
for (int E = 0; E < m; ++E) {
if (!taken[E]) {
int npath1 = min(path1, from1[V[E]] + ton[U[E]] + C[E]);
int npath2 = min(path2, fromn[V[E]] + to1[U[E]] + C[E]);
if (npath1 < INF && npath2 < INF) {
uin(answer, D[E] + npath1 + npath2);
}
} else {
swap(U[E], V[E]);
reset();
int go1 = dij(1, n);
int go2 = dij(n, 1);
if (go1 < INF && go2 < INF) {
uin(answer, D[E] + dij(1, n) + dij(n, 1));
}
swap(U[E], V[E]);
reset();
}
}
if (answer > 2 * INF - 1000000) answer = -1;
cout << answer << '\n';
return 0;
}
// RU_023
/*
4 5
1 2 4 4
1 3 2 1
4 3 1 2
4 1 6 1
2 4 2 5
---
10
4 10
1 2 4 4
1 2 4 4
1 3 2 1
1 3 2 1
4 3 1 2
4 3 1 2
4 1 6 1
4 1 6 1
2 4 2 5
2 4 2 5
---
10
4 4
1 2 0 4
1 3 0 1
4 3 0 2
4 1 0 1
---
2
4 5
1 2 4 4
1 3 2 4
4 3 1 5
4 1 6 1
2 4 2 5
---
12
4 5
2 1 4 4
1 3 2 1
4 3 1 2
4 3 6 1
2 4 2 5
---
-1
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
7 ms |
376 KB |
Output is correct |
4 |
Correct |
8 ms |
376 KB |
Output is correct |
5 |
Correct |
5 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 |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
47 ms |
376 KB |
Output is correct |
11 |
Correct |
60 ms |
376 KB |
Output is correct |
12 |
Correct |
58 ms |
380 KB |
Output is correct |
13 |
Correct |
5 ms |
376 KB |
Output is correct |
14 |
Correct |
6 ms |
376 KB |
Output is correct |
15 |
Correct |
6 ms |
376 KB |
Output is correct |
16 |
Correct |
6 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
54 ms |
2344 KB |
Output is correct |
2 |
Correct |
55 ms |
2352 KB |
Output is correct |
3 |
Correct |
54 ms |
2352 KB |
Output is correct |
4 |
Correct |
7 ms |
504 KB |
Output is correct |
5 |
Correct |
7 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 |
32 ms |
2344 KB |
Output is correct |
10 |
Correct |
31 ms |
2344 KB |
Output is correct |
11 |
Correct |
48 ms |
2348 KB |
Output is correct |
12 |
Correct |
44 ms |
2352 KB |
Output is correct |
13 |
Correct |
46 ms |
2352 KB |
Output is correct |
14 |
Correct |
44 ms |
2352 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
26 ms |
2100 KB |
Output is correct |
4 |
Correct |
6 ms |
376 KB |
Output is correct |
5 |
Correct |
33 ms |
2492 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
26 ms |
2340 KB |
Output is correct |
9 |
Correct |
27 ms |
2340 KB |
Output is correct |
10 |
Correct |
28 ms |
2348 KB |
Output is correct |
11 |
Correct |
29 ms |
2348 KB |
Output is correct |
12 |
Correct |
32 ms |
2348 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 |
30 ms |
2348 KB |
Output is correct |
20 |
Correct |
29 ms |
2348 KB |
Output is correct |
21 |
Correct |
30 ms |
2248 KB |
Output is correct |
22 |
Correct |
29 ms |
2348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
7 ms |
376 KB |
Output is correct |
4 |
Correct |
8 ms |
376 KB |
Output is correct |
5 |
Correct |
5 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 |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
47 ms |
376 KB |
Output is correct |
11 |
Correct |
60 ms |
376 KB |
Output is correct |
12 |
Correct |
58 ms |
380 KB |
Output is correct |
13 |
Correct |
5 ms |
376 KB |
Output is correct |
14 |
Correct |
6 ms |
376 KB |
Output is correct |
15 |
Correct |
6 ms |
376 KB |
Output is correct |
16 |
Correct |
6 ms |
376 KB |
Output is correct |
17 |
Correct |
54 ms |
2344 KB |
Output is correct |
18 |
Correct |
55 ms |
2352 KB |
Output is correct |
19 |
Correct |
54 ms |
2352 KB |
Output is correct |
20 |
Correct |
7 ms |
504 KB |
Output is correct |
21 |
Correct |
7 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 |
32 ms |
2344 KB |
Output is correct |
26 |
Correct |
31 ms |
2344 KB |
Output is correct |
27 |
Correct |
48 ms |
2348 KB |
Output is correct |
28 |
Correct |
44 ms |
2352 KB |
Output is correct |
29 |
Correct |
46 ms |
2352 KB |
Output is correct |
30 |
Correct |
44 ms |
2352 KB |
Output is correct |
31 |
Correct |
7 ms |
376 KB |
Output is correct |
32 |
Correct |
5 ms |
376 KB |
Output is correct |
33 |
Correct |
26 ms |
2100 KB |
Output is correct |
34 |
Correct |
6 ms |
376 KB |
Output is correct |
35 |
Correct |
33 ms |
2492 KB |
Output is correct |
36 |
Correct |
5 ms |
376 KB |
Output is correct |
37 |
Correct |
5 ms |
376 KB |
Output is correct |
38 |
Correct |
26 ms |
2340 KB |
Output is correct |
39 |
Correct |
27 ms |
2340 KB |
Output is correct |
40 |
Correct |
28 ms |
2348 KB |
Output is correct |
41 |
Correct |
29 ms |
2348 KB |
Output is correct |
42 |
Correct |
32 ms |
2348 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 |
30 ms |
2348 KB |
Output is correct |
50 |
Correct |
29 ms |
2348 KB |
Output is correct |
51 |
Correct |
30 ms |
2248 KB |
Output is correct |
52 |
Correct |
29 ms |
2348 KB |
Output is correct |
53 |
Correct |
54 ms |
2348 KB |
Output is correct |
54 |
Correct |
72 ms |
2344 KB |
Output is correct |
55 |
Correct |
65 ms |
2348 KB |
Output is correct |
56 |
Correct |
7 ms |
376 KB |
Output is correct |
57 |
Correct |
6 ms |
376 KB |
Output is correct |
58 |
Correct |
516 ms |
2100 KB |
Output is correct |
59 |
Correct |
637 ms |
2100 KB |
Output is correct |
60 |
Correct |
885 ms |
1968 KB |
Output is correct |
61 |
Correct |
482 ms |
2100 KB |
Output is correct |
62 |
Correct |
580 ms |
2100 KB |
Output is correct |
63 |
Correct |
866 ms |
2096 KB |
Output is correct |
64 |
Execution timed out |
1092 ms |
2100 KB |
Time limit exceeded |
65 |
Halted |
0 ms |
0 KB |
- |