#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
typedef pair<int, int> pii;
#define MAXN 200
#define MAXM 50000
struct Edge {
int u, v, c, d;
};
int n, m;
Edge edges[MAXM];
list<int> adj[MAXN];
int dist[MAXN][MAXN];
int dis[MAXN];
int cost(int s, int t, int ri){
fill(dis, dis+n, INF);
priority_queue<pii, vector<pii>, greater<pii>> pq;
pq.push({0, s});
dis[s] = 0;
while (!pq.empty()){
auto [d, u] = pq.top();
pq.pop();
if (dis[u] != d) continue;
if (u == t) return d;
if (ri != -1 && edges[ri].v == u){
int v = edges[ri].u, cd = edges[ri].c;
if (d + cd < dis[v]){
dis[v] = d + cd;
pq.push({dis[v], v});
}
}
for (int i : adj[u]){
if (i == ri) continue;
int v = edges[i].v, cd = edges[i].c;
if (d + cd < dis[v]){
dis[v] = d + cd;
pq.push({dis[v], v});
}
}
}
return dis[t];
}
int cost(int ri = -1){ return cost(0, n-1, ri) + cost(n-1, 0, ri); }
int main(){
// freopen("sol.in", "r", stdin);
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> m;
for (int i = 0; i < n; i++){
for (int j = 0; j < n; j++)
dist[i][j] = INF;
dist[i][i] = 0;
}
for (int i = 0; i < m; i++){
auto &[u, v, c, d] = edges[i];
cin >> u >> v >> c >> d;
u--, v--;
adj[u].push_back(i);
dist[u][v] = min(dist[u][v], c);
}
for (int k = 0; k < n; k++)
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
int ans = cost();
for (int i = 0; i < m; i++){
auto [u, v, c, d] = edges[i];
int fw = min(dist[0][n-1], dist[0][v] + c + dist[u][n-1]),
bw = min(dist[n-1][0], dist[n-1][v] + c + dist[u][0]);
if (fw < INF && bw < INF && (fw + bw + d) < ans)
ans = min(ans, cost(i) + d);
}
cout << (ans >= INF ? -1 : ans) << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
604 KB |
Output is correct |
2 |
Correct |
7 ms |
604 KB |
Output is correct |
3 |
Correct |
8 ms |
604 KB |
Output is correct |
4 |
Correct |
8 ms |
668 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
11 ms |
860 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
10 ms |
604 KB |
Output is correct |
11 |
Correct |
17 ms |
604 KB |
Output is correct |
12 |
Correct |
8 ms |
604 KB |
Output is correct |
13 |
Correct |
8 ms |
604 KB |
Output is correct |
14 |
Correct |
8 ms |
604 KB |
Output is correct |
15 |
Correct |
8 ms |
604 KB |
Output is correct |
16 |
Correct |
8 ms |
672 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
2944 KB |
Output is correct |
2 |
Correct |
22 ms |
2980 KB |
Output is correct |
3 |
Correct |
26 ms |
2908 KB |
Output is correct |
4 |
Correct |
8 ms |
604 KB |
Output is correct |
5 |
Correct |
8 ms |
604 KB |
Output is correct |
6 |
Correct |
9 ms |
600 KB |
Output is correct |
7 |
Correct |
8 ms |
604 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
31 ms |
2908 KB |
Output is correct |
10 |
Correct |
26 ms |
2908 KB |
Output is correct |
11 |
Correct |
22 ms |
2908 KB |
Output is correct |
12 |
Correct |
24 ms |
2948 KB |
Output is correct |
13 |
Correct |
22 ms |
2904 KB |
Output is correct |
14 |
Correct |
24 ms |
2908 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
600 KB |
Output is correct |
2 |
Correct |
10 ms |
776 KB |
Output is correct |
3 |
Correct |
19 ms |
2396 KB |
Output is correct |
4 |
Correct |
8 ms |
636 KB |
Output is correct |
5 |
Correct |
21 ms |
2908 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
30 ms |
2804 KB |
Output is correct |
9 |
Correct |
39 ms |
2924 KB |
Output is correct |
10 |
Correct |
22 ms |
2908 KB |
Output is correct |
11 |
Correct |
26 ms |
2972 KB |
Output is correct |
12 |
Correct |
23 ms |
2908 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
360 KB |
Output is correct |
16 |
Correct |
0 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 |
22 ms |
2908 KB |
Output is correct |
20 |
Correct |
22 ms |
2908 KB |
Output is correct |
21 |
Correct |
23 ms |
2848 KB |
Output is correct |
22 |
Correct |
23 ms |
2908 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
604 KB |
Output is correct |
2 |
Correct |
7 ms |
604 KB |
Output is correct |
3 |
Correct |
8 ms |
604 KB |
Output is correct |
4 |
Correct |
8 ms |
668 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
11 ms |
860 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
10 ms |
604 KB |
Output is correct |
11 |
Correct |
17 ms |
604 KB |
Output is correct |
12 |
Correct |
8 ms |
604 KB |
Output is correct |
13 |
Correct |
8 ms |
604 KB |
Output is correct |
14 |
Correct |
8 ms |
604 KB |
Output is correct |
15 |
Correct |
8 ms |
604 KB |
Output is correct |
16 |
Correct |
8 ms |
672 KB |
Output is correct |
17 |
Correct |
21 ms |
2944 KB |
Output is correct |
18 |
Correct |
22 ms |
2980 KB |
Output is correct |
19 |
Correct |
26 ms |
2908 KB |
Output is correct |
20 |
Correct |
8 ms |
604 KB |
Output is correct |
21 |
Correct |
8 ms |
604 KB |
Output is correct |
22 |
Correct |
9 ms |
600 KB |
Output is correct |
23 |
Correct |
8 ms |
604 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
31 ms |
2908 KB |
Output is correct |
26 |
Correct |
26 ms |
2908 KB |
Output is correct |
27 |
Correct |
22 ms |
2908 KB |
Output is correct |
28 |
Correct |
24 ms |
2948 KB |
Output is correct |
29 |
Correct |
22 ms |
2904 KB |
Output is correct |
30 |
Correct |
24 ms |
2908 KB |
Output is correct |
31 |
Correct |
8 ms |
600 KB |
Output is correct |
32 |
Correct |
10 ms |
776 KB |
Output is correct |
33 |
Correct |
19 ms |
2396 KB |
Output is correct |
34 |
Correct |
8 ms |
636 KB |
Output is correct |
35 |
Correct |
21 ms |
2908 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
30 ms |
2804 KB |
Output is correct |
39 |
Correct |
39 ms |
2924 KB |
Output is correct |
40 |
Correct |
22 ms |
2908 KB |
Output is correct |
41 |
Correct |
26 ms |
2972 KB |
Output is correct |
42 |
Correct |
23 ms |
2908 KB |
Output is correct |
43 |
Correct |
0 ms |
348 KB |
Output is correct |
44 |
Correct |
0 ms |
348 KB |
Output is correct |
45 |
Correct |
1 ms |
360 KB |
Output is correct |
46 |
Correct |
0 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 |
22 ms |
2908 KB |
Output is correct |
50 |
Correct |
22 ms |
2908 KB |
Output is correct |
51 |
Correct |
23 ms |
2848 KB |
Output is correct |
52 |
Correct |
23 ms |
2908 KB |
Output is correct |
53 |
Correct |
21 ms |
2904 KB |
Output is correct |
54 |
Correct |
21 ms |
2904 KB |
Output is correct |
55 |
Correct |
22 ms |
2908 KB |
Output is correct |
56 |
Correct |
8 ms |
604 KB |
Output is correct |
57 |
Correct |
8 ms |
604 KB |
Output is correct |
58 |
Correct |
24 ms |
2504 KB |
Output is correct |
59 |
Correct |
25 ms |
2376 KB |
Output is correct |
60 |
Correct |
27 ms |
2648 KB |
Output is correct |
61 |
Correct |
27 ms |
2392 KB |
Output is correct |
62 |
Correct |
109 ms |
2508 KB |
Output is correct |
63 |
Correct |
233 ms |
3164 KB |
Output is correct |
64 |
Correct |
68 ms |
3528 KB |
Output is correct |
65 |
Correct |
191 ms |
3704 KB |
Output is correct |
66 |
Correct |
337 ms |
3540 KB |
Output is correct |
67 |
Correct |
15 ms |
2908 KB |
Output is correct |
68 |
Correct |
38 ms |
4048 KB |
Output is correct |
69 |
Correct |
27 ms |
3932 KB |
Output is correct |
70 |
Correct |
24 ms |
3932 KB |
Output is correct |
71 |
Correct |
25 ms |
3928 KB |
Output is correct |
72 |
Correct |
23 ms |
3928 KB |
Output is correct |
73 |
Correct |
24 ms |
3928 KB |
Output is correct |
74 |
Correct |
25 ms |
3932 KB |
Output is correct |