#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;
}
vector<ll> adjall[N];
bool vis[N];
void dfs(ll node) {
if (vis[node]) {
return;
}
vis[node] = true;
for (auto &i : adjall[node]) {
dfs(i);
}
}
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}), adjall[u].push_back(v), adjall[v].push_back(u);
edges[i].u = u, edges[i].v = v, edges[i].i = i;
dist[u][v] = min(dist[u][v], c[i]);
}
dfs(1);
if (!vis[n]) {
cout << "-1\n";
return 0;
}
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 |
7 ms |
860 KB |
Output is correct |
4 |
Correct |
8 ms |
948 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
8 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
11 ms |
956 KB |
Output is correct |
11 |
Correct |
22 ms |
964 KB |
Output is correct |
12 |
Correct |
11 ms |
948 KB |
Output is correct |
13 |
Correct |
10 ms |
948 KB |
Output is correct |
14 |
Correct |
8 ms |
948 KB |
Output is correct |
15 |
Correct |
9 ms |
944 KB |
Output is correct |
16 |
Correct |
8 ms |
860 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
37 ms |
7004 KB |
Output is correct |
2 |
Correct |
43 ms |
7004 KB |
Output is correct |
3 |
Correct |
37 ms |
6872 KB |
Output is correct |
4 |
Correct |
8 ms |
860 KB |
Output is correct |
5 |
Correct |
7 ms |
860 KB |
Output is correct |
6 |
Correct |
7 ms |
860 KB |
Output is correct |
7 |
Correct |
1 ms |
860 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
54 ms |
6856 KB |
Output is correct |
10 |
Correct |
47 ms |
7000 KB |
Output is correct |
11 |
Correct |
44 ms |
7080 KB |
Output is correct |
12 |
Correct |
44 ms |
6992 KB |
Output is correct |
13 |
Correct |
43 ms |
7000 KB |
Output is correct |
14 |
Correct |
43 ms |
7000 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
1112 KB |
Output is correct |
2 |
Correct |
7 ms |
604 KB |
Output is correct |
3 |
Correct |
31 ms |
5616 KB |
Output is correct |
4 |
Correct |
7 ms |
604 KB |
Output is correct |
5 |
Correct |
45 ms |
6992 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
43 ms |
7004 KB |
Output is correct |
9 |
Correct |
54 ms |
7004 KB |
Output is correct |
10 |
Correct |
49 ms |
7004 KB |
Output is correct |
11 |
Correct |
40 ms |
6748 KB |
Output is correct |
12 |
Correct |
40 ms |
7004 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 |
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 |
42 ms |
7124 KB |
Output is correct |
20 |
Correct |
41 ms |
7004 KB |
Output is correct |
21 |
Correct |
41 ms |
6996 KB |
Output is correct |
22 |
Correct |
40 ms |
7000 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 |
7 ms |
860 KB |
Output is correct |
4 |
Correct |
8 ms |
948 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
8 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
11 ms |
956 KB |
Output is correct |
11 |
Correct |
22 ms |
964 KB |
Output is correct |
12 |
Correct |
11 ms |
948 KB |
Output is correct |
13 |
Correct |
10 ms |
948 KB |
Output is correct |
14 |
Correct |
8 ms |
948 KB |
Output is correct |
15 |
Correct |
9 ms |
944 KB |
Output is correct |
16 |
Correct |
8 ms |
860 KB |
Output is correct |
17 |
Correct |
37 ms |
7004 KB |
Output is correct |
18 |
Correct |
43 ms |
7004 KB |
Output is correct |
19 |
Correct |
37 ms |
6872 KB |
Output is correct |
20 |
Correct |
8 ms |
860 KB |
Output is correct |
21 |
Correct |
7 ms |
860 KB |
Output is correct |
22 |
Correct |
7 ms |
860 KB |
Output is correct |
23 |
Correct |
1 ms |
860 KB |
Output is correct |
24 |
Correct |
0 ms |
344 KB |
Output is correct |
25 |
Correct |
54 ms |
6856 KB |
Output is correct |
26 |
Correct |
47 ms |
7000 KB |
Output is correct |
27 |
Correct |
44 ms |
7080 KB |
Output is correct |
28 |
Correct |
44 ms |
6992 KB |
Output is correct |
29 |
Correct |
43 ms |
7000 KB |
Output is correct |
30 |
Correct |
43 ms |
7000 KB |
Output is correct |
31 |
Correct |
7 ms |
1112 KB |
Output is correct |
32 |
Correct |
7 ms |
604 KB |
Output is correct |
33 |
Correct |
31 ms |
5616 KB |
Output is correct |
34 |
Correct |
7 ms |
604 KB |
Output is correct |
35 |
Correct |
45 ms |
6992 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
43 ms |
7004 KB |
Output is correct |
39 |
Correct |
54 ms |
7004 KB |
Output is correct |
40 |
Correct |
49 ms |
7004 KB |
Output is correct |
41 |
Correct |
40 ms |
6748 KB |
Output is correct |
42 |
Correct |
40 ms |
7004 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 |
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 |
42 ms |
7124 KB |
Output is correct |
50 |
Correct |
41 ms |
7004 KB |
Output is correct |
51 |
Correct |
41 ms |
6996 KB |
Output is correct |
52 |
Correct |
40 ms |
7000 KB |
Output is correct |
53 |
Correct |
39 ms |
6736 KB |
Output is correct |
54 |
Correct |
40 ms |
7000 KB |
Output is correct |
55 |
Correct |
40 ms |
7004 KB |
Output is correct |
56 |
Correct |
8 ms |
860 KB |
Output is correct |
57 |
Correct |
8 ms |
860 KB |
Output is correct |
58 |
Correct |
40 ms |
5716 KB |
Output is correct |
59 |
Correct |
40 ms |
5712 KB |
Output is correct |
60 |
Correct |
40 ms |
5716 KB |
Output is correct |
61 |
Correct |
72 ms |
5724 KB |
Output is correct |
62 |
Correct |
563 ms |
5724 KB |
Output is correct |
63 |
Execution timed out |
1069 ms |
5716 KB |
Time limit exceeded |
64 |
Halted |
0 ms |
0 KB |
- |