#include <bits/stdc++.h>
using namespace std;
#define ll long long
const ll N = 201, M = 50001;
ll n, m;
vector<pair<ll, ll>> adj[N];
ll c[M], d[M];
pair<ll, ll> edges[M];
vector<ll> sssp(ll source, ll i, bool b) {
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 (edges[nb.second].first != node) {
if (!(nb.second == i && b)) {
continue;
}
} else if (nb.second == i and b) {
continue;
}
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].push_back({v, i});
adj[v].push_back({u, i});
edges[i] = {u, v};
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]);
for (ll i = 0; i < m; ++i) {
ll 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) {
ans = min(ans, d[i] + sssp(1, i, 1)[n] + sssp(n, i, 1)[1]);
}
}
if (ans == (ll)(1e15)) {
ans = -1;
}
cout << ans << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
856 KB |
Output is correct |
2 |
Correct |
7 ms |
604 KB |
Output is correct |
3 |
Correct |
7 ms |
888 KB |
Output is correct |
4 |
Correct |
7 ms |
860 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
9 ms |
820 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 |
12 ms |
1112 KB |
Output is correct |
11 |
Correct |
24 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 |
7 ms |
860 KB |
Output is correct |
15 |
Correct |
8 ms |
860 KB |
Output is correct |
16 |
Correct |
7 ms |
860 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
4700 KB |
Output is correct |
2 |
Correct |
28 ms |
4652 KB |
Output is correct |
3 |
Correct |
20 ms |
4440 KB |
Output is correct |
4 |
Correct |
8 ms |
860 KB |
Output is correct |
5 |
Correct |
8 ms |
860 KB |
Output is correct |
6 |
Correct |
7 ms |
604 KB |
Output is correct |
7 |
Correct |
7 ms |
836 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
63 ms |
4580 KB |
Output is correct |
10 |
Correct |
44 ms |
4700 KB |
Output is correct |
11 |
Correct |
29 ms |
4700 KB |
Output is correct |
12 |
Correct |
29 ms |
4752 KB |
Output is correct |
13 |
Correct |
25 ms |
4700 KB |
Output is correct |
14 |
Correct |
24 ms |
4952 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
856 KB |
Output is correct |
2 |
Correct |
7 ms |
600 KB |
Output is correct |
3 |
Correct |
17 ms |
3676 KB |
Output is correct |
4 |
Correct |
7 ms |
604 KB |
Output is correct |
5 |
Correct |
19 ms |
4672 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
38 ms |
4536 KB |
Output is correct |
9 |
Correct |
39 ms |
4700 KB |
Output is correct |
10 |
Correct |
21 ms |
4696 KB |
Output is correct |
11 |
Correct |
26 ms |
4656 KB |
Output is correct |
12 |
Correct |
20 ms |
4992 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 |
21 ms |
4744 KB |
Output is correct |
20 |
Correct |
22 ms |
4440 KB |
Output is correct |
21 |
Correct |
21 ms |
4700 KB |
Output is correct |
22 |
Correct |
20 ms |
4696 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
856 KB |
Output is correct |
2 |
Correct |
7 ms |
604 KB |
Output is correct |
3 |
Correct |
7 ms |
888 KB |
Output is correct |
4 |
Correct |
7 ms |
860 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
9 ms |
820 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 |
12 ms |
1112 KB |
Output is correct |
11 |
Correct |
24 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 |
7 ms |
860 KB |
Output is correct |
15 |
Correct |
8 ms |
860 KB |
Output is correct |
16 |
Correct |
7 ms |
860 KB |
Output is correct |
17 |
Correct |
20 ms |
4700 KB |
Output is correct |
18 |
Correct |
28 ms |
4652 KB |
Output is correct |
19 |
Correct |
20 ms |
4440 KB |
Output is correct |
20 |
Correct |
8 ms |
860 KB |
Output is correct |
21 |
Correct |
8 ms |
860 KB |
Output is correct |
22 |
Correct |
7 ms |
604 KB |
Output is correct |
23 |
Correct |
7 ms |
836 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
63 ms |
4580 KB |
Output is correct |
26 |
Correct |
44 ms |
4700 KB |
Output is correct |
27 |
Correct |
29 ms |
4700 KB |
Output is correct |
28 |
Correct |
29 ms |
4752 KB |
Output is correct |
29 |
Correct |
25 ms |
4700 KB |
Output is correct |
30 |
Correct |
24 ms |
4952 KB |
Output is correct |
31 |
Correct |
7 ms |
856 KB |
Output is correct |
32 |
Correct |
7 ms |
600 KB |
Output is correct |
33 |
Correct |
17 ms |
3676 KB |
Output is correct |
34 |
Correct |
7 ms |
604 KB |
Output is correct |
35 |
Correct |
19 ms |
4672 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
38 ms |
4536 KB |
Output is correct |
39 |
Correct |
39 ms |
4700 KB |
Output is correct |
40 |
Correct |
21 ms |
4696 KB |
Output is correct |
41 |
Correct |
26 ms |
4656 KB |
Output is correct |
42 |
Correct |
20 ms |
4992 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 |
21 ms |
4744 KB |
Output is correct |
50 |
Correct |
22 ms |
4440 KB |
Output is correct |
51 |
Correct |
21 ms |
4700 KB |
Output is correct |
52 |
Correct |
20 ms |
4696 KB |
Output is correct |
53 |
Correct |
22 ms |
4600 KB |
Output is correct |
54 |
Correct |
20 ms |
4752 KB |
Output is correct |
55 |
Correct |
23 ms |
4648 KB |
Output is correct |
56 |
Correct |
7 ms |
860 KB |
Output is correct |
57 |
Correct |
7 ms |
860 KB |
Output is correct |
58 |
Correct |
47 ms |
3768 KB |
Output is correct |
59 |
Correct |
48 ms |
3676 KB |
Output is correct |
60 |
Correct |
58 ms |
3676 KB |
Output is correct |
61 |
Correct |
54 ms |
3676 KB |
Output is correct |
62 |
Correct |
449 ms |
3836 KB |
Output is correct |
63 |
Execution timed out |
1045 ms |
3676 KB |
Time limit exceeded |
64 |
Halted |
0 ms |
0 KB |
- |