#include <bits/stdc++.h>
using namespace std;
#define int long long
int n, m;
const int MXN = 205, MXM = 50005;
int apsp[MXN][MXN];
int eds[MXM][4];
vector<int> adj[MXN];
int mxh = INT_MAX * 1000ll;
int dist(int src, int dest, int skip) {
int dists[MXN];
for (int i=0; i<MXN; i++) dists[i] = mxh;
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> proc;
proc.push({0, src});
while (!proc.empty()) {
auto x = proc.top(); proc.pop();
int di = x.first, no = x.second;
if (dists[no] < di) continue;
dists[no] = di;
for (auto ed: adj[no]) {
if (ed != skip) {
int ot = eds[ed][0] + eds[ed][1] - no;
int we = eds[ed][2];
if (dists[ot] > dists[no] + we) {
dists[ot] = dists[no] + we;
proc.push({dists[ot], ot});
}
}
}
if (no == eds[skip][1]) {
int ot = eds[skip][0];
int od = dists[no] + eds[skip][2];
if (dists[ot] > od) {
dists[ot] = od;
proc.push({od, ot});
}
}
}
return dists[dest];
}
signed main() {
for (int i=0; i<MXN; i++) {
for (int j=0; j<MXN; j++) {
apsp[i][j] = mxh;
}
apsp[i][i] = 0;
}
cin >> n >> m;
for (int i=0; i<m; i++) {
int a, b, w, c;
cin >> a >> b >> w >> c;
eds[i][0] = a;
eds[i][1] = b;
eds[i][2] = w;
eds[i][3] = c;
adj[a].push_back(i);
apsp[a][b] = min(apsp[a][b], w);
}
for (int k=1; k<=n; k++) {
for (int i=1; i<=n; i++) {
for (int j=1; j<=n; j++) {
if (apsp[i][k] + apsp[k][j] < apsp[i][j]) {
apsp[i][j] = apsp[i][k] + apsp[k][j];
}
}
}
}
int res = apsp[1][n] + apsp[n][1];
for (int i=0; i<m; i++) {
int a = eds[i][0], b = eds[i][1];
int w = eds[i][2], c = eds[i][3];
int d1 = min(apsp[1][n], apsp[1][b] + w + apsp[a][n]);
int d2 = min(apsp[n][1], apsp[n][b] + w + apsp[a][1]);
if (d1 + c + d2 < res) {
int actual = dist(1, n, i) + dist(n, 1, i) + c;
res = min(res, actual);
}
}
if (res >= mxh) cout << -1 << endl;
else cout << res << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
768 KB |
Output is correct |
2 |
Correct |
10 ms |
640 KB |
Output is correct |
3 |
Correct |
13 ms |
768 KB |
Output is correct |
4 |
Correct |
13 ms |
768 KB |
Output is correct |
5 |
Correct |
3 ms |
768 KB |
Output is correct |
6 |
Correct |
10 ms |
768 KB |
Output is correct |
7 |
Correct |
1 ms |
640 KB |
Output is correct |
8 |
Correct |
1 ms |
640 KB |
Output is correct |
9 |
Correct |
3 ms |
768 KB |
Output is correct |
10 |
Correct |
17 ms |
768 KB |
Output is correct |
11 |
Correct |
27 ms |
768 KB |
Output is correct |
12 |
Correct |
14 ms |
768 KB |
Output is correct |
13 |
Correct |
13 ms |
768 KB |
Output is correct |
14 |
Correct |
12 ms |
768 KB |
Output is correct |
15 |
Correct |
13 ms |
800 KB |
Output is correct |
16 |
Correct |
12 ms |
896 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
115 ms |
3832 KB |
Output is correct |
2 |
Correct |
113 ms |
3832 KB |
Output is correct |
3 |
Correct |
124 ms |
4084 KB |
Output is correct |
4 |
Correct |
17 ms |
768 KB |
Output is correct |
5 |
Correct |
12 ms |
768 KB |
Output is correct |
6 |
Correct |
10 ms |
640 KB |
Output is correct |
7 |
Correct |
10 ms |
724 KB |
Output is correct |
8 |
Correct |
1 ms |
640 KB |
Output is correct |
9 |
Correct |
130 ms |
3960 KB |
Output is correct |
10 |
Correct |
129 ms |
3960 KB |
Output is correct |
11 |
Correct |
130 ms |
4092 KB |
Output is correct |
12 |
Correct |
126 ms |
4088 KB |
Output is correct |
13 |
Correct |
123 ms |
4088 KB |
Output is correct |
14 |
Correct |
125 ms |
4216 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
776 KB |
Output is correct |
2 |
Correct |
9 ms |
640 KB |
Output is correct |
3 |
Correct |
86 ms |
3064 KB |
Output is correct |
4 |
Correct |
10 ms |
640 KB |
Output is correct |
5 |
Correct |
105 ms |
3704 KB |
Output is correct |
6 |
Correct |
1 ms |
640 KB |
Output is correct |
7 |
Correct |
1 ms |
640 KB |
Output is correct |
8 |
Correct |
110 ms |
3704 KB |
Output is correct |
9 |
Correct |
115 ms |
3808 KB |
Output is correct |
10 |
Correct |
107 ms |
3704 KB |
Output is correct |
11 |
Correct |
118 ms |
3728 KB |
Output is correct |
12 |
Correct |
108 ms |
3832 KB |
Output is correct |
13 |
Correct |
1 ms |
640 KB |
Output is correct |
14 |
Correct |
1 ms |
640 KB |
Output is correct |
15 |
Correct |
1 ms |
640 KB |
Output is correct |
16 |
Correct |
1 ms |
672 KB |
Output is correct |
17 |
Correct |
1 ms |
640 KB |
Output is correct |
18 |
Correct |
1 ms |
640 KB |
Output is correct |
19 |
Correct |
104 ms |
3808 KB |
Output is correct |
20 |
Correct |
108 ms |
3704 KB |
Output is correct |
21 |
Correct |
105 ms |
3704 KB |
Output is correct |
22 |
Correct |
104 ms |
3832 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
768 KB |
Output is correct |
2 |
Correct |
10 ms |
640 KB |
Output is correct |
3 |
Correct |
13 ms |
768 KB |
Output is correct |
4 |
Correct |
13 ms |
768 KB |
Output is correct |
5 |
Correct |
3 ms |
768 KB |
Output is correct |
6 |
Correct |
10 ms |
768 KB |
Output is correct |
7 |
Correct |
1 ms |
640 KB |
Output is correct |
8 |
Correct |
1 ms |
640 KB |
Output is correct |
9 |
Correct |
3 ms |
768 KB |
Output is correct |
10 |
Correct |
17 ms |
768 KB |
Output is correct |
11 |
Correct |
27 ms |
768 KB |
Output is correct |
12 |
Correct |
14 ms |
768 KB |
Output is correct |
13 |
Correct |
13 ms |
768 KB |
Output is correct |
14 |
Correct |
12 ms |
768 KB |
Output is correct |
15 |
Correct |
13 ms |
800 KB |
Output is correct |
16 |
Correct |
12 ms |
896 KB |
Output is correct |
17 |
Correct |
115 ms |
3832 KB |
Output is correct |
18 |
Correct |
113 ms |
3832 KB |
Output is correct |
19 |
Correct |
124 ms |
4084 KB |
Output is correct |
20 |
Correct |
17 ms |
768 KB |
Output is correct |
21 |
Correct |
12 ms |
768 KB |
Output is correct |
22 |
Correct |
10 ms |
640 KB |
Output is correct |
23 |
Correct |
10 ms |
724 KB |
Output is correct |
24 |
Correct |
1 ms |
640 KB |
Output is correct |
25 |
Correct |
130 ms |
3960 KB |
Output is correct |
26 |
Correct |
129 ms |
3960 KB |
Output is correct |
27 |
Correct |
130 ms |
4092 KB |
Output is correct |
28 |
Correct |
126 ms |
4088 KB |
Output is correct |
29 |
Correct |
123 ms |
4088 KB |
Output is correct |
30 |
Correct |
125 ms |
4216 KB |
Output is correct |
31 |
Correct |
11 ms |
776 KB |
Output is correct |
32 |
Correct |
9 ms |
640 KB |
Output is correct |
33 |
Correct |
86 ms |
3064 KB |
Output is correct |
34 |
Correct |
10 ms |
640 KB |
Output is correct |
35 |
Correct |
105 ms |
3704 KB |
Output is correct |
36 |
Correct |
1 ms |
640 KB |
Output is correct |
37 |
Correct |
1 ms |
640 KB |
Output is correct |
38 |
Correct |
110 ms |
3704 KB |
Output is correct |
39 |
Correct |
115 ms |
3808 KB |
Output is correct |
40 |
Correct |
107 ms |
3704 KB |
Output is correct |
41 |
Correct |
118 ms |
3728 KB |
Output is correct |
42 |
Correct |
108 ms |
3832 KB |
Output is correct |
43 |
Correct |
1 ms |
640 KB |
Output is correct |
44 |
Correct |
1 ms |
640 KB |
Output is correct |
45 |
Correct |
1 ms |
640 KB |
Output is correct |
46 |
Correct |
1 ms |
672 KB |
Output is correct |
47 |
Correct |
1 ms |
640 KB |
Output is correct |
48 |
Correct |
1 ms |
640 KB |
Output is correct |
49 |
Correct |
104 ms |
3808 KB |
Output is correct |
50 |
Correct |
108 ms |
3704 KB |
Output is correct |
51 |
Correct |
105 ms |
3704 KB |
Output is correct |
52 |
Correct |
104 ms |
3832 KB |
Output is correct |
53 |
Correct |
118 ms |
3896 KB |
Output is correct |
54 |
Correct |
112 ms |
3832 KB |
Output is correct |
55 |
Correct |
113 ms |
3832 KB |
Output is correct |
56 |
Correct |
13 ms |
768 KB |
Output is correct |
57 |
Correct |
14 ms |
768 KB |
Output is correct |
58 |
Correct |
99 ms |
3192 KB |
Output is correct |
59 |
Correct |
99 ms |
3196 KB |
Output is correct |
60 |
Correct |
103 ms |
3196 KB |
Output is correct |
61 |
Correct |
103 ms |
3192 KB |
Output is correct |
62 |
Correct |
141 ms |
3192 KB |
Output is correct |
63 |
Correct |
195 ms |
3192 KB |
Output is correct |
64 |
Correct |
155 ms |
3660 KB |
Output is correct |
65 |
Correct |
293 ms |
3684 KB |
Output is correct |
66 |
Correct |
466 ms |
3672 KB |
Output is correct |
67 |
Correct |
82 ms |
3188 KB |
Output is correct |
68 |
Correct |
130 ms |
3964 KB |
Output is correct |
69 |
Correct |
127 ms |
3960 KB |
Output is correct |
70 |
Correct |
124 ms |
3960 KB |
Output is correct |
71 |
Correct |
124 ms |
3960 KB |
Output is correct |
72 |
Correct |
126 ms |
3960 KB |
Output is correct |
73 |
Correct |
125 ms |
4088 KB |
Output is correct |
74 |
Correct |
126 ms |
3964 KB |
Output is correct |