#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
#define ll long long
const ll N = 201, M = 50001;
ll n, m;
struct Edge {
ll u, v, i, c;
};
vector<pair<ll, pair<ll, bool>>> adj[N];
ll c[M], d[M];
Edge edges[M];
ll sssp(ll source, ll i, ll dest) {
vector<ll> dist(n + 1, 1e15);
vector<bool> vis(n + 1);
dist[source] = 0;
priority_queue<pair<ll, ll>> pq;
pq.push({0, source});
while (!pq.empty()) {
ll node = pq.top().second, _dist = -pq.top().first;
pq.pop();
if (vis[node]) {
continue;
}
vis[node] = true;
if (node == dest) {
return dist[node];
}
for (auto &nb : adj[node]) {
if (nb.second.second) {
if (nb.second.first != i) {
continue;
}
} else if (nb.second.first == i) {
continue;
}
if (dist[nb.first] > _dist + c[nb.second.first]) {
dist[nb.first] = _dist + c[nb.second.first];
pq.push({-dist[nb.first], nb.first});
}
}
}
return dist[dest];
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> m;
for (ll i = 1; i <= n; ++i) {
adj[i].clear();
}
vector<vector<ll>> dist(n + 1, vector<ll>(n + 1, 1e15));
for (ll i = 1; i <= n; ++i) {
dist[i][i] = 0;
}
for (ll i = 0, u, v; i < m; ++i) {
cin >> u >> v >> c[i] >> d[i];
adj[u].push_back({v, {i, 0}}), adj[v].push_back({u, {i, 1}});
edges[i].u = u, edges[i].v = v, edges[i].i = i, edges[i].c = c[i];
dist[u][v] = min(dist[u][v], c[i]);
}
for (ll k = 1; k <= n; ++k) {
for (ll i = 1; i <= n; ++i) {
for (ll j = 1; j <= n; ++j) {
dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
}
}
}
ll ans = min((ll)1e15, dist[1][n] + dist[n][1]);
for (auto &[u, v, i, c] : edges) {
ll d1 = min(dist[1][n], dist[1][v] + c + dist[u][n]);
ll d2 = min(dist[n][1], dist[n][v] + c + dist[u][1]);
if (d1 + d2 + d[i] < ans) {
ll actual = d[i] + sssp(1, i, n) + sssp(n, i, 1);
ans = min(ans, actual);
}
}
if (ans == (ll)(1e15)) {
ans = -1;
}
cout << ans << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
2908 KB |
Output is correct |
2 |
Correct |
5 ms |
2652 KB |
Output is correct |
3 |
Correct |
7 ms |
2908 KB |
Output is correct |
4 |
Correct |
7 ms |
2964 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
5 ms |
2652 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
10 ms |
2908 KB |
Output is correct |
11 |
Correct |
18 ms |
2964 KB |
Output is correct |
12 |
Correct |
7 ms |
2908 KB |
Output is correct |
13 |
Correct |
7 ms |
2908 KB |
Output is correct |
14 |
Correct |
6 ms |
2908 KB |
Output is correct |
15 |
Correct |
6 ms |
2968 KB |
Output is correct |
16 |
Correct |
6 ms |
2908 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
6704 KB |
Output is correct |
2 |
Correct |
23 ms |
6456 KB |
Output is correct |
3 |
Correct |
23 ms |
6236 KB |
Output is correct |
4 |
Correct |
7 ms |
2904 KB |
Output is correct |
5 |
Correct |
6 ms |
2904 KB |
Output is correct |
6 |
Correct |
5 ms |
2652 KB |
Output is correct |
7 |
Correct |
5 ms |
2652 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
28 ms |
6772 KB |
Output is correct |
10 |
Correct |
24 ms |
6492 KB |
Output is correct |
11 |
Correct |
21 ms |
6748 KB |
Output is correct |
12 |
Correct |
22 ms |
6600 KB |
Output is correct |
13 |
Correct |
21 ms |
6672 KB |
Output is correct |
14 |
Correct |
21 ms |
6748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
2904 KB |
Output is correct |
2 |
Correct |
5 ms |
2652 KB |
Output is correct |
3 |
Correct |
15 ms |
5724 KB |
Output is correct |
4 |
Correct |
5 ms |
2652 KB |
Output is correct |
5 |
Correct |
21 ms |
6864 KB |
Output is correct |
6 |
Correct |
1 ms |
2392 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
25 ms |
6492 KB |
Output is correct |
9 |
Correct |
30 ms |
6524 KB |
Output is correct |
10 |
Correct |
19 ms |
6504 KB |
Output is correct |
11 |
Correct |
23 ms |
6492 KB |
Output is correct |
12 |
Correct |
18 ms |
6748 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
20 ms |
6824 KB |
Output is correct |
20 |
Correct |
20 ms |
6492 KB |
Output is correct |
21 |
Correct |
22 ms |
6488 KB |
Output is correct |
22 |
Correct |
20 ms |
6492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
2908 KB |
Output is correct |
2 |
Correct |
5 ms |
2652 KB |
Output is correct |
3 |
Correct |
7 ms |
2908 KB |
Output is correct |
4 |
Correct |
7 ms |
2964 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
5 ms |
2652 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
10 ms |
2908 KB |
Output is correct |
11 |
Correct |
18 ms |
2964 KB |
Output is correct |
12 |
Correct |
7 ms |
2908 KB |
Output is correct |
13 |
Correct |
7 ms |
2908 KB |
Output is correct |
14 |
Correct |
6 ms |
2908 KB |
Output is correct |
15 |
Correct |
6 ms |
2968 KB |
Output is correct |
16 |
Correct |
6 ms |
2908 KB |
Output is correct |
17 |
Correct |
23 ms |
6704 KB |
Output is correct |
18 |
Correct |
23 ms |
6456 KB |
Output is correct |
19 |
Correct |
23 ms |
6236 KB |
Output is correct |
20 |
Correct |
7 ms |
2904 KB |
Output is correct |
21 |
Correct |
6 ms |
2904 KB |
Output is correct |
22 |
Correct |
5 ms |
2652 KB |
Output is correct |
23 |
Correct |
5 ms |
2652 KB |
Output is correct |
24 |
Correct |
1 ms |
2396 KB |
Output is correct |
25 |
Correct |
28 ms |
6772 KB |
Output is correct |
26 |
Correct |
24 ms |
6492 KB |
Output is correct |
27 |
Correct |
21 ms |
6748 KB |
Output is correct |
28 |
Correct |
22 ms |
6600 KB |
Output is correct |
29 |
Correct |
21 ms |
6672 KB |
Output is correct |
30 |
Correct |
21 ms |
6748 KB |
Output is correct |
31 |
Correct |
6 ms |
2904 KB |
Output is correct |
32 |
Correct |
5 ms |
2652 KB |
Output is correct |
33 |
Correct |
15 ms |
5724 KB |
Output is correct |
34 |
Correct |
5 ms |
2652 KB |
Output is correct |
35 |
Correct |
21 ms |
6864 KB |
Output is correct |
36 |
Correct |
1 ms |
2392 KB |
Output is correct |
37 |
Correct |
1 ms |
2396 KB |
Output is correct |
38 |
Correct |
25 ms |
6492 KB |
Output is correct |
39 |
Correct |
30 ms |
6524 KB |
Output is correct |
40 |
Correct |
19 ms |
6504 KB |
Output is correct |
41 |
Correct |
23 ms |
6492 KB |
Output is correct |
42 |
Correct |
18 ms |
6748 KB |
Output is correct |
43 |
Correct |
1 ms |
2396 KB |
Output is correct |
44 |
Correct |
1 ms |
2396 KB |
Output is correct |
45 |
Correct |
1 ms |
2396 KB |
Output is correct |
46 |
Correct |
1 ms |
2396 KB |
Output is correct |
47 |
Correct |
1 ms |
2396 KB |
Output is correct |
48 |
Correct |
1 ms |
2396 KB |
Output is correct |
49 |
Correct |
20 ms |
6824 KB |
Output is correct |
50 |
Correct |
20 ms |
6492 KB |
Output is correct |
51 |
Correct |
22 ms |
6488 KB |
Output is correct |
52 |
Correct |
20 ms |
6492 KB |
Output is correct |
53 |
Correct |
23 ms |
6572 KB |
Output is correct |
54 |
Correct |
21 ms |
6544 KB |
Output is correct |
55 |
Correct |
24 ms |
6252 KB |
Output is correct |
56 |
Correct |
6 ms |
2904 KB |
Output is correct |
57 |
Correct |
7 ms |
2908 KB |
Output is correct |
58 |
Correct |
23 ms |
5760 KB |
Output is correct |
59 |
Correct |
22 ms |
5720 KB |
Output is correct |
60 |
Correct |
31 ms |
5644 KB |
Output is correct |
61 |
Correct |
24 ms |
5720 KB |
Output is correct |
62 |
Correct |
98 ms |
5672 KB |
Output is correct |
63 |
Correct |
216 ms |
5720 KB |
Output is correct |
64 |
Correct |
64 ms |
6140 KB |
Output is correct |
65 |
Correct |
183 ms |
6232 KB |
Output is correct |
66 |
Correct |
327 ms |
6152 KB |
Output is correct |
67 |
Correct |
12 ms |
5964 KB |
Output is correct |
68 |
Correct |
28 ms |
7492 KB |
Output is correct |
69 |
Correct |
25 ms |
7668 KB |
Output is correct |
70 |
Correct |
21 ms |
7516 KB |
Output is correct |
71 |
Correct |
29 ms |
7504 KB |
Output is correct |
72 |
Correct |
22 ms |
7772 KB |
Output is correct |
73 |
Correct |
22 ms |
7932 KB |
Output is correct |
74 |
Correct |
23 ms |
7896 KB |
Output is correct |