#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int mxn = 203;
const ll inf = 1e12;
vector<vector<ll>> adj, rev;
vector<vector<bool>> inpath;
vector<array<int, 2>> edges[mxn][mxn];
int n, m;
vector<int> dijkstra(int src, vector<ll> &dist, vector<vector<ll>> &g) {
dist = vector<ll>(n, inf);
dist[src] = 0;
vector<int> p(n, -1);
vector<bool> vis(n, false);
while(true) {
pair<ll, int> mn = {inf, -1};
for(int i = 0; i < n; i++) if(!vis[i])
mn = min(mn, {dist[i], i});
if(mn.first >= inf)
break;
vis[mn.second] = true;
for(int u = 0; u < n; u++) {
if(g[mn.second][u] >= inf) continue;
ll ndist = mn.first + g[mn.second][u];
if(ndist < dist[u]) {
dist[u] = ndist;
p[u] = mn.second;
}
}
}
return p;
}
void extract(int end, vector<int> p) {
vector<int> path;
path.push_back(end);
while(p[path.back()] > -1)
path.push_back(p[path.back()]);
reverse(path.begin(), path.end());
for(int i = 1; i < (int) path.size(); i++)
inpath[path[i - 1]][path[i]] = true;
}
template<typename T>
void out(vector<T> a) {
for(T x : a)
cout << x << ' ';
cout << endl;
}
signed main() {
std::ios::sync_with_stdio(0);
std::cout.tie(0);
std::cin.tie(0);
cin >> n >> m;
adj = rev = vector<vector<ll>>(n, vector<ll>(n, inf));
inpath = vector<vector<bool>>(n, vector<bool>(n, false));
for(int u, v, c, d, i = 0; i < m; i++) {
cin >> u >> v >> c >> d;
u--, v--;
adj[u][v] = min(adj[u][v], (ll) c);
rev[v][u] = min(rev[v][u], (ll) c);
edges[u][v].push_back({c, d});
}
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
sort(edges[i][j].begin(), edges[i][j].end());
vector<ll> d1, dn, r1, rn;
extract(n - 1, dijkstra(0, d1, adj));
extract(0, dijkstra(n - 1, dn, adj));
dijkstra(0, r1, rev);
dijkstra(n - 1, rn, rev);
//out(d1); out(dn); out(r1); out(rn);
ll ans = d1[n - 1] + dn[0];
for(int u = 0; u < n; u++) {
for(int v = 0; v < n; v++) if(!edges[u][v].empty()) {
if(inpath[u][v]) {
ll uv = adj[u][v], vu = adj[v][u];
adj[u][v] = (edges[u][v].size() > 1 ? edges[u][v][1][0] : inf);
adj[v][u] = edges[u][v][0][0];
vector<ll> nd1, ndn;
dijkstra(0, nd1, adj);
dijkstra(n - 1, ndn, adj);
ll cost = nd1[n - 1] + ndn[0] + edges[u][v][0][1];
ans = min(ans, cost);
adj[u][v] = uv;
adj[v][u] = vu;
}
for(int i = 0 + inpath[u][v]; i < (int) edges[u][v].size(); i++) {
auto [c, d] = edges[u][v][i];
ll c1 = d1[v] + rn[u] + c + d; //1 -> v -> u -> n
ll c2 = dn[v] + r1[u] + c + d; //n -> v -> u -> 1
ans = min({ans, c1 + dn[0], c2 + d1[n - 1], c1 + c2 - d});
}
}
}
cout << (ans >= inf ? -1 : ans) << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
1868 KB |
Output is correct |
2 |
Correct |
1 ms |
1868 KB |
Output is correct |
3 |
Correct |
5 ms |
1868 KB |
Output is correct |
4 |
Correct |
8 ms |
1868 KB |
Output is correct |
5 |
Correct |
1 ms |
1228 KB |
Output is correct |
6 |
Correct |
2 ms |
1868 KB |
Output is correct |
7 |
Correct |
1 ms |
1228 KB |
Output is correct |
8 |
Correct |
1 ms |
1228 KB |
Output is correct |
9 |
Correct |
1 ms |
1228 KB |
Output is correct |
10 |
Correct |
69 ms |
1868 KB |
Output is correct |
11 |
Correct |
88 ms |
1988 KB |
Output is correct |
12 |
Correct |
82 ms |
1868 KB |
Output is correct |
13 |
Correct |
2 ms |
1868 KB |
Output is correct |
14 |
Correct |
5 ms |
1868 KB |
Output is correct |
15 |
Correct |
4 ms |
1896 KB |
Output is correct |
16 |
Correct |
4 ms |
1984 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
28 ms |
2568 KB |
Output is correct |
2 |
Correct |
41 ms |
2620 KB |
Output is correct |
3 |
Correct |
28 ms |
2640 KB |
Output is correct |
4 |
Correct |
6 ms |
1964 KB |
Output is correct |
5 |
Correct |
6 ms |
1968 KB |
Output is correct |
6 |
Correct |
1 ms |
1868 KB |
Output is correct |
7 |
Correct |
1 ms |
1868 KB |
Output is correct |
8 |
Correct |
1 ms |
1228 KB |
Output is correct |
9 |
Correct |
22 ms |
2628 KB |
Output is correct |
10 |
Correct |
22 ms |
2508 KB |
Output is correct |
11 |
Correct |
27 ms |
2584 KB |
Output is correct |
12 |
Correct |
26 ms |
2560 KB |
Output is correct |
13 |
Correct |
24 ms |
2508 KB |
Output is correct |
14 |
Correct |
25 ms |
2508 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
1868 KB |
Output is correct |
2 |
Correct |
2 ms |
1868 KB |
Output is correct |
3 |
Correct |
22 ms |
2764 KB |
Output is correct |
4 |
Correct |
1 ms |
1868 KB |
Output is correct |
5 |
Correct |
35 ms |
2856 KB |
Output is correct |
6 |
Correct |
1 ms |
1228 KB |
Output is correct |
7 |
Correct |
1 ms |
1228 KB |
Output is correct |
8 |
Correct |
23 ms |
2812 KB |
Output is correct |
9 |
Correct |
24 ms |
2856 KB |
Output is correct |
10 |
Correct |
23 ms |
2764 KB |
Output is correct |
11 |
Correct |
25 ms |
2764 KB |
Output is correct |
12 |
Correct |
32 ms |
2696 KB |
Output is correct |
13 |
Correct |
2 ms |
1228 KB |
Output is correct |
14 |
Correct |
1 ms |
1228 KB |
Output is correct |
15 |
Correct |
1 ms |
1228 KB |
Output is correct |
16 |
Correct |
1 ms |
1228 KB |
Output is correct |
17 |
Correct |
1 ms |
1228 KB |
Output is correct |
18 |
Correct |
1 ms |
1228 KB |
Output is correct |
19 |
Correct |
26 ms |
2788 KB |
Output is correct |
20 |
Correct |
26 ms |
2696 KB |
Output is correct |
21 |
Correct |
24 ms |
2796 KB |
Output is correct |
22 |
Correct |
25 ms |
2872 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
1868 KB |
Output is correct |
2 |
Correct |
1 ms |
1868 KB |
Output is correct |
3 |
Correct |
5 ms |
1868 KB |
Output is correct |
4 |
Correct |
8 ms |
1868 KB |
Output is correct |
5 |
Correct |
1 ms |
1228 KB |
Output is correct |
6 |
Correct |
2 ms |
1868 KB |
Output is correct |
7 |
Correct |
1 ms |
1228 KB |
Output is correct |
8 |
Correct |
1 ms |
1228 KB |
Output is correct |
9 |
Correct |
1 ms |
1228 KB |
Output is correct |
10 |
Correct |
69 ms |
1868 KB |
Output is correct |
11 |
Correct |
88 ms |
1988 KB |
Output is correct |
12 |
Correct |
82 ms |
1868 KB |
Output is correct |
13 |
Correct |
2 ms |
1868 KB |
Output is correct |
14 |
Correct |
5 ms |
1868 KB |
Output is correct |
15 |
Correct |
4 ms |
1896 KB |
Output is correct |
16 |
Correct |
4 ms |
1984 KB |
Output is correct |
17 |
Correct |
28 ms |
2568 KB |
Output is correct |
18 |
Correct |
41 ms |
2620 KB |
Output is correct |
19 |
Correct |
28 ms |
2640 KB |
Output is correct |
20 |
Correct |
6 ms |
1964 KB |
Output is correct |
21 |
Correct |
6 ms |
1968 KB |
Output is correct |
22 |
Correct |
1 ms |
1868 KB |
Output is correct |
23 |
Correct |
1 ms |
1868 KB |
Output is correct |
24 |
Correct |
1 ms |
1228 KB |
Output is correct |
25 |
Correct |
22 ms |
2628 KB |
Output is correct |
26 |
Correct |
22 ms |
2508 KB |
Output is correct |
27 |
Correct |
27 ms |
2584 KB |
Output is correct |
28 |
Correct |
26 ms |
2560 KB |
Output is correct |
29 |
Correct |
24 ms |
2508 KB |
Output is correct |
30 |
Correct |
25 ms |
2508 KB |
Output is correct |
31 |
Correct |
8 ms |
1868 KB |
Output is correct |
32 |
Correct |
2 ms |
1868 KB |
Output is correct |
33 |
Correct |
22 ms |
2764 KB |
Output is correct |
34 |
Correct |
1 ms |
1868 KB |
Output is correct |
35 |
Correct |
35 ms |
2856 KB |
Output is correct |
36 |
Correct |
1 ms |
1228 KB |
Output is correct |
37 |
Correct |
1 ms |
1228 KB |
Output is correct |
38 |
Correct |
23 ms |
2812 KB |
Output is correct |
39 |
Correct |
24 ms |
2856 KB |
Output is correct |
40 |
Correct |
23 ms |
2764 KB |
Output is correct |
41 |
Correct |
25 ms |
2764 KB |
Output is correct |
42 |
Correct |
32 ms |
2696 KB |
Output is correct |
43 |
Correct |
2 ms |
1228 KB |
Output is correct |
44 |
Correct |
1 ms |
1228 KB |
Output is correct |
45 |
Correct |
1 ms |
1228 KB |
Output is correct |
46 |
Correct |
1 ms |
1228 KB |
Output is correct |
47 |
Correct |
1 ms |
1228 KB |
Output is correct |
48 |
Correct |
1 ms |
1228 KB |
Output is correct |
49 |
Correct |
26 ms |
2788 KB |
Output is correct |
50 |
Correct |
26 ms |
2696 KB |
Output is correct |
51 |
Correct |
24 ms |
2796 KB |
Output is correct |
52 |
Correct |
25 ms |
2872 KB |
Output is correct |
53 |
Correct |
27 ms |
2876 KB |
Output is correct |
54 |
Correct |
43 ms |
2884 KB |
Output is correct |
55 |
Correct |
32 ms |
2876 KB |
Output is correct |
56 |
Correct |
6 ms |
1868 KB |
Output is correct |
57 |
Correct |
4 ms |
1868 KB |
Output is correct |
58 |
Correct |
135 ms |
2792 KB |
Output is correct |
59 |
Correct |
151 ms |
2688 KB |
Output is correct |
60 |
Correct |
204 ms |
2764 KB |
Output is correct |
61 |
Correct |
146 ms |
2776 KB |
Output is correct |
62 |
Correct |
159 ms |
2764 KB |
Output is correct |
63 |
Correct |
198 ms |
2780 KB |
Output is correct |
64 |
Correct |
116 ms |
2664 KB |
Output is correct |
65 |
Correct |
128 ms |
2664 KB |
Output is correct |
66 |
Correct |
158 ms |
2668 KB |
Output is correct |
67 |
Correct |
16 ms |
1872 KB |
Output is correct |
68 |
Correct |
26 ms |
2844 KB |
Output is correct |
69 |
Correct |
32 ms |
2788 KB |
Output is correct |
70 |
Correct |
42 ms |
2740 KB |
Output is correct |
71 |
Correct |
29 ms |
2692 KB |
Output is correct |
72 |
Correct |
26 ms |
2760 KB |
Output is correct |
73 |
Correct |
39 ms |
2768 KB |
Output is correct |
74 |
Correct |
42 ms |
2672 KB |
Output is correct |