#include <bits/stdc++.h>
using namespace std;
#define int long long
#define INF 1e18
#define endl '\n'
const int N = 205;
struct node {
int u, v, c, d;
};
multiset<pair<int, int>> graph[N];
vector<int> dijkstra(int src) {
vector<int> dist(N, INF);
vector<bool> vis(N, false);
dist[src] = 0;
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
pq.push({0, src});
while (!pq.empty()) {
int u = pq.top().second;
pq.pop();
if (vis[u]) continue;
vis[u] = true;
for (auto& [v, weight] : graph[u]) {
if (dist[v] > dist[u] + weight) {
dist[v] = dist[u] + weight;
pq.push({dist[v], v});
}
}
}
return dist;
}
void solve() {
int n, m;
cin >> n >> m;
vector<node> edges(m);
for (int i = 0; i < m; ++i) {
cin >> edges[i].u >> edges[i].v >> edges[i].c >> edges[i].d;
graph[edges[i].u].insert({edges[i].v, edges[i].c});
}
vector<vector<int>> dist(n + 1, vector<int>(n + 1, INF));
for (int i = 1; i <= n; ++i) dist[i][i] = 0;
for (auto& e : edges) {
dist[e.u][e.v] = min(dist[e.u][e.v], e.c);
}
for (int k = 1; k <= n; ++k) {
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
}
}
}
int ans = dist[1][n] + dist[n][1];
for (auto& e : edges) {
int u = e.u, v = e.v;
if (min(dist[1][n], dist[1][v] + e.c + dist[u][n]) + min(dist[n][1], dist[n][v] + e.c + dist[u][1]) + e.d >= ans) continue;
graph[u].erase(graph[u].find({v, e.c}));
graph[v].insert({u, e.c});
auto dis1 = dijkstra(1);
auto dis2 = dijkstra(n);
ans = min(ans, dis1[n] + dis2[1] + e.d);
graph[v].erase(graph[v].find({u, e.c}));
graph[u].insert({v, e.c});
}
if (ans >= INF) cout << -1 << endl;
else cout << ans << endl;
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
solve();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
856 KB |
Output is correct |
2 |
Correct |
7 ms |
604 KB |
Output is correct |
3 |
Correct |
7 ms |
856 KB |
Output is correct |
4 |
Correct |
8 ms |
904 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
7 ms |
604 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
472 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
11 ms |
904 KB |
Output is correct |
11 |
Correct |
20 ms |
860 KB |
Output is correct |
12 |
Correct |
11 ms |
916 KB |
Output is correct |
13 |
Correct |
8 ms |
824 KB |
Output is correct |
14 |
Correct |
8 ms |
912 KB |
Output is correct |
15 |
Correct |
8 ms |
856 KB |
Output is correct |
16 |
Correct |
8 ms |
856 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
29 ms |
6232 KB |
Output is correct |
2 |
Correct |
35 ms |
6488 KB |
Output is correct |
3 |
Correct |
36 ms |
6572 KB |
Output is correct |
4 |
Correct |
8 ms |
860 KB |
Output is correct |
5 |
Correct |
8 ms |
860 KB |
Output is correct |
6 |
Correct |
11 ms |
860 KB |
Output is correct |
7 |
Correct |
8 ms |
860 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
50 ms |
6596 KB |
Output is correct |
10 |
Correct |
38 ms |
6664 KB |
Output is correct |
11 |
Correct |
38 ms |
6608 KB |
Output is correct |
12 |
Correct |
36 ms |
6424 KB |
Output is correct |
13 |
Correct |
35 ms |
6660 KB |
Output is correct |
14 |
Correct |
32 ms |
6488 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
860 KB |
Output is correct |
2 |
Correct |
8 ms |
604 KB |
Output is correct |
3 |
Correct |
28 ms |
4956 KB |
Output is correct |
4 |
Correct |
7 ms |
600 KB |
Output is correct |
5 |
Correct |
29 ms |
6236 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
54 ms |
6312 KB |
Output is correct |
9 |
Correct |
59 ms |
6348 KB |
Output is correct |
10 |
Correct |
30 ms |
6408 KB |
Output is correct |
11 |
Correct |
34 ms |
6224 KB |
Output is correct |
12 |
Correct |
31 ms |
6268 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
464 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
344 KB |
Output is correct |
17 |
Correct |
1 ms |
604 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
30 ms |
6336 KB |
Output is correct |
20 |
Correct |
34 ms |
6232 KB |
Output is correct |
21 |
Correct |
33 ms |
6176 KB |
Output is correct |
22 |
Correct |
30 ms |
6416 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
856 KB |
Output is correct |
2 |
Correct |
7 ms |
604 KB |
Output is correct |
3 |
Correct |
7 ms |
856 KB |
Output is correct |
4 |
Correct |
8 ms |
904 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
7 ms |
604 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
472 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
11 ms |
904 KB |
Output is correct |
11 |
Correct |
20 ms |
860 KB |
Output is correct |
12 |
Correct |
11 ms |
916 KB |
Output is correct |
13 |
Correct |
8 ms |
824 KB |
Output is correct |
14 |
Correct |
8 ms |
912 KB |
Output is correct |
15 |
Correct |
8 ms |
856 KB |
Output is correct |
16 |
Correct |
8 ms |
856 KB |
Output is correct |
17 |
Correct |
29 ms |
6232 KB |
Output is correct |
18 |
Correct |
35 ms |
6488 KB |
Output is correct |
19 |
Correct |
36 ms |
6572 KB |
Output is correct |
20 |
Correct |
8 ms |
860 KB |
Output is correct |
21 |
Correct |
8 ms |
860 KB |
Output is correct |
22 |
Correct |
11 ms |
860 KB |
Output is correct |
23 |
Correct |
8 ms |
860 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
50 ms |
6596 KB |
Output is correct |
26 |
Correct |
38 ms |
6664 KB |
Output is correct |
27 |
Correct |
38 ms |
6608 KB |
Output is correct |
28 |
Correct |
36 ms |
6424 KB |
Output is correct |
29 |
Correct |
35 ms |
6660 KB |
Output is correct |
30 |
Correct |
32 ms |
6488 KB |
Output is correct |
31 |
Correct |
7 ms |
860 KB |
Output is correct |
32 |
Correct |
8 ms |
604 KB |
Output is correct |
33 |
Correct |
28 ms |
4956 KB |
Output is correct |
34 |
Correct |
7 ms |
600 KB |
Output is correct |
35 |
Correct |
29 ms |
6236 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
54 ms |
6312 KB |
Output is correct |
39 |
Correct |
59 ms |
6348 KB |
Output is correct |
40 |
Correct |
30 ms |
6408 KB |
Output is correct |
41 |
Correct |
34 ms |
6224 KB |
Output is correct |
42 |
Correct |
31 ms |
6268 KB |
Output is correct |
43 |
Correct |
0 ms |
348 KB |
Output is correct |
44 |
Correct |
0 ms |
464 KB |
Output is correct |
45 |
Correct |
0 ms |
348 KB |
Output is correct |
46 |
Correct |
1 ms |
344 KB |
Output is correct |
47 |
Correct |
1 ms |
604 KB |
Output is correct |
48 |
Correct |
1 ms |
348 KB |
Output is correct |
49 |
Correct |
30 ms |
6336 KB |
Output is correct |
50 |
Correct |
34 ms |
6232 KB |
Output is correct |
51 |
Correct |
33 ms |
6176 KB |
Output is correct |
52 |
Correct |
30 ms |
6416 KB |
Output is correct |
53 |
Correct |
35 ms |
6360 KB |
Output is correct |
54 |
Correct |
29 ms |
6224 KB |
Output is correct |
55 |
Correct |
29 ms |
6236 KB |
Output is correct |
56 |
Correct |
8 ms |
912 KB |
Output is correct |
57 |
Correct |
8 ms |
860 KB |
Output is correct |
58 |
Correct |
36 ms |
5208 KB |
Output is correct |
59 |
Correct |
34 ms |
5372 KB |
Output is correct |
60 |
Correct |
41 ms |
5208 KB |
Output is correct |
61 |
Correct |
43 ms |
5232 KB |
Output is correct |
62 |
Correct |
154 ms |
5544 KB |
Output is correct |
63 |
Correct |
335 ms |
5360 KB |
Output is correct |
64 |
Correct |
90 ms |
5768 KB |
Output is correct |
65 |
Correct |
233 ms |
5760 KB |
Output is correct |
66 |
Correct |
434 ms |
5812 KB |
Output is correct |
67 |
Correct |
35 ms |
4956 KB |
Output is correct |
68 |
Correct |
48 ms |
6452 KB |
Output is correct |
69 |
Correct |
42 ms |
6652 KB |
Output is correct |
70 |
Correct |
31 ms |
6420 KB |
Output is correct |
71 |
Correct |
34 ms |
6488 KB |
Output is correct |
72 |
Correct |
31 ms |
6484 KB |
Output is correct |
73 |
Correct |
32 ms |
6652 KB |
Output is correct |
74 |
Correct |
34 ms |
6496 KB |
Output is correct |