#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N = 201, M = 50001;
const int INF = 1e15;
int n, m;
multiset<pair<int, int>> adj[N];
int c[M], d[M];
pair<int, int> edges[M];
vector<int> dijkstra(int source){
vector<int> dist(n + 1, INF);
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
pq.push({0, source});
dist[source] = 0;
while (!pq.empty()) {
int u = pq.top().second;
int du = pq.top().first;
pq.pop();
if (du > dist[u]) continue; // Skip if this node is already processed
for (auto &n : adj[u]){
int v = n.first;
int uv_weight = c[n.second];
if (dist[v] > du + uv_weight){
dist[v] = du + uv_weight;
pq.push({dist[v], v});
}
}
}
return dist;
}
signed main(){
cin >> n >> m;
for (int i = 1; i <= n; ++i) {
adj[i].clear();
}
vector<vector<int>> dist(n + 1, vector<int>(n + 1, INF));
for (int i = 1; i <= n; ++i) {
dist[i][i] = 0;
}
for (int i = 0; i < m; ++i){
int u, v;
cin >> u >> v >> c[i] >> d[i];
adj[u].insert({v, i});
edges[i] = {u, v};
dist[u][v] = min(dist[u][v], c[i]);
}
// Floyd Warshall
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 = min(INF, dist[1][n] + dist[n][1]);
for (int i = 0; i < m; ++i) {
int u = edges[i].first, v = edges[i].second;
if (min(dist[1][n], dist[1][v] + c[i] + dist[u][n]) +
min(dist[n][1], dist[n][v] + c[i] + dist[u][1]) + d[i] < ans) {
adj[u].erase(adj[u].find({v, i}));
adj[v].insert({u, i});
vector<int> from_start = dijkstra(1);
vector<int> to_end = dijkstra(n);
ans = min(ans, d[i] + from_start[n] + to_end[1]);
adj[v].erase(adj[v].find({edges[i].first, i}));
adj[u].insert({v, i});
}
}
if (ans == INF) {
ans = -1;
}
cout << ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
860 KB |
Output is correct |
2 |
Correct |
7 ms |
604 KB |
Output is correct |
3 |
Correct |
8 ms |
860 KB |
Output is correct |
4 |
Correct |
8 ms |
860 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 |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
11 ms |
860 KB |
Output is correct |
11 |
Correct |
20 ms |
856 KB |
Output is correct |
12 |
Correct |
8 ms |
860 KB |
Output is correct |
13 |
Correct |
8 ms |
856 KB |
Output is correct |
14 |
Correct |
8 ms |
1124 KB |
Output is correct |
15 |
Correct |
8 ms |
860 KB |
Output is correct |
16 |
Correct |
10 ms |
856 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
58 ms |
5412 KB |
Output is correct |
2 |
Correct |
53 ms |
6400 KB |
Output is correct |
3 |
Correct |
57 ms |
6484 KB |
Output is correct |
4 |
Correct |
10 ms |
856 KB |
Output is correct |
5 |
Correct |
8 ms |
860 KB |
Output is correct |
6 |
Correct |
9 ms |
604 KB |
Output is correct |
7 |
Correct |
8 ms |
604 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
74 ms |
6396 KB |
Output is correct |
10 |
Correct |
64 ms |
6608 KB |
Output is correct |
11 |
Correct |
59 ms |
6488 KB |
Output is correct |
12 |
Correct |
57 ms |
6488 KB |
Output is correct |
13 |
Correct |
55 ms |
6480 KB |
Output is correct |
14 |
Correct |
61 ms |
6740 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
860 KB |
Output is correct |
2 |
Correct |
7 ms |
600 KB |
Output is correct |
3 |
Correct |
40 ms |
4172 KB |
Output is correct |
4 |
Correct |
10 ms |
600 KB |
Output is correct |
5 |
Correct |
69 ms |
5440 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
77 ms |
5448 KB |
Output is correct |
9 |
Correct |
84 ms |
6368 KB |
Output is correct |
10 |
Correct |
52 ms |
6224 KB |
Output is correct |
11 |
Correct |
58 ms |
6160 KB |
Output is correct |
12 |
Correct |
62 ms |
6168 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
64 ms |
6364 KB |
Output is correct |
20 |
Correct |
55 ms |
6204 KB |
Output is correct |
21 |
Correct |
53 ms |
6228 KB |
Output is correct |
22 |
Correct |
53 ms |
6340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
860 KB |
Output is correct |
2 |
Correct |
7 ms |
604 KB |
Output is correct |
3 |
Correct |
8 ms |
860 KB |
Output is correct |
4 |
Correct |
8 ms |
860 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 |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
11 ms |
860 KB |
Output is correct |
11 |
Correct |
20 ms |
856 KB |
Output is correct |
12 |
Correct |
8 ms |
860 KB |
Output is correct |
13 |
Correct |
8 ms |
856 KB |
Output is correct |
14 |
Correct |
8 ms |
1124 KB |
Output is correct |
15 |
Correct |
8 ms |
860 KB |
Output is correct |
16 |
Correct |
10 ms |
856 KB |
Output is correct |
17 |
Correct |
58 ms |
5412 KB |
Output is correct |
18 |
Correct |
53 ms |
6400 KB |
Output is correct |
19 |
Correct |
57 ms |
6484 KB |
Output is correct |
20 |
Correct |
10 ms |
856 KB |
Output is correct |
21 |
Correct |
8 ms |
860 KB |
Output is correct |
22 |
Correct |
9 ms |
604 KB |
Output is correct |
23 |
Correct |
8 ms |
604 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
74 ms |
6396 KB |
Output is correct |
26 |
Correct |
64 ms |
6608 KB |
Output is correct |
27 |
Correct |
59 ms |
6488 KB |
Output is correct |
28 |
Correct |
57 ms |
6488 KB |
Output is correct |
29 |
Correct |
55 ms |
6480 KB |
Output is correct |
30 |
Correct |
61 ms |
6740 KB |
Output is correct |
31 |
Correct |
8 ms |
860 KB |
Output is correct |
32 |
Correct |
7 ms |
600 KB |
Output is correct |
33 |
Correct |
40 ms |
4172 KB |
Output is correct |
34 |
Correct |
10 ms |
600 KB |
Output is correct |
35 |
Correct |
69 ms |
5440 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
77 ms |
5448 KB |
Output is correct |
39 |
Correct |
84 ms |
6368 KB |
Output is correct |
40 |
Correct |
52 ms |
6224 KB |
Output is correct |
41 |
Correct |
58 ms |
6160 KB |
Output is correct |
42 |
Correct |
62 ms |
6168 KB |
Output is correct |
43 |
Correct |
0 ms |
348 KB |
Output is correct |
44 |
Correct |
0 ms |
348 KB |
Output is correct |
45 |
Correct |
0 ms |
348 KB |
Output is correct |
46 |
Correct |
1 ms |
348 KB |
Output is correct |
47 |
Correct |
0 ms |
348 KB |
Output is correct |
48 |
Correct |
0 ms |
348 KB |
Output is correct |
49 |
Correct |
64 ms |
6364 KB |
Output is correct |
50 |
Correct |
55 ms |
6204 KB |
Output is correct |
51 |
Correct |
53 ms |
6228 KB |
Output is correct |
52 |
Correct |
53 ms |
6340 KB |
Output is correct |
53 |
Correct |
57 ms |
6484 KB |
Output is correct |
54 |
Correct |
54 ms |
6412 KB |
Output is correct |
55 |
Correct |
52 ms |
6280 KB |
Output is correct |
56 |
Correct |
8 ms |
860 KB |
Output is correct |
57 |
Correct |
8 ms |
892 KB |
Output is correct |
58 |
Correct |
52 ms |
5296 KB |
Output is correct |
59 |
Correct |
52 ms |
5332 KB |
Output is correct |
60 |
Correct |
56 ms |
5200 KB |
Output is correct |
61 |
Correct |
55 ms |
5204 KB |
Output is correct |
62 |
Correct |
180 ms |
5476 KB |
Output is correct |
63 |
Correct |
369 ms |
5356 KB |
Output is correct |
64 |
Correct |
108 ms |
5724 KB |
Output is correct |
65 |
Correct |
248 ms |
5744 KB |
Output is correct |
66 |
Correct |
490 ms |
5728 KB |
Output is correct |
67 |
Correct |
39 ms |
4944 KB |
Output is correct |
68 |
Correct |
87 ms |
6604 KB |
Output is correct |
69 |
Correct |
72 ms |
6536 KB |
Output is correct |
70 |
Correct |
57 ms |
6484 KB |
Output is correct |
71 |
Correct |
59 ms |
6604 KB |
Output is correct |
72 |
Correct |
58 ms |
6616 KB |
Output is correct |
73 |
Correct |
64 ms |
6744 KB |
Output is correct |
74 |
Correct |
67 ms |
6616 KB |
Output is correct |