#include <bits/stdc++.h>
#include <bits/extc++.h>
#define StarBurstStream ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define iter(a) a.begin(), a.end()
#define riter(a) a.rbegin(), a.rend()
#define lsort(a) sort(iter(a))
#define gsort(a) sort(riter(a))
#define pb(a) push_back(a)
#define eb(a) emplace_back(a)
#define pf(a) push_front(a)
#define ef(a) emplace_front(a)
#define pob pop_back()
#define pof pop_front()
#define mp(a, b) make_pair(a, b)
#define F first
#define S second
#define mt make_tuple
#define gt(t, i) get<i>(t)
#define tomax(a, b) ((a) = max((a), (b)))
#define tomin(a, b) ((a) = min((a), (b)))
#define topos(a) ((a) = (((a) % MOD + MOD) % MOD))
#define uni(a) a.resize(unique(iter(a)) - a.begin())
#define printv(a, b) {bool pvaspace=false; \
for(auto pva : a){ \
if(pvaspace) b << " "; pvaspace=true;\
b << pva;\
}\
b << "\n";}
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pdd = pair<ld, ld>;
using tiii = tuple<int, int, int>;
const ll MOD = 1000000007;
const ll MAX = 1LL << 60;
template<typename A, typename B>
ostream& operator<<(ostream& o, pair<A, B> p){
return o << '(' << p.F << ',' << p.S << ')';
}
ll ifloor(ll a, ll b){
if(b < 0) a *= -1, b *= -1;
if(a < 0) return (a - b + 1) / b;
else return a / b;
}
ll iceil(ll a, ll b){
if(b < 0) a *= -1, b *= -1;
if(a > 0) return (a + b - 1) / b;
else return a / b;
}
struct edge{
int from, to, c, d, id;
};
int main(){
StarBurstStream
int n, m;
cin >> n >> m;
vector<vector<edge>> g(n + 1);
vector<vector<ll>> dis(n + 1, vector<ll>(n + 1, MAX));
vector<edge> e(m + 1);
for(int i = 1; i <= n; i++) dis[i][i] = 0;
for(int i = 1; i <= m; i++){
int u, v, c, d;
cin >> u >> v >> c >> d;
g[u].eb(e[i] = edge({u, v, c, d, i}));
dis[u][v] = min(dis[u][v], (ll)c);
}
for(int k = 1; k <= n; k++){
for(int i = 1; i <= n; i++){
for(int j = 1; j <= n; j++){
dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);
}
}
}
vector<bool> tr(m + 1);
auto eliminate = [&](int s){
vector<ll> d(n + 1, MAX);
vector<int> lst(n + 1, -1);
d[s] = 0;
std::priority_queue<pll, vector<pll>, greater<>> pq;
pq.push(mp(0, s));
while(!pq.empty()){
int now = pq.top().S;
ll nd = pq.top().F;
pq.pop();
if(nd != d[now]) continue;
if(lst[now] != -1) tr[lst[now]] = true;
for(auto i : g[now]){
if(nd + i.c >= d[i.to]) continue;
d[i.to] = nd + i.c;
pq.push(mp(nd + i.c, i.to));
lst[i.to] = i.id;
}
}
};
eliminate(1);
eliminate(n);
auto calc = [&](int s, int t, int no){
vector<ll> d(n + 1, MAX);
d[s] = 0;
std::priority_queue<pll, vector<pll>, greater<>> pq;
pq.push(mp(0, s));
while(!pq.empty()){
int now = pq.top().S;
ll nd = pq.top().F;
pq.pop();
if(nd != d[now]) continue;
for(auto i : g[now]){
if(i.id == no) continue;
if(nd + i.c >= d[i.to]) continue;
d[i.to] = nd + i.c;
pq.push(mp(nd + i.c, i.to));
}
}
return d[t];
};
ll ans = dis[1][n] + dis[n][1];
for(int i = 1; i <= m; i++){
if(tr[i]){
g[e[i].to].eb(edge({e[i].to, e[i].from, e[i].c, e[i].d, -1}));
ans = min(ans, calc(1, n, i) + calc(n, 1, i) + e[i].d);
g[e[i].to].pob;
}
else{
ans = min(ans,
min(dis[1][n], dis[1][e[i].to] + dis[e[i].from][n] + e[i].c) +
min(dis[n][1], dis[n][e[i].to] + dis[e[i].from][1] + e[i].c) +
e[i].d);
//cerr << "test " << i << " " << ans << "\n";
}
}
if(ans >= MAX) ans = -1;
cout << ans << "\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
700 KB |
Output is correct |
2 |
Correct |
7 ms |
576 KB |
Output is correct |
3 |
Correct |
22 ms |
712 KB |
Output is correct |
4 |
Correct |
23 ms |
716 KB |
Output is correct |
5 |
Correct |
2 ms |
324 KB |
Output is correct |
6 |
Correct |
9 ms |
576 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
324 KB |
Output is correct |
10 |
Correct |
24 ms |
716 KB |
Output is correct |
11 |
Correct |
29 ms |
716 KB |
Output is correct |
12 |
Correct |
26 ms |
732 KB |
Output is correct |
13 |
Correct |
11 ms |
712 KB |
Output is correct |
14 |
Correct |
16 ms |
716 KB |
Output is correct |
15 |
Correct |
23 ms |
708 KB |
Output is correct |
16 |
Correct |
22 ms |
704 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
114 ms |
4156 KB |
Output is correct |
2 |
Correct |
109 ms |
4268 KB |
Output is correct |
3 |
Correct |
111 ms |
4284 KB |
Output is correct |
4 |
Correct |
21 ms |
780 KB |
Output is correct |
5 |
Correct |
14 ms |
716 KB |
Output is correct |
6 |
Correct |
9 ms |
564 KB |
Output is correct |
7 |
Correct |
9 ms |
668 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
45 ms |
4348 KB |
Output is correct |
10 |
Correct |
42 ms |
4368 KB |
Output is correct |
11 |
Correct |
99 ms |
4348 KB |
Output is correct |
12 |
Correct |
94 ms |
4444 KB |
Output is correct |
13 |
Correct |
75 ms |
4344 KB |
Output is correct |
14 |
Correct |
64 ms |
4508 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
716 KB |
Output is correct |
2 |
Correct |
9 ms |
588 KB |
Output is correct |
3 |
Correct |
67 ms |
3140 KB |
Output is correct |
4 |
Correct |
10 ms |
640 KB |
Output is correct |
5 |
Correct |
93 ms |
4056 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
35 ms |
4060 KB |
Output is correct |
9 |
Correct |
39 ms |
4092 KB |
Output is correct |
10 |
Correct |
61 ms |
4212 KB |
Output is correct |
11 |
Correct |
63 ms |
4084 KB |
Output is correct |
12 |
Correct |
88 ms |
4152 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
0 ms |
204 KB |
Output is correct |
17 |
Correct |
0 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
67 ms |
4236 KB |
Output is correct |
20 |
Correct |
63 ms |
4064 KB |
Output is correct |
21 |
Correct |
62 ms |
4140 KB |
Output is correct |
22 |
Correct |
59 ms |
3976 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
700 KB |
Output is correct |
2 |
Correct |
7 ms |
576 KB |
Output is correct |
3 |
Correct |
22 ms |
712 KB |
Output is correct |
4 |
Correct |
23 ms |
716 KB |
Output is correct |
5 |
Correct |
2 ms |
324 KB |
Output is correct |
6 |
Correct |
9 ms |
576 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
324 KB |
Output is correct |
10 |
Correct |
24 ms |
716 KB |
Output is correct |
11 |
Correct |
29 ms |
716 KB |
Output is correct |
12 |
Correct |
26 ms |
732 KB |
Output is correct |
13 |
Correct |
11 ms |
712 KB |
Output is correct |
14 |
Correct |
16 ms |
716 KB |
Output is correct |
15 |
Correct |
23 ms |
708 KB |
Output is correct |
16 |
Correct |
22 ms |
704 KB |
Output is correct |
17 |
Correct |
114 ms |
4156 KB |
Output is correct |
18 |
Correct |
109 ms |
4268 KB |
Output is correct |
19 |
Correct |
111 ms |
4284 KB |
Output is correct |
20 |
Correct |
21 ms |
780 KB |
Output is correct |
21 |
Correct |
14 ms |
716 KB |
Output is correct |
22 |
Correct |
9 ms |
564 KB |
Output is correct |
23 |
Correct |
9 ms |
668 KB |
Output is correct |
24 |
Correct |
1 ms |
204 KB |
Output is correct |
25 |
Correct |
45 ms |
4348 KB |
Output is correct |
26 |
Correct |
42 ms |
4368 KB |
Output is correct |
27 |
Correct |
99 ms |
4348 KB |
Output is correct |
28 |
Correct |
94 ms |
4444 KB |
Output is correct |
29 |
Correct |
75 ms |
4344 KB |
Output is correct |
30 |
Correct |
64 ms |
4508 KB |
Output is correct |
31 |
Correct |
16 ms |
716 KB |
Output is correct |
32 |
Correct |
9 ms |
588 KB |
Output is correct |
33 |
Correct |
67 ms |
3140 KB |
Output is correct |
34 |
Correct |
10 ms |
640 KB |
Output is correct |
35 |
Correct |
93 ms |
4056 KB |
Output is correct |
36 |
Correct |
1 ms |
204 KB |
Output is correct |
37 |
Correct |
1 ms |
204 KB |
Output is correct |
38 |
Correct |
35 ms |
4060 KB |
Output is correct |
39 |
Correct |
39 ms |
4092 KB |
Output is correct |
40 |
Correct |
61 ms |
4212 KB |
Output is correct |
41 |
Correct |
63 ms |
4084 KB |
Output is correct |
42 |
Correct |
88 ms |
4152 KB |
Output is correct |
43 |
Correct |
1 ms |
204 KB |
Output is correct |
44 |
Correct |
1 ms |
204 KB |
Output is correct |
45 |
Correct |
1 ms |
204 KB |
Output is correct |
46 |
Correct |
0 ms |
204 KB |
Output is correct |
47 |
Correct |
0 ms |
204 KB |
Output is correct |
48 |
Correct |
1 ms |
204 KB |
Output is correct |
49 |
Correct |
67 ms |
4236 KB |
Output is correct |
50 |
Correct |
63 ms |
4064 KB |
Output is correct |
51 |
Correct |
62 ms |
4140 KB |
Output is correct |
52 |
Correct |
59 ms |
3976 KB |
Output is correct |
53 |
Correct |
124 ms |
4016 KB |
Output is correct |
54 |
Correct |
140 ms |
4120 KB |
Output is correct |
55 |
Correct |
113 ms |
4148 KB |
Output is correct |
56 |
Correct |
22 ms |
716 KB |
Output is correct |
57 |
Correct |
21 ms |
716 KB |
Output is correct |
58 |
Correct |
82 ms |
3352 KB |
Output is correct |
59 |
Correct |
97 ms |
3356 KB |
Output is correct |
60 |
Correct |
144 ms |
3312 KB |
Output is correct |
61 |
Correct |
88 ms |
3380 KB |
Output is correct |
62 |
Correct |
92 ms |
3408 KB |
Output is correct |
63 |
Correct |
118 ms |
3280 KB |
Output is correct |
64 |
Correct |
642 ms |
4044 KB |
Output is correct |
65 |
Correct |
682 ms |
4124 KB |
Output is correct |
66 |
Correct |
911 ms |
4048 KB |
Output is correct |
67 |
Correct |
15 ms |
3252 KB |
Output is correct |
68 |
Correct |
57 ms |
4288 KB |
Output is correct |
69 |
Correct |
45 ms |
4300 KB |
Output is correct |
70 |
Correct |
86 ms |
4412 KB |
Output is correct |
71 |
Correct |
86 ms |
4288 KB |
Output is correct |
72 |
Correct |
89 ms |
4420 KB |
Output is correct |
73 |
Correct |
118 ms |
4432 KB |
Output is correct |
74 |
Correct |
80 ms |
4448 KB |
Output is correct |