#include <iostream>
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const ll maxn = 201, INF = 4e18+9;
struct edge{
ll u, v, c, d, id;
};
vector<ll> dijkstra(int s, vector<vector<edge>> &adj, edge e){
int ban = e.id;
adj[e.v].push_back({e.v, e.u, e.c, e.d, -1});
int n = adj.size();
vector<ll> dist(n+1, INF);
dist[s] = 0;
vector<bool> vis(n+1, false);
priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>> pq;
pq.push({0, s});
while(!pq.empty()){
ll d_u = pq.top().first;
int u = pq.top().second;
pq.pop();
if(vis[u]){
continue;
}
vis[u] = true;
for(auto it : adj[u]){
int v = it.v, id = it.id;
ll w = it.c;
if(vis[v] || ban == id) continue;
if(dist[v] > d_u+w){
dist[v] = d_u+w;
pq.push({dist[v], v});
}
}
}
adj[e.v].pop_back();
return dist;
}
vector<ll> dijkstra_trace(int s, int t, int m, vector<vector<edge>> &adj){
vector<ll> res(m+1, 0);
int n = adj.size();
vector<ll> dist(n+1, INF);
vector<pair<int, int>> pa(n+1);
dist[s] = 0;
vector<bool> vis(n+1, false);
priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>> pq;
pq.push({0, s});
while(!pq.empty()){
ll d_u = pq.top().first;
int u = pq.top().second;
pq.pop();
if(vis[u]){
continue;
}
vis[u] = true;
for(auto it : adj[u]){
int v = it.v;
ll w = it.c;
if(vis[v]) continue;
if(dist[v] > d_u+w){
dist[v] = d_u+w;
pa[v] = {u, it.id};
pq.push({dist[v], v});
}
}
}
if(dist[t] == INF) return res;
while(t != s){
res[pa[t].second] = 1;
t = pa[t].first;
}
return res;
}
void solve(){
int n, m;
cin >> n >> m;
vector<vector<edge>> adj(n+1), adj_rev(n+1);
vector<edge> e(1);
for(int i = 1; i <= m; i++){
int u, v;
ll c, d;
cin >> u >> v >> c >> d;
e.push_back({u, v, c, d, i});
adj[u].push_back({u, v, c, d, i});
adj_rev[v].push_back({v, u, c, d, i});
}
edge null = {0, 0, INF, INF, -1};
vector<ll> dist_1f = dijkstra(1, adj, null),
dist_1b = dijkstra(n, adj_rev, null),
dist_nf = dijkstra(n, adj, null),
dist_nb = dijkstra(1, adj_rev, null);
vector<ll> bamboo_1 = dijkstra_trace(1, n, m, adj),
bamboo_n = dijkstra_trace(n, 1, m, adj);
auto solvefor = [&](int s, int t, vector<ll> dist_f, vector<ll> dist_b, vector<ll> bamboo) -> vector<ll>{
vector<ll> res(m+1);
for(int i = 1; i <= m; i++){
if(bamboo[i]){
vector<ll> a = dijkstra(s, adj, e[i]);
res[i] = a[t];
}else{
res[i] = min(dist_f[t], dist_f[e[i].v]+e[i].c+dist_b[e[i].u]);
}
}
return res;
};
vector<ll> res_1 = solvefor(1, n, dist_1f, dist_1b, bamboo_1),
res_n = solvefor(n, 1, dist_nf, dist_nb, bamboo_n);
ll ans = dist_1f[n] + dist_nf[1];
for(int i = 1; i <= m; i++){
ans = min(ans, res_1[i] + res_n[i] + e[i].d);
}
if(ans >= INF) ans = -1;
cout << ans;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
solve();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
600 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
456 KB |
Output is correct |
9 |
Correct |
1 ms |
600 KB |
Output is correct |
10 |
Correct |
10 ms |
604 KB |
Output is correct |
11 |
Correct |
10 ms |
704 KB |
Output is correct |
12 |
Correct |
10 ms |
700 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
10988 KB |
Output is correct |
2 |
Correct |
21 ms |
11488 KB |
Output is correct |
3 |
Correct |
25 ms |
11488 KB |
Output is correct |
4 |
Correct |
1 ms |
860 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
464 KB |
Output is correct |
9 |
Correct |
20 ms |
11560 KB |
Output is correct |
10 |
Correct |
22 ms |
11488 KB |
Output is correct |
11 |
Correct |
21 ms |
11244 KB |
Output is correct |
12 |
Correct |
23 ms |
11584 KB |
Output is correct |
13 |
Correct |
23 ms |
11500 KB |
Output is correct |
14 |
Correct |
23 ms |
12048 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
15 ms |
8588 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
20 ms |
10932 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
19 ms |
10972 KB |
Output is correct |
9 |
Correct |
17 ms |
10984 KB |
Output is correct |
10 |
Correct |
17 ms |
10980 KB |
Output is correct |
11 |
Correct |
18 ms |
11244 KB |
Output is correct |
12 |
Correct |
19 ms |
11496 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
344 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
18 ms |
11444 KB |
Output is correct |
20 |
Correct |
20 ms |
11240 KB |
Output is correct |
21 |
Correct |
19 ms |
10988 KB |
Output is correct |
22 |
Correct |
20 ms |
10984 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
600 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
456 KB |
Output is correct |
9 |
Correct |
1 ms |
600 KB |
Output is correct |
10 |
Correct |
10 ms |
604 KB |
Output is correct |
11 |
Correct |
10 ms |
704 KB |
Output is correct |
12 |
Correct |
10 ms |
700 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
1 ms |
604 KB |
Output is correct |
17 |
Correct |
19 ms |
10988 KB |
Output is correct |
18 |
Correct |
21 ms |
11488 KB |
Output is correct |
19 |
Correct |
25 ms |
11488 KB |
Output is correct |
20 |
Correct |
1 ms |
860 KB |
Output is correct |
21 |
Correct |
1 ms |
604 KB |
Output is correct |
22 |
Correct |
1 ms |
604 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
464 KB |
Output is correct |
25 |
Correct |
20 ms |
11560 KB |
Output is correct |
26 |
Correct |
22 ms |
11488 KB |
Output is correct |
27 |
Correct |
21 ms |
11244 KB |
Output is correct |
28 |
Correct |
23 ms |
11584 KB |
Output is correct |
29 |
Correct |
23 ms |
11500 KB |
Output is correct |
30 |
Correct |
23 ms |
12048 KB |
Output is correct |
31 |
Correct |
1 ms |
604 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
15 ms |
8588 KB |
Output is correct |
34 |
Correct |
1 ms |
348 KB |
Output is correct |
35 |
Correct |
20 ms |
10932 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
19 ms |
10972 KB |
Output is correct |
39 |
Correct |
17 ms |
10984 KB |
Output is correct |
40 |
Correct |
17 ms |
10980 KB |
Output is correct |
41 |
Correct |
18 ms |
11244 KB |
Output is correct |
42 |
Correct |
19 ms |
11496 KB |
Output is correct |
43 |
Correct |
0 ms |
348 KB |
Output is correct |
44 |
Correct |
0 ms |
348 KB |
Output is correct |
45 |
Correct |
0 ms |
348 KB |
Output is correct |
46 |
Correct |
1 ms |
348 KB |
Output is correct |
47 |
Correct |
0 ms |
344 KB |
Output is correct |
48 |
Correct |
0 ms |
348 KB |
Output is correct |
49 |
Correct |
18 ms |
11444 KB |
Output is correct |
50 |
Correct |
20 ms |
11240 KB |
Output is correct |
51 |
Correct |
19 ms |
10988 KB |
Output is correct |
52 |
Correct |
20 ms |
10984 KB |
Output is correct |
53 |
Correct |
22 ms |
10732 KB |
Output is correct |
54 |
Correct |
22 ms |
11240 KB |
Output is correct |
55 |
Correct |
26 ms |
10988 KB |
Output is correct |
56 |
Correct |
1 ms |
468 KB |
Output is correct |
57 |
Correct |
1 ms |
604 KB |
Output is correct |
58 |
Correct |
100 ms |
8824 KB |
Output is correct |
59 |
Correct |
105 ms |
8820 KB |
Output is correct |
60 |
Correct |
111 ms |
8668 KB |
Output is correct |
61 |
Correct |
83 ms |
8672 KB |
Output is correct |
62 |
Correct |
101 ms |
8672 KB |
Output is correct |
63 |
Correct |
117 ms |
8668 KB |
Output is correct |
64 |
Correct |
503 ms |
9196 KB |
Output is correct |
65 |
Correct |
466 ms |
9184 KB |
Output is correct |
66 |
Correct |
434 ms |
9184 KB |
Output is correct |
67 |
Correct |
15 ms |
7496 KB |
Output is correct |
68 |
Correct |
18 ms |
11240 KB |
Output is correct |
69 |
Correct |
25 ms |
11468 KB |
Output is correct |
70 |
Correct |
24 ms |
11232 KB |
Output is correct |
71 |
Correct |
22 ms |
11324 KB |
Output is correct |
72 |
Correct |
20 ms |
11232 KB |
Output is correct |
73 |
Correct |
20 ms |
11500 KB |
Output is correct |
74 |
Correct |
24 ms |
11492 KB |
Output is correct |