#include <bits/stdc++.h>
using namespace std;
#define f first
#define s second
#define pb push_back
#define ar array
#define all(x) x.begin(), x.end()
#define siz(x) (int)x.size()
#define FOR(x, y, z) for(int x = (y); x < (z); x++)
#define ROF(x, z, y) for(int x = (y-1); x >= (z); x--)
#define F0R(x, z) FOR(x, 0, z)
#define R0F(x, z) ROF(x, 0, z)
#define trav(x, y) for(auto&x:y)
using ll = long long;
using vi = vector<int>;
using vl = vector<long long>;
using pii = pair<int, int>;
using vpii = vector<pair<int, int>>;
using pli = pair<ll, int>;
using vpli = vector<pli>;
template<class T> inline bool ckmin(T&a, T b) {return b < a ? a = b, 1 : 0;}
template<class T> inline bool ckmax(T&a, T b) {return b > a ? a = b, 1 : 0;}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const char nl = '\n';
const int mxN = 201;
const int mxM = 5e4 + 10;
const int MOD = 1e9 + 7;
const long long infLL = 1e18;
struct edge{
int v, i; ll c;
edge(int V, ll C, int I){
v = V;
c = C;
i = I;
}
};
int n, m;
int u[mxM], v[mxM]; ll c[mxM], d[mxM];
pii par[2][mxN][mxN]; ll dist[2][mxN][mxN];
int used[2][mxM];
vector<edge> adj[2][mxN];
ll ans = 0;
void go(int u, int gl){
priority_queue<pli, vpli, greater<pli>> pq;
fill(dist[gl][u], dist[gl][u] + n, infLL);
dist[gl][u][u]=0;
par[gl][u][u]={-1, -1};
pq.push({0, u});
while(!pq.empty()){
pii cur = pq.top();
pq.pop();
if(cur.f != dist[gl][u][cur.s]){
continue;
}
trav(nxt, adj[gl][cur.s]){
if(ckmin(dist[gl][u][nxt.v], cur.f + nxt.c)){
pq.push({dist[gl][u][nxt.v], nxt.v});
par[gl][u][nxt.v]= {nxt.i, cur.s};
}
}
}
}
void findPath(int gl){
if(dist[0][(gl?n-1:0)][(gl?0:n-1)] == infLL){
return;
}
int u = (gl ? n-1 : 0), v = (gl ? 0 : n - 1);
while(v != u){
used[gl][par[0][u][v].f]=1;
v=par[0][u][v].s;
}
}
ll go(int u, int v, int cant){
vector<ll> d(n, infLL);
d[u]=0;
priority_queue<pli, vpli, greater<pli>> pq;
pq.push({0, u});
while(!pq.empty()){
pli cur = pq.top();
pq.pop();
if(cur.f != d[cur.s]) continue;
trav(x, adj[0][cur.s]){
if(x.i != cant){
if(ckmin(d[x.v], cur.f + x.c)){
pq.push({d[x.v], x.v});
}
}
}
trav(x, adj[1][cur.s]){
if(x.i == cant){
if(ckmin(d[x.v], cur.f + x.c)){
pq.push({d[x.v], x.v});
}
}
}
}
//F0R(i, n){ cout << d[i] << ' ';
//}
//cout << nl;
return d[v];
}
int32_t main(){
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> n >> m;
F0R(i, m){
cin >> u[i] >> v[i] >> c[i] >> d[i]; u[i]--; v[i]--;
adj[0][u[i]].pb({v[i], c[i], i});
adj[1][v[i]].pb({u[i], c[i], i});
}
F0R(i, n){
go(i, 0);
//go(i, 1);
}
findPath(0); findPath(1);
ans=dist[0][0][n-1]+dist[0][n-1][0];
ll tmp[2] = {dist[0][0][n-1], dist[0][n-1][0]};
F0R(i, m){
ll res = d[i];
F0R(chk, 2){
if(used[chk][i]){
res += go(chk?n-1:0, chk?0:n-1, i);
}
else{
res += min(tmp[chk], dist[0][(chk?n-1:0)][v[i]]+dist[0][u[i]][chk?0:n-1]+c[i]);
}
}
ckmin(ans, res);
}
cout << (ans >= infLL ? -1 : ans) << nl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
1132 KB |
Output is correct |
2 |
Correct |
1 ms |
1004 KB |
Output is correct |
3 |
Correct |
8 ms |
1132 KB |
Output is correct |
4 |
Correct |
9 ms |
1132 KB |
Output is correct |
5 |
Correct |
1 ms |
620 KB |
Output is correct |
6 |
Correct |
1 ms |
1004 KB |
Output is correct |
7 |
Correct |
0 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
492 KB |
Output is correct |
10 |
Correct |
28 ms |
1132 KB |
Output is correct |
11 |
Correct |
27 ms |
1132 KB |
Output is correct |
12 |
Correct |
27 ms |
1252 KB |
Output is correct |
13 |
Correct |
5 ms |
1132 KB |
Output is correct |
14 |
Correct |
6 ms |
1132 KB |
Output is correct |
15 |
Correct |
6 ms |
1132 KB |
Output is correct |
16 |
Correct |
7 ms |
1132 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
54 ms |
4836 KB |
Output is correct |
2 |
Correct |
55 ms |
4856 KB |
Output is correct |
3 |
Correct |
57 ms |
4836 KB |
Output is correct |
4 |
Correct |
9 ms |
1132 KB |
Output is correct |
5 |
Correct |
5 ms |
1132 KB |
Output is correct |
6 |
Correct |
2 ms |
1004 KB |
Output is correct |
7 |
Correct |
1 ms |
1004 KB |
Output is correct |
8 |
Correct |
0 ms |
364 KB |
Output is correct |
9 |
Correct |
44 ms |
4708 KB |
Output is correct |
10 |
Correct |
44 ms |
4836 KB |
Output is correct |
11 |
Correct |
48 ms |
4840 KB |
Output is correct |
12 |
Correct |
48 ms |
4964 KB |
Output is correct |
13 |
Correct |
48 ms |
4836 KB |
Output is correct |
14 |
Correct |
48 ms |
5348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
1132 KB |
Output is correct |
2 |
Correct |
2 ms |
1004 KB |
Output is correct |
3 |
Correct |
30 ms |
3820 KB |
Output is correct |
4 |
Correct |
2 ms |
1004 KB |
Output is correct |
5 |
Correct |
36 ms |
4708 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
32 ms |
4856 KB |
Output is correct |
9 |
Correct |
32 ms |
4724 KB |
Output is correct |
10 |
Correct |
32 ms |
4708 KB |
Output is correct |
11 |
Correct |
32 ms |
4836 KB |
Output is correct |
12 |
Correct |
33 ms |
4836 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
512 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
33 ms |
4972 KB |
Output is correct |
20 |
Correct |
33 ms |
4844 KB |
Output is correct |
21 |
Correct |
33 ms |
4708 KB |
Output is correct |
22 |
Correct |
33 ms |
4856 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
1132 KB |
Output is correct |
2 |
Correct |
1 ms |
1004 KB |
Output is correct |
3 |
Correct |
8 ms |
1132 KB |
Output is correct |
4 |
Correct |
9 ms |
1132 KB |
Output is correct |
5 |
Correct |
1 ms |
620 KB |
Output is correct |
6 |
Correct |
1 ms |
1004 KB |
Output is correct |
7 |
Correct |
0 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
492 KB |
Output is correct |
10 |
Correct |
28 ms |
1132 KB |
Output is correct |
11 |
Correct |
27 ms |
1132 KB |
Output is correct |
12 |
Correct |
27 ms |
1252 KB |
Output is correct |
13 |
Correct |
5 ms |
1132 KB |
Output is correct |
14 |
Correct |
6 ms |
1132 KB |
Output is correct |
15 |
Correct |
6 ms |
1132 KB |
Output is correct |
16 |
Correct |
7 ms |
1132 KB |
Output is correct |
17 |
Correct |
54 ms |
4836 KB |
Output is correct |
18 |
Correct |
55 ms |
4856 KB |
Output is correct |
19 |
Correct |
57 ms |
4836 KB |
Output is correct |
20 |
Correct |
9 ms |
1132 KB |
Output is correct |
21 |
Correct |
5 ms |
1132 KB |
Output is correct |
22 |
Correct |
2 ms |
1004 KB |
Output is correct |
23 |
Correct |
1 ms |
1004 KB |
Output is correct |
24 |
Correct |
0 ms |
364 KB |
Output is correct |
25 |
Correct |
44 ms |
4708 KB |
Output is correct |
26 |
Correct |
44 ms |
4836 KB |
Output is correct |
27 |
Correct |
48 ms |
4840 KB |
Output is correct |
28 |
Correct |
48 ms |
4964 KB |
Output is correct |
29 |
Correct |
48 ms |
4836 KB |
Output is correct |
30 |
Correct |
48 ms |
5348 KB |
Output is correct |
31 |
Correct |
6 ms |
1132 KB |
Output is correct |
32 |
Correct |
2 ms |
1004 KB |
Output is correct |
33 |
Correct |
30 ms |
3820 KB |
Output is correct |
34 |
Correct |
2 ms |
1004 KB |
Output is correct |
35 |
Correct |
36 ms |
4708 KB |
Output is correct |
36 |
Correct |
1 ms |
364 KB |
Output is correct |
37 |
Correct |
1 ms |
364 KB |
Output is correct |
38 |
Correct |
32 ms |
4856 KB |
Output is correct |
39 |
Correct |
32 ms |
4724 KB |
Output is correct |
40 |
Correct |
32 ms |
4708 KB |
Output is correct |
41 |
Correct |
32 ms |
4836 KB |
Output is correct |
42 |
Correct |
33 ms |
4836 KB |
Output is correct |
43 |
Correct |
1 ms |
364 KB |
Output is correct |
44 |
Correct |
1 ms |
364 KB |
Output is correct |
45 |
Correct |
1 ms |
512 KB |
Output is correct |
46 |
Correct |
1 ms |
364 KB |
Output is correct |
47 |
Correct |
1 ms |
364 KB |
Output is correct |
48 |
Correct |
1 ms |
364 KB |
Output is correct |
49 |
Correct |
33 ms |
4972 KB |
Output is correct |
50 |
Correct |
33 ms |
4844 KB |
Output is correct |
51 |
Correct |
33 ms |
4708 KB |
Output is correct |
52 |
Correct |
33 ms |
4856 KB |
Output is correct |
53 |
Correct |
59 ms |
4708 KB |
Output is correct |
54 |
Correct |
60 ms |
4708 KB |
Output is correct |
55 |
Correct |
62 ms |
4708 KB |
Output is correct |
56 |
Correct |
8 ms |
1132 KB |
Output is correct |
57 |
Correct |
8 ms |
1132 KB |
Output is correct |
58 |
Correct |
131 ms |
4168 KB |
Output is correct |
59 |
Correct |
141 ms |
4068 KB |
Output is correct |
60 |
Correct |
140 ms |
4068 KB |
Output is correct |
61 |
Correct |
115 ms |
4068 KB |
Output is correct |
62 |
Correct |
134 ms |
4144 KB |
Output is correct |
63 |
Correct |
138 ms |
4100 KB |
Output is correct |
64 |
Correct |
920 ms |
4964 KB |
Output is correct |
65 |
Correct |
837 ms |
5940 KB |
Output is correct |
66 |
Correct |
789 ms |
5824 KB |
Output is correct |
67 |
Correct |
18 ms |
3808 KB |
Output is correct |
68 |
Correct |
48 ms |
5860 KB |
Output is correct |
69 |
Correct |
48 ms |
5992 KB |
Output is correct |
70 |
Correct |
51 ms |
5988 KB |
Output is correct |
71 |
Correct |
52 ms |
5860 KB |
Output is correct |
72 |
Correct |
52 ms |
6020 KB |
Output is correct |
73 |
Correct |
51 ms |
5988 KB |
Output is correct |
74 |
Correct |
52 ms |
5988 KB |
Output is correct |