#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 205, M = 5e4 + 7;
const int INF = 1e18 + 39;
struct Edge{
int v, u, c, d;
Edge() {}
Edge(int v, int u, int c, int d) : v(v), u(u), c(c), d(d) {}
};
int n ,m;
Edge edg[M];
void dijks(int st, int d[], int p[], bool rev, vector<int> g[], int ign) {
for (int i = 0; i < n; i++) {
d[i] = INF;
p[i] = -1;
}
d[st] = 0;
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> q;
q.push({d[st], st});
while (!q.empty()) {
auto cur = q.top();
q.pop();
int v = cur.second;
if (d[v] != cur.first)
continue;
for (auto id : g[v]) {
if (id == ign) continue;
int w = edg[id].c, u = rev ? edg[id].v : edg[id].u;
if (d[v] + w < d[u]) {
d[u] = d[v] + w;
p[u] = id;
q.push({d[u], u});
}
}
}
}
vector<int> g[N], gr[N];
int d1[N], dn[N];
int d1r[N], dnr[N];
int p1[N], pn[N];
int p1r[N], pnr[N];
bool bad1[M], badn[M];
bool bad1r[M], badnr[M];
int tmp_d[N], tmp_p[N];
int get_d1(int v, int ign) {
if (bad1[ign]) {
dijks(0, tmp_d, tmp_p, 0, g, ign);
return tmp_d[v];
}
return d1[v];
}
int get_dn(int v, int ign) {
if (badn[ign]) {
dijks(n - 1, tmp_d, tmp_p, 0, g, ign);
return tmp_d[v];
}
return dn[v];
}
int get_d1r(int v, int ign) {
if (bad1r[ign]) {
dijks(0, tmp_d, tmp_p, 1, gr, ign);
return tmp_d[v];
}
return d1r[v];
}
int get_dnr(int v, int ign) {
if (badnr[ign]) {
dijks(n - 1, tmp_d, tmp_p, 1, gr, ign);
return tmp_d[v];
}
return dnr[v];
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m;
for (int i = 0; i < m; i++) {
cin >> edg[i].v >> edg[i].u >> edg[i].c >> edg[i].d;
edg[i].v--;
edg[i].u--;
g[edg[i].v].push_back(i);
gr[edg[i].u].push_back(i);
}
dijks(0, d1, p1, 0, g, -1);
dijks(0, d1r, p1r, 1, gr, -1);
dijks(n - 1, dn, pn, 0, g, -1);
dijks(n - 1, dnr, pnr, 1, gr, -1);
for (int i = 0; i < n; i++) {
if (~p1[i])
bad1[p1[i]] = 1;
if (~pn[i])
badn[pn[i]] = 1;
if (~p1r[i])
bad1r[p1r[i]] = 1;
if (~pnr[i])
badnr[pnr[i]] = 1;
}
int ans = min(INF, d1[n - 1] + dn[0]);
for (int i = 0; i < m; ++i) {
int dist1 = min(get_d1(n - 1, i), get_d1(edg[i].u, i) + get_dnr(edg[i].v, i) + edg[i].c);
int distn = min(get_dn(0, i), get_dn(edg[i].u, i) + get_d1r(edg[i].v, i) + edg[i].c);
ans = min(ans, dist1 + distn + edg[i].d);
}
if (ans == INF)
cout << "-1\n";
else
cout << ans << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
27 ms |
512 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
38 ms |
512 KB |
Output is correct |
4 |
Correct |
41 ms |
512 KB |
Output is correct |
5 |
Correct |
3 ms |
512 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
54 ms |
512 KB |
Output is correct |
11 |
Correct |
51 ms |
512 KB |
Output is correct |
12 |
Correct |
54 ms |
512 KB |
Output is correct |
13 |
Correct |
8 ms |
512 KB |
Output is correct |
14 |
Correct |
25 ms |
532 KB |
Output is correct |
15 |
Correct |
22 ms |
512 KB |
Output is correct |
16 |
Correct |
23 ms |
536 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
386 ms |
3712 KB |
Output is correct |
2 |
Correct |
380 ms |
4472 KB |
Output is correct |
3 |
Correct |
385 ms |
4608 KB |
Output is correct |
4 |
Correct |
37 ms |
512 KB |
Output is correct |
5 |
Correct |
18 ms |
512 KB |
Output is correct |
6 |
Correct |
3 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
91 ms |
4472 KB |
Output is correct |
10 |
Correct |
84 ms |
4600 KB |
Output is correct |
11 |
Correct |
239 ms |
4608 KB |
Output is correct |
12 |
Correct |
238 ms |
4600 KB |
Output is correct |
13 |
Correct |
239 ms |
4608 KB |
Output is correct |
14 |
Correct |
235 ms |
4736 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
27 ms |
512 KB |
Output is correct |
2 |
Correct |
3 ms |
384 KB |
Output is correct |
3 |
Correct |
226 ms |
3436 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
288 ms |
4352 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
65 ms |
4352 KB |
Output is correct |
9 |
Correct |
72 ms |
4352 KB |
Output is correct |
10 |
Correct |
180 ms |
4344 KB |
Output is correct |
11 |
Correct |
187 ms |
4352 KB |
Output is correct |
12 |
Correct |
185 ms |
4344 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
0 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
0 ms |
384 KB |
Output is correct |
19 |
Correct |
183 ms |
4352 KB |
Output is correct |
20 |
Correct |
176 ms |
4352 KB |
Output is correct |
21 |
Correct |
188 ms |
4352 KB |
Output is correct |
22 |
Correct |
187 ms |
4344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
27 ms |
512 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
38 ms |
512 KB |
Output is correct |
4 |
Correct |
41 ms |
512 KB |
Output is correct |
5 |
Correct |
3 ms |
512 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
54 ms |
512 KB |
Output is correct |
11 |
Correct |
51 ms |
512 KB |
Output is correct |
12 |
Correct |
54 ms |
512 KB |
Output is correct |
13 |
Correct |
8 ms |
512 KB |
Output is correct |
14 |
Correct |
25 ms |
532 KB |
Output is correct |
15 |
Correct |
22 ms |
512 KB |
Output is correct |
16 |
Correct |
23 ms |
536 KB |
Output is correct |
17 |
Correct |
386 ms |
3712 KB |
Output is correct |
18 |
Correct |
380 ms |
4472 KB |
Output is correct |
19 |
Correct |
385 ms |
4608 KB |
Output is correct |
20 |
Correct |
37 ms |
512 KB |
Output is correct |
21 |
Correct |
18 ms |
512 KB |
Output is correct |
22 |
Correct |
3 ms |
384 KB |
Output is correct |
23 |
Correct |
1 ms |
384 KB |
Output is correct |
24 |
Correct |
1 ms |
384 KB |
Output is correct |
25 |
Correct |
91 ms |
4472 KB |
Output is correct |
26 |
Correct |
84 ms |
4600 KB |
Output is correct |
27 |
Correct |
239 ms |
4608 KB |
Output is correct |
28 |
Correct |
238 ms |
4600 KB |
Output is correct |
29 |
Correct |
239 ms |
4608 KB |
Output is correct |
30 |
Correct |
235 ms |
4736 KB |
Output is correct |
31 |
Correct |
27 ms |
512 KB |
Output is correct |
32 |
Correct |
3 ms |
384 KB |
Output is correct |
33 |
Correct |
226 ms |
3436 KB |
Output is correct |
34 |
Correct |
2 ms |
384 KB |
Output is correct |
35 |
Correct |
288 ms |
4352 KB |
Output is correct |
36 |
Correct |
1 ms |
384 KB |
Output is correct |
37 |
Correct |
0 ms |
384 KB |
Output is correct |
38 |
Correct |
65 ms |
4352 KB |
Output is correct |
39 |
Correct |
72 ms |
4352 KB |
Output is correct |
40 |
Correct |
180 ms |
4344 KB |
Output is correct |
41 |
Correct |
187 ms |
4352 KB |
Output is correct |
42 |
Correct |
185 ms |
4344 KB |
Output is correct |
43 |
Correct |
1 ms |
384 KB |
Output is correct |
44 |
Correct |
1 ms |
384 KB |
Output is correct |
45 |
Correct |
1 ms |
384 KB |
Output is correct |
46 |
Correct |
0 ms |
384 KB |
Output is correct |
47 |
Correct |
1 ms |
384 KB |
Output is correct |
48 |
Correct |
0 ms |
384 KB |
Output is correct |
49 |
Correct |
183 ms |
4352 KB |
Output is correct |
50 |
Correct |
176 ms |
4352 KB |
Output is correct |
51 |
Correct |
188 ms |
4352 KB |
Output is correct |
52 |
Correct |
187 ms |
4344 KB |
Output is correct |
53 |
Correct |
416 ms |
4472 KB |
Output is correct |
54 |
Correct |
392 ms |
4344 KB |
Output is correct |
55 |
Correct |
397 ms |
4344 KB |
Output is correct |
56 |
Correct |
40 ms |
512 KB |
Output is correct |
57 |
Correct |
52 ms |
512 KB |
Output is correct |
58 |
Correct |
341 ms |
3576 KB |
Output is correct |
59 |
Correct |
354 ms |
3704 KB |
Output is correct |
60 |
Correct |
359 ms |
3576 KB |
Output is correct |
61 |
Correct |
326 ms |
3712 KB |
Output is correct |
62 |
Correct |
343 ms |
3704 KB |
Output is correct |
63 |
Correct |
347 ms |
3704 KB |
Output is correct |
64 |
Execution timed out |
1094 ms |
4304 KB |
Time limit exceeded |
65 |
Halted |
0 ms |
0 KB |
- |