/* be name khoda */
#define long_enable
#include <iostream>
#include <algorithm>
#include <cstring>
#include <numeric>
#include <vector>
#include <fstream>
#include <set>
#include <map>
using namespace std;
#ifdef long_enable
typedef long long int ll;
#else
typedef int ll;
#endif
typedef pair<ll, ll> pii;
typedef pair<pii, ll> ppi;
typedef pair<ll, pii> pip;
typedef vector<ll> vi;
typedef vector<pii> vpii;
#define forifrom(i, s, n) for (ll i = s; i < n; ++i)
#define forirto(i, n, e) for (ll i = (n) - 1; i >= e; --i)
#define fori(i, n) forifrom (i, 0, n)
#define forir(i, n) forirto (i, n, 0)
#define F first
#define S second
#define pb push_back
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define smin(a, b) a = min(a, (b))
#define smax(a, b) a = max(a, (b))
#define debug(x) cout << #x << " -> " << (x) << endl
#define debug2(x, y) cout << #x << ' ' << #y << " -> " << (x) << ' ' << (y) << endl
#define debug3(x, y, z) cout << #x << ' ' << #y << ' ' << #z << " -> " << (x) << ' ' << (y) << ' ' << (z) << endl
#define debug4(x, y, z, t) cout << #x << ' ' << #y << ' ' << #z << ' ' << #t << " -> " << (x) << ' ' << (y) << ' ' << (z) << ' ' << (t) << endl
#define debuga(x, n) cout << #x << " -> "; fori (i1_da, n) { cout << (x)[i1_da] << ' '; } cout << endl
#define debugaa(x, n, m) cout << #x << " ->\n"; fori (i1_daa, n) { fori (i2_daa, m) { cout << (x)[i1_daa][i2_daa] << ' '; } cout << '\n'; } cout << endl
const ll MOD = 1000000007;
const ll INF = 2000000000;
const long long BIG = 1446803456761533460LL;
#define XL (x << 1)
#define XR (XL | 1)
#define MD ((l + r) >> 1)
#define Add(a, b) a = ((a) + (b)) % MOD
#define Mul(a, b) a = (1LL * (a) * (b)) % MOD
// -----------------------------------------------------------------------
const ll maxn = 210;
const ll maxm = 50010;
ll n, m, d[6][maxn], par[maxn];
vpii g[maxn];
bool vis[maxn], crit[maxm];
struct Edge {
ll a, b, c, d;
} es[maxm];
void dij(ll s, ll *dd, bool reverse, bool marktree) {
fori (i, m) {
if (reverse) g[es[i].b].eb(es[i].a, i);
else g[es[i].a].eb(es[i].b, i);
}
memset(dd, 20, sizeof d[0]);
memset(vis, 0, sizeof vis);
dd[s] = 0;
par[s] = s;
fori (i, n) {
ll x = -1;
fori (j, n) if (!vis[j] && (x == -1 || dd[j] < dd[x])) x = j;
if (x != s && marktree) crit[par[x]] = true;
vis[x] = true;
for (auto [y, e] : g[x]) {
if (dd[x] + es[e].c < dd[y]) dd[y] = dd[x] + es[e].c, par[y] = e;
}
}
fori (i, n) g[i].clear();
}
int main() {
ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> n >> m;
fori (i, m) {
cin >> es[i].a >> es[i].b >> es[i].c >> es[i].d;
--es[i].a, --es[i].b;
}
dij(0, d[0], false, true);
dij(0, d[1], true, true);
dij(n - 1, d[2], false, true);
dij(n - 1, d[3], true, true);
ll ans = d[0][n - 1] + d[2][0];
fori (i, m) {
if (!crit[i]) {
ll a = d[0][es[i].b] + d[3][es[i].a];
ll b = d[2][es[i].b] + d[1][es[i].a];
smin(ans, a + b + es[i].c * 2 + es[i].d);
smin(ans, a + d[2][0] + es[i].c + es[i].d);
smin(ans, d[0][n - 1] + b + es[i].c + es[i].d);
} else {
swap(es[i].a, es[i].b);
dij(0, d[4], false, false);
dij(n - 1, d[5], false, false);
swap(es[i].a, es[i].b);
smin(ans, d[4][n - 1] + d[5][0] + es[i].d);
}
}
if (ans >= BIG) ans = -1;
cout << ans << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
90 ms |
504 KB |
Output is correct |
2 |
Correct |
16 ms |
384 KB |
Output is correct |
3 |
Correct |
100 ms |
512 KB |
Output is correct |
4 |
Correct |
103 ms |
512 KB |
Output is correct |
5 |
Correct |
3 ms |
384 KB |
Output is correct |
6 |
Correct |
25 ms |
440 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 |
512 KB |
Output is correct |
10 |
Correct |
51 ms |
504 KB |
Output is correct |
11 |
Correct |
66 ms |
504 KB |
Output is correct |
12 |
Correct |
61 ms |
504 KB |
Output is correct |
13 |
Correct |
56 ms |
512 KB |
Output is correct |
14 |
Correct |
88 ms |
512 KB |
Output is correct |
15 |
Correct |
84 ms |
512 KB |
Output is correct |
16 |
Correct |
84 ms |
512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
684 ms |
4152 KB |
Output is correct |
2 |
Correct |
714 ms |
4028 KB |
Output is correct |
3 |
Correct |
681 ms |
4856 KB |
Output is correct |
4 |
Correct |
106 ms |
512 KB |
Output is correct |
5 |
Correct |
81 ms |
504 KB |
Output is correct |
6 |
Correct |
36 ms |
504 KB |
Output is correct |
7 |
Correct |
7 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
448 ms |
4856 KB |
Output is correct |
10 |
Correct |
435 ms |
4984 KB |
Output is correct |
11 |
Correct |
630 ms |
4652 KB |
Output is correct |
12 |
Correct |
602 ms |
4740 KB |
Output is correct |
13 |
Correct |
600 ms |
4780 KB |
Output is correct |
14 |
Correct |
618 ms |
4608 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
94 ms |
512 KB |
Output is correct |
2 |
Correct |
30 ms |
384 KB |
Output is correct |
3 |
Correct |
836 ms |
3056 KB |
Output is correct |
4 |
Correct |
26 ms |
384 KB |
Output is correct |
5 |
Correct |
997 ms |
3968 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
638 ms |
4248 KB |
Output is correct |
9 |
Correct |
619 ms |
4404 KB |
Output is correct |
10 |
Correct |
807 ms |
4192 KB |
Output is correct |
11 |
Correct |
801 ms |
4216 KB |
Output is correct |
12 |
Correct |
806 ms |
4220 KB |
Output is correct |
13 |
Correct |
1 ms |
512 KB |
Output is correct |
14 |
Correct |
0 ms |
384 KB |
Output is correct |
15 |
Correct |
0 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
19 |
Correct |
808 ms |
4212 KB |
Output is correct |
20 |
Correct |
838 ms |
4216 KB |
Output is correct |
21 |
Correct |
874 ms |
4336 KB |
Output is correct |
22 |
Correct |
809 ms |
4344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
90 ms |
504 KB |
Output is correct |
2 |
Correct |
16 ms |
384 KB |
Output is correct |
3 |
Correct |
100 ms |
512 KB |
Output is correct |
4 |
Correct |
103 ms |
512 KB |
Output is correct |
5 |
Correct |
3 ms |
384 KB |
Output is correct |
6 |
Correct |
25 ms |
440 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 |
512 KB |
Output is correct |
10 |
Correct |
51 ms |
504 KB |
Output is correct |
11 |
Correct |
66 ms |
504 KB |
Output is correct |
12 |
Correct |
61 ms |
504 KB |
Output is correct |
13 |
Correct |
56 ms |
512 KB |
Output is correct |
14 |
Correct |
88 ms |
512 KB |
Output is correct |
15 |
Correct |
84 ms |
512 KB |
Output is correct |
16 |
Correct |
84 ms |
512 KB |
Output is correct |
17 |
Correct |
684 ms |
4152 KB |
Output is correct |
18 |
Correct |
714 ms |
4028 KB |
Output is correct |
19 |
Correct |
681 ms |
4856 KB |
Output is correct |
20 |
Correct |
106 ms |
512 KB |
Output is correct |
21 |
Correct |
81 ms |
504 KB |
Output is correct |
22 |
Correct |
36 ms |
504 KB |
Output is correct |
23 |
Correct |
7 ms |
384 KB |
Output is correct |
24 |
Correct |
0 ms |
384 KB |
Output is correct |
25 |
Correct |
448 ms |
4856 KB |
Output is correct |
26 |
Correct |
435 ms |
4984 KB |
Output is correct |
27 |
Correct |
630 ms |
4652 KB |
Output is correct |
28 |
Correct |
602 ms |
4740 KB |
Output is correct |
29 |
Correct |
600 ms |
4780 KB |
Output is correct |
30 |
Correct |
618 ms |
4608 KB |
Output is correct |
31 |
Correct |
94 ms |
512 KB |
Output is correct |
32 |
Correct |
30 ms |
384 KB |
Output is correct |
33 |
Correct |
836 ms |
3056 KB |
Output is correct |
34 |
Correct |
26 ms |
384 KB |
Output is correct |
35 |
Correct |
997 ms |
3968 KB |
Output is correct |
36 |
Correct |
1 ms |
384 KB |
Output is correct |
37 |
Correct |
0 ms |
384 KB |
Output is correct |
38 |
Correct |
638 ms |
4248 KB |
Output is correct |
39 |
Correct |
619 ms |
4404 KB |
Output is correct |
40 |
Correct |
807 ms |
4192 KB |
Output is correct |
41 |
Correct |
801 ms |
4216 KB |
Output is correct |
42 |
Correct |
806 ms |
4220 KB |
Output is correct |
43 |
Correct |
1 ms |
512 KB |
Output is correct |
44 |
Correct |
0 ms |
384 KB |
Output is correct |
45 |
Correct |
0 ms |
384 KB |
Output is correct |
46 |
Correct |
1 ms |
384 KB |
Output is correct |
47 |
Correct |
1 ms |
384 KB |
Output is correct |
48 |
Correct |
1 ms |
384 KB |
Output is correct |
49 |
Correct |
808 ms |
4212 KB |
Output is correct |
50 |
Correct |
838 ms |
4216 KB |
Output is correct |
51 |
Correct |
874 ms |
4336 KB |
Output is correct |
52 |
Correct |
809 ms |
4344 KB |
Output is correct |
53 |
Correct |
667 ms |
4460 KB |
Output is correct |
54 |
Correct |
693 ms |
4344 KB |
Output is correct |
55 |
Correct |
682 ms |
4424 KB |
Output is correct |
56 |
Correct |
103 ms |
512 KB |
Output is correct |
57 |
Correct |
109 ms |
512 KB |
Output is correct |
58 |
Correct |
251 ms |
3320 KB |
Output is correct |
59 |
Correct |
290 ms |
3328 KB |
Output is correct |
60 |
Correct |
404 ms |
3448 KB |
Output is correct |
61 |
Correct |
253 ms |
3448 KB |
Output is correct |
62 |
Correct |
303 ms |
3328 KB |
Output is correct |
63 |
Correct |
394 ms |
3328 KB |
Output is correct |
64 |
Correct |
307 ms |
3968 KB |
Output is correct |
65 |
Correct |
342 ms |
3968 KB |
Output is correct |
66 |
Correct |
464 ms |
4088 KB |
Output is correct |
67 |
Correct |
20 ms |
4600 KB |
Output is correct |
68 |
Correct |
439 ms |
4900 KB |
Output is correct |
69 |
Correct |
442 ms |
4956 KB |
Output is correct |
70 |
Correct |
622 ms |
4608 KB |
Output is correct |
71 |
Correct |
629 ms |
4600 KB |
Output is correct |
72 |
Correct |
594 ms |
4608 KB |
Output is correct |
73 |
Correct |
587 ms |
4600 KB |
Output is correct |
74 |
Correct |
605 ms |
4736 KB |
Output is correct |