/*
IN THE NAME OF GOD
*/
#include <bits/stdc++.h>
// #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
// #pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef long double ld;
#define F first
#define S second
#define Mp make_pair
#define pb push_back
#define pf push_front
#define size(x) ((ll)x.size())
#define all(x) (x).begin(),(x).end()
#define kill(x) cout << x << '\n', exit(0);
#define fuck(x) cout << "(" << #x << " , " << x << ")" << endl
#define endl '\n'
const int N = 3e5+23, lg = 18;
ll Mod = 1e9+7; //998244353;
inline ll MOD(ll a, ll mod=Mod) {a%=mod; (a<0)&&(a+=mod); return a;}
inline ll poww(ll a, ll b, ll mod=Mod) {
ll ans = 1;
a=MOD(a, mod);
while (b) {
if (b & 1) ans = MOD(ans*a, mod);
b >>= 1;
a = MOD(a*a, mod);
}
return ans;
}
int n, m, mark[N];
ll ev[N], eu[N], dist[N], c[N], d[N], f[205][205];
vector<int> adj[N];
ll solve(int rt, int e) {
fill(dist, dist+n+2, 1e18);
fill(mark, mark+n+2, 0);
priority_queue<pll, vector<pll>,greater<pll>> pq;
pq.push({0, rt});
while(size(pq) > 0) {
pll v = pq.top(); pq.pop();
if(mark[v.S]) continue;
mark[v.S]=1, dist[v.S] = v.F;
for(int u : adj[v.S]) {
if(u!=e&&mark[eu[u]]==0) pq.push({v.F+c[u], eu[u]});
}
if(v.S == eu[e]) pq.push({v.F+c[e], ev[e]});
}
if(rt == 1) return dist[n];
return dist[1];
}
int main () {
ios_base::sync_with_stdio(false), cin.tie(0);
cin>>n>>m;
for(int i=1; i<=n; i++) {
for(int j=1; j<=n; j++) if(i!=j) f[i][j] = 1e18;
}
for(int i=1; i<=m; i++) {
cin>>ev[i]>>eu[i]>>c[i]>>d[i];
adj[ev[i]].pb(i);
f[ev[i]][eu[i]] = min(c[i], f[ev[i]][eu[i]]);
}
for(int i=1; i<=n; i++) {
for(int j=1; j<=n; j++) {
for(int k=1; k<=n; k++) {
f[j][k] = min(f[j][i]+f[i][k], f[j][k]);
}
}
}
ll ans = f[1][n] + f[n][1];
for(int i=1; i<=m; i++) {
if(min(f[1][n],f[1][eu[i]]+f[ev[i]][n]+c[i])+min(f[n][1],f[n][eu[i]]+f[ev[i]][1]+c[i])+d[i] < ans) {
ans = min(ans, solve(1,i)+solve(n,i)+d[i]);
}
}
cout<<(ans>=1e11 ? -1 : ans)<<endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
14940 KB |
Output is correct |
2 |
Correct |
10 ms |
16988 KB |
Output is correct |
3 |
Correct |
11 ms |
15192 KB |
Output is correct |
4 |
Correct |
10 ms |
14936 KB |
Output is correct |
5 |
Correct |
3 ms |
16984 KB |
Output is correct |
6 |
Correct |
10 ms |
16988 KB |
Output is correct |
7 |
Correct |
3 ms |
16984 KB |
Output is correct |
8 |
Correct |
3 ms |
16988 KB |
Output is correct |
9 |
Correct |
4 ms |
16988 KB |
Output is correct |
10 |
Correct |
15 ms |
17000 KB |
Output is correct |
11 |
Correct |
25 ms |
17084 KB |
Output is correct |
12 |
Correct |
11 ms |
16988 KB |
Output is correct |
13 |
Correct |
11 ms |
16988 KB |
Output is correct |
14 |
Correct |
12 ms |
16988 KB |
Output is correct |
15 |
Correct |
11 ms |
16988 KB |
Output is correct |
16 |
Correct |
10 ms |
16984 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
17244 KB |
Output is correct |
2 |
Correct |
33 ms |
20324 KB |
Output is correct |
3 |
Correct |
22 ms |
17428 KB |
Output is correct |
4 |
Correct |
11 ms |
16988 KB |
Output is correct |
5 |
Correct |
11 ms |
16988 KB |
Output is correct |
6 |
Correct |
10 ms |
17052 KB |
Output is correct |
7 |
Correct |
10 ms |
14940 KB |
Output is correct |
8 |
Correct |
4 ms |
16988 KB |
Output is correct |
9 |
Correct |
69 ms |
21364 KB |
Output is correct |
10 |
Correct |
49 ms |
21052 KB |
Output is correct |
11 |
Correct |
34 ms |
21528 KB |
Output is correct |
12 |
Correct |
35 ms |
21364 KB |
Output is correct |
13 |
Correct |
30 ms |
21380 KB |
Output is correct |
14 |
Correct |
30 ms |
21404 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
14936 KB |
Output is correct |
2 |
Correct |
10 ms |
14940 KB |
Output is correct |
3 |
Correct |
20 ms |
17308 KB |
Output is correct |
4 |
Correct |
10 ms |
16988 KB |
Output is correct |
5 |
Correct |
21 ms |
18268 KB |
Output is correct |
6 |
Correct |
3 ms |
16984 KB |
Output is correct |
7 |
Correct |
3 ms |
16988 KB |
Output is correct |
8 |
Correct |
71 ms |
20848 KB |
Output is correct |
9 |
Correct |
81 ms |
20808 KB |
Output is correct |
10 |
Correct |
25 ms |
20716 KB |
Output is correct |
11 |
Correct |
25 ms |
20632 KB |
Output is correct |
12 |
Correct |
27 ms |
20604 KB |
Output is correct |
13 |
Correct |
3 ms |
16988 KB |
Output is correct |
14 |
Correct |
3 ms |
16988 KB |
Output is correct |
15 |
Correct |
3 ms |
16984 KB |
Output is correct |
16 |
Correct |
3 ms |
16984 KB |
Output is correct |
17 |
Correct |
3 ms |
16984 KB |
Output is correct |
18 |
Correct |
3 ms |
16988 KB |
Output is correct |
19 |
Correct |
26 ms |
20648 KB |
Output is correct |
20 |
Correct |
31 ms |
20760 KB |
Output is correct |
21 |
Correct |
28 ms |
20640 KB |
Output is correct |
22 |
Correct |
26 ms |
20744 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
14940 KB |
Output is correct |
2 |
Correct |
10 ms |
16988 KB |
Output is correct |
3 |
Correct |
11 ms |
15192 KB |
Output is correct |
4 |
Correct |
10 ms |
14936 KB |
Output is correct |
5 |
Correct |
3 ms |
16984 KB |
Output is correct |
6 |
Correct |
10 ms |
16988 KB |
Output is correct |
7 |
Correct |
3 ms |
16984 KB |
Output is correct |
8 |
Correct |
3 ms |
16988 KB |
Output is correct |
9 |
Correct |
4 ms |
16988 KB |
Output is correct |
10 |
Correct |
15 ms |
17000 KB |
Output is correct |
11 |
Correct |
25 ms |
17084 KB |
Output is correct |
12 |
Correct |
11 ms |
16988 KB |
Output is correct |
13 |
Correct |
11 ms |
16988 KB |
Output is correct |
14 |
Correct |
12 ms |
16988 KB |
Output is correct |
15 |
Correct |
11 ms |
16988 KB |
Output is correct |
16 |
Correct |
10 ms |
16984 KB |
Output is correct |
17 |
Correct |
21 ms |
17244 KB |
Output is correct |
18 |
Correct |
33 ms |
20324 KB |
Output is correct |
19 |
Correct |
22 ms |
17428 KB |
Output is correct |
20 |
Correct |
11 ms |
16988 KB |
Output is correct |
21 |
Correct |
11 ms |
16988 KB |
Output is correct |
22 |
Correct |
10 ms |
17052 KB |
Output is correct |
23 |
Correct |
10 ms |
14940 KB |
Output is correct |
24 |
Correct |
4 ms |
16988 KB |
Output is correct |
25 |
Correct |
69 ms |
21364 KB |
Output is correct |
26 |
Correct |
49 ms |
21052 KB |
Output is correct |
27 |
Correct |
34 ms |
21528 KB |
Output is correct |
28 |
Correct |
35 ms |
21364 KB |
Output is correct |
29 |
Correct |
30 ms |
21380 KB |
Output is correct |
30 |
Correct |
30 ms |
21404 KB |
Output is correct |
31 |
Correct |
10 ms |
14936 KB |
Output is correct |
32 |
Correct |
10 ms |
14940 KB |
Output is correct |
33 |
Correct |
20 ms |
17308 KB |
Output is correct |
34 |
Correct |
10 ms |
16988 KB |
Output is correct |
35 |
Correct |
21 ms |
18268 KB |
Output is correct |
36 |
Correct |
3 ms |
16984 KB |
Output is correct |
37 |
Correct |
3 ms |
16988 KB |
Output is correct |
38 |
Correct |
71 ms |
20848 KB |
Output is correct |
39 |
Correct |
81 ms |
20808 KB |
Output is correct |
40 |
Correct |
25 ms |
20716 KB |
Output is correct |
41 |
Correct |
25 ms |
20632 KB |
Output is correct |
42 |
Correct |
27 ms |
20604 KB |
Output is correct |
43 |
Correct |
3 ms |
16988 KB |
Output is correct |
44 |
Correct |
3 ms |
16988 KB |
Output is correct |
45 |
Correct |
3 ms |
16984 KB |
Output is correct |
46 |
Correct |
3 ms |
16984 KB |
Output is correct |
47 |
Correct |
3 ms |
16984 KB |
Output is correct |
48 |
Correct |
3 ms |
16988 KB |
Output is correct |
49 |
Correct |
26 ms |
20648 KB |
Output is correct |
50 |
Correct |
31 ms |
20760 KB |
Output is correct |
51 |
Correct |
28 ms |
20640 KB |
Output is correct |
52 |
Correct |
26 ms |
20744 KB |
Output is correct |
53 |
Correct |
22 ms |
18268 KB |
Output is correct |
54 |
Correct |
22 ms |
18268 KB |
Output is correct |
55 |
Correct |
21 ms |
18272 KB |
Output is correct |
56 |
Correct |
10 ms |
14940 KB |
Output is correct |
57 |
Correct |
10 ms |
15004 KB |
Output is correct |
58 |
Correct |
43 ms |
20944 KB |
Output is correct |
59 |
Correct |
44 ms |
20960 KB |
Output is correct |
60 |
Correct |
54 ms |
20680 KB |
Output is correct |
61 |
Correct |
53 ms |
20916 KB |
Output is correct |
62 |
Correct |
388 ms |
20960 KB |
Output is correct |
63 |
Correct |
908 ms |
20972 KB |
Output is correct |
64 |
Correct |
121 ms |
21152 KB |
Output is correct |
65 |
Correct |
294 ms |
21176 KB |
Output is correct |
66 |
Correct |
560 ms |
21260 KB |
Output is correct |
67 |
Correct |
67 ms |
21732 KB |
Output is correct |
68 |
Correct |
59 ms |
21056 KB |
Output is correct |
69 |
Correct |
55 ms |
21080 KB |
Output is correct |
70 |
Correct |
28 ms |
21260 KB |
Output is correct |
71 |
Correct |
34 ms |
21364 KB |
Output is correct |
72 |
Correct |
29 ms |
21196 KB |
Output is correct |
73 |
Correct |
29 ms |
21400 KB |
Output is correct |
74 |
Correct |
35 ms |
21340 KB |
Output is correct |