#include <bits/stdc++.h>
using namespace std;
#define ll long long
const ll N = 201, M = 50001;
ll n, m;
struct Edge {
ll u, v, i;
};
multiset<pair<ll, ll>> adj[N];
ll c[M], d[M];
Edge edges[M];
vector<ll> sssp(ll source) {
vector<ll> dist(n + 1, 1e15);
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();
for (auto &nb : adj[node]) {
if (dist[nb.first] > _dist + c[nb.second]) {
dist[nb.first] = _dist + c[nb.second];
pq.push({-dist[nb.first], nb.first});
}
}
}
return dist;
}
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].insert({v, i});
edges[i].u = u, edges[i].v = v, edges[i].i = 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]);
sort(edges, edges + m, [&dist](const Edge &e1, const Edge &e2) {
ll u1 = e1.u, u2 = e2.u, v1 = e1.v, v2 = e2.v, i1 = e1.i, i2 = e2.i;
ll d1 = min(dist[1][n], dist[1][v1] + c[i1] + dist[u1][n]) +
min(dist[n][1], dist[n][v1] + c[i1] + dist[u1][1]) + d[i1];
ll d2 = min(dist[1][n], dist[1][v2] + c[i2] + dist[u2][n]) +
min(dist[n][1], dist[n][v2] + c[i2] + dist[u2][1]) + d[i2];
return d1 < d2;
});
for (ll i = 0; i < m; ++i) {
ll u = edges[i].u, v = edges[i].v, _i = edges[i].i;
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});
ans = min(ans, d[_i] + sssp(1)[n] + sssp(n)[1]);
adj[v].erase(adj[v].find({u, _i}));
adj[u].insert({v, _i});
}
}
if (ans == (ll)(1e15)) {
ans = -1;
}
cout << ans << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 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 |
344 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 |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
11 ms |
860 KB |
Output is correct |
11 |
Correct |
26 ms |
860 KB |
Output is correct |
12 |
Correct |
9 ms |
860 KB |
Output is correct |
13 |
Correct |
7 ms |
860 KB |
Output is correct |
14 |
Correct |
7 ms |
860 KB |
Output is correct |
15 |
Correct |
8 ms |
856 KB |
Output is correct |
16 |
Correct |
9 ms |
860 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
39 ms |
5796 KB |
Output is correct |
2 |
Correct |
41 ms |
5720 KB |
Output is correct |
3 |
Correct |
37 ms |
5724 KB |
Output is correct |
4 |
Correct |
10 ms |
1008 KB |
Output is correct |
5 |
Correct |
7 ms |
856 KB |
Output is correct |
6 |
Correct |
7 ms |
860 KB |
Output is correct |
7 |
Correct |
7 ms |
844 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
53 ms |
5664 KB |
Output is correct |
10 |
Correct |
42 ms |
5876 KB |
Output is correct |
11 |
Correct |
42 ms |
5724 KB |
Output is correct |
12 |
Correct |
43 ms |
5724 KB |
Output is correct |
13 |
Correct |
49 ms |
5724 KB |
Output is correct |
14 |
Correct |
43 ms |
5688 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
860 KB |
Output is correct |
2 |
Correct |
7 ms |
824 KB |
Output is correct |
3 |
Correct |
30 ms |
4700 KB |
Output is correct |
4 |
Correct |
7 ms |
600 KB |
Output is correct |
5 |
Correct |
37 ms |
5724 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
40 ms |
5724 KB |
Output is correct |
9 |
Correct |
39 ms |
5724 KB |
Output is correct |
10 |
Correct |
39 ms |
5648 KB |
Output is correct |
11 |
Correct |
44 ms |
5880 KB |
Output is correct |
12 |
Correct |
46 ms |
5716 KB |
Output is correct |
13 |
Correct |
1 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 |
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 |
43 ms |
5744 KB |
Output is correct |
20 |
Correct |
39 ms |
5712 KB |
Output is correct |
21 |
Correct |
41 ms |
5720 KB |
Output is correct |
22 |
Correct |
42 ms |
5644 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 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 |
344 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 |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
11 ms |
860 KB |
Output is correct |
11 |
Correct |
26 ms |
860 KB |
Output is correct |
12 |
Correct |
9 ms |
860 KB |
Output is correct |
13 |
Correct |
7 ms |
860 KB |
Output is correct |
14 |
Correct |
7 ms |
860 KB |
Output is correct |
15 |
Correct |
8 ms |
856 KB |
Output is correct |
16 |
Correct |
9 ms |
860 KB |
Output is correct |
17 |
Correct |
39 ms |
5796 KB |
Output is correct |
18 |
Correct |
41 ms |
5720 KB |
Output is correct |
19 |
Correct |
37 ms |
5724 KB |
Output is correct |
20 |
Correct |
10 ms |
1008 KB |
Output is correct |
21 |
Correct |
7 ms |
856 KB |
Output is correct |
22 |
Correct |
7 ms |
860 KB |
Output is correct |
23 |
Correct |
7 ms |
844 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
53 ms |
5664 KB |
Output is correct |
26 |
Correct |
42 ms |
5876 KB |
Output is correct |
27 |
Correct |
42 ms |
5724 KB |
Output is correct |
28 |
Correct |
43 ms |
5724 KB |
Output is correct |
29 |
Correct |
49 ms |
5724 KB |
Output is correct |
30 |
Correct |
43 ms |
5688 KB |
Output is correct |
31 |
Correct |
7 ms |
860 KB |
Output is correct |
32 |
Correct |
7 ms |
824 KB |
Output is correct |
33 |
Correct |
30 ms |
4700 KB |
Output is correct |
34 |
Correct |
7 ms |
600 KB |
Output is correct |
35 |
Correct |
37 ms |
5724 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
40 ms |
5724 KB |
Output is correct |
39 |
Correct |
39 ms |
5724 KB |
Output is correct |
40 |
Correct |
39 ms |
5648 KB |
Output is correct |
41 |
Correct |
44 ms |
5880 KB |
Output is correct |
42 |
Correct |
46 ms |
5716 KB |
Output is correct |
43 |
Correct |
1 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 |
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 |
43 ms |
5744 KB |
Output is correct |
50 |
Correct |
39 ms |
5712 KB |
Output is correct |
51 |
Correct |
41 ms |
5720 KB |
Output is correct |
52 |
Correct |
42 ms |
5644 KB |
Output is correct |
53 |
Correct |
37 ms |
5656 KB |
Output is correct |
54 |
Correct |
38 ms |
5724 KB |
Output is correct |
55 |
Correct |
38 ms |
5976 KB |
Output is correct |
56 |
Correct |
7 ms |
860 KB |
Output is correct |
57 |
Correct |
8 ms |
904 KB |
Output is correct |
58 |
Correct |
39 ms |
4696 KB |
Output is correct |
59 |
Correct |
39 ms |
4700 KB |
Output is correct |
60 |
Correct |
46 ms |
4860 KB |
Output is correct |
61 |
Correct |
71 ms |
4696 KB |
Output is correct |
62 |
Correct |
564 ms |
4700 KB |
Output is correct |
63 |
Execution timed out |
1094 ms |
4696 KB |
Time limit exceeded |
64 |
Halted |
0 ms |
0 KB |
- |