#include<bits/stdc++.h>
#include<fstream>
using namespace std;
ifstream fin("WINTER.inp");
ofstream fout("WINTER.out");
#define ll long long
#define int long long
#define pb push_back
#define forr(i, a, b) for(int i = a; i < b; i++)
#define dorr(i, a, b) for(int i = a; i >= b; i--)
#define ld long double
#define vt vector
#include<fstream>
#define fi first
#define se second
#define pll pair<ll, ll>
#define pii pair<int, int>
const ld PI = 3.14159265359;
const ll mod = 1e9 + 7;
const int mxn = 205, mxm = 50005;
struct E{
ll v, c, e, id;
};
ll d[mxn + 1];
vt<E>adj[mxn + 1], rev[mxn + 1];
E edge[mxm + 1];
bool used[mxm + 1];
pii trace[mxn + 1], trace2[mxn + 1];
int n, m;
ll dis[205][205];
signed main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> m;
forr(i, 1, n + 1){
forr(j, 1, n + 1){
dis[i][j] = 1e18;
}
}
forr(i, 0, m){
int u, v, c, e; cin >> u >> v >> c >> e;
adj[u].pb({v, c, e, i}); rev[v].pb({u, c, e, i});
edge[i] = {u, v, c, e};
}
dis[1][1] = 0;
priority_queue<pll, vt<pll>, greater<pll>>pq; pq.push({dis[1][1], 1});
while(!pq.empty()){
auto [dd, u] = pq.top(); pq.pop();
if(dis[1][u] != dd)continue;
for(auto [v, c, e, id]: adj[u]){
if(dis[1][v] > dis[1][u] + c){
dis[1][v] = dis[1][u] + c;
pq.push({dis[1][v], v});
trace[v] = make_pair(id, u);
}
}
}
dis[n][n] = 0;
pq.push({dis[n][n], n});
while(!pq.empty()){
auto [dd, u] = pq.top(); pq.pop();
if(dis[u][n] != dd)continue;
for(auto [v, c, e, id]: rev[u]){
if(dis[v][n] > dis[u][n] + c){
dis[v][n] = dis[u][n] + c;
pq.push({dis[v][n], v});
}
}
}
pq.push({dis[n][n], n});
while(!pq.empty()){
auto [dd, u] = pq.top(); pq.pop();
if(dis[n][u] != dd)continue;
for(auto [v, c, e, id]: adj[u]){
if(dis[n][v] > dis[n][u] + c){
dis[n][v] = dis[n][u] + c;
pq.push({dis[n][v], v});
trace2[v] = make_pair(id, u);
}
}
}
pq.push({dis[1][1], 1});
while(!pq.empty()){
auto [dd, u] = pq.top(); pq.pop();
if(dis[u][1] != dd)continue;
for(auto [v, c, e, id]: rev[u]){
if(dis[v][1] > dis[u][1] + c){
dis[v][1] = dis[u][1] + c;
pq.push({dis[v][1], v});
}
}
}
if(dis[1][n] < 1e18){
int v = n;
while(v != 1){
used[trace[v].fi] = 1;
v = trace[v].se;
}
}
if(dis[n][1] < 1e18){
int v = 1;
while(v != n){
used[trace2[v].fi] = 1;
v = trace2[v].se;
}
}
int cnt = 0;
ll ans = dis[1][n] + dis[n][1];
for(int i = 0; i < m; i++){
auto [u, v, c, e] = edge[i];
if(!used[i]){
ll ton = min(dis[1][n], dis[1][v] + dis[u][n] + c);
ll to1 = min(dis[n][1], dis[n][v] + dis[u][1] + c);
ans = min(ans, ton + to1 + e);
}else{
cnt++;
forr(j, 1, n + 1)d[j] = 1e18;
d[1] = 0; pq.push({d[1], 1});
while(!pq.empty()){
auto [dd, uu] = pq.top(); pq.pop();
if(d[uu] != dd)continue;
for(auto [vv, cc, ee, id]: adj[uu]){
if(id == i)continue;
if(d[vv] > d[uu] + cc){
d[vv] = d[uu] + cc; pq.push({d[vv], vv});
}
}
if(uu == v){
if(d[u] > d[uu] + c){
d[u] = d[uu] + c;
pq.push({d[u], u});
}
}
}
ll curr = d[n];
forr(j, 1, n + 1)d[j] = 1e18;
d[n] = 0; pq.push({d[n], n});
while(!pq.empty()){
auto [dd, uu] = pq.top(); pq.pop();
if(d[uu] != dd)continue;
for(auto [vv, cc, ee, id]: adj[uu]){
if(id == i)continue;
if(d[vv] > d[uu] + cc){
d[vv] = d[uu] + cc; pq.push({d[vv], vv});
}
}
if(uu == v){
if(d[u] > d[uu] + c){
d[u] = d[uu] + c;
pq.push({d[u], u});
}
}
}
curr += d[1];
ans = min(ans, curr + e);
}
}
if(ans >= 1e18)cout << -1;
else cout << ans;
return(0);
}
Compilation message
ho_t4.cpp: In function 'int main()':
ho_t4.cpp:48:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
48 | auto [dd, u] = pq.top(); pq.pop();
| ^
ho_t4.cpp:50:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
50 | for(auto [v, c, e, id]: adj[u]){
| ^
ho_t4.cpp:61:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
61 | auto [dd, u] = pq.top(); pq.pop();
| ^
ho_t4.cpp:63:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
63 | for(auto [v, c, e, id]: rev[u]){
| ^
ho_t4.cpp:73:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
73 | auto [dd, u] = pq.top(); pq.pop();
| ^
ho_t4.cpp:75:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
75 | for(auto [v, c, e, id]: adj[u]){
| ^
ho_t4.cpp:86:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
86 | auto [dd, u] = pq.top(); pq.pop();
| ^
ho_t4.cpp:88:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
88 | for(auto [v, c, e, id]: rev[u]){
| ^
ho_t4.cpp:115:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
115 | auto [u, v, c, e] = edge[i];
| ^
ho_t4.cpp:127:22: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
127 | auto [dd, uu] = pq.top(); pq.pop();
| ^
ho_t4.cpp:129:26: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
129 | for(auto [vv, cc, ee, id]: adj[uu]){
| ^
ho_t4.cpp:146:22: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
146 | auto [dd, uu] = pq.top(); pq.pop();
| ^
ho_t4.cpp:148:26: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
148 | for(auto [vv, cc, ee, id]: adj[uu]){
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
724 KB |
Output is correct |
2 |
Correct |
1 ms |
596 KB |
Output is correct |
3 |
Correct |
2 ms |
724 KB |
Output is correct |
4 |
Correct |
2 ms |
724 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
596 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
15 ms |
832 KB |
Output is correct |
11 |
Correct |
19 ms |
724 KB |
Output is correct |
12 |
Correct |
20 ms |
832 KB |
Output is correct |
13 |
Correct |
1 ms |
724 KB |
Output is correct |
14 |
Correct |
1 ms |
748 KB |
Output is correct |
15 |
Correct |
2 ms |
852 KB |
Output is correct |
16 |
Correct |
2 ms |
852 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
7252 KB |
Output is correct |
2 |
Correct |
24 ms |
7128 KB |
Output is correct |
3 |
Correct |
22 ms |
7124 KB |
Output is correct |
4 |
Correct |
2 ms |
852 KB |
Output is correct |
5 |
Correct |
2 ms |
724 KB |
Output is correct |
6 |
Correct |
1 ms |
724 KB |
Output is correct |
7 |
Correct |
1 ms |
724 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
20 ms |
6852 KB |
Output is correct |
10 |
Correct |
21 ms |
8404 KB |
Output is correct |
11 |
Correct |
24 ms |
8372 KB |
Output is correct |
12 |
Correct |
23 ms |
8124 KB |
Output is correct |
13 |
Correct |
26 ms |
8124 KB |
Output is correct |
14 |
Correct |
28 ms |
9048 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
724 KB |
Output is correct |
2 |
Correct |
1 ms |
596 KB |
Output is correct |
3 |
Correct |
15 ms |
5508 KB |
Output is correct |
4 |
Correct |
1 ms |
596 KB |
Output is correct |
5 |
Correct |
19 ms |
6940 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
21 ms |
7204 KB |
Output is correct |
9 |
Correct |
18 ms |
7168 KB |
Output is correct |
10 |
Correct |
20 ms |
7028 KB |
Output is correct |
11 |
Correct |
18 ms |
6908 KB |
Output is correct |
12 |
Correct |
19 ms |
7108 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
352 KB |
Output is correct |
15 |
Correct |
1 ms |
416 KB |
Output is correct |
16 |
Correct |
1 ms |
352 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
356 KB |
Output is correct |
19 |
Correct |
21 ms |
8232 KB |
Output is correct |
20 |
Correct |
20 ms |
7836 KB |
Output is correct |
21 |
Correct |
21 ms |
7852 KB |
Output is correct |
22 |
Correct |
22 ms |
7892 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
724 KB |
Output is correct |
2 |
Correct |
1 ms |
596 KB |
Output is correct |
3 |
Correct |
2 ms |
724 KB |
Output is correct |
4 |
Correct |
2 ms |
724 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
596 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
15 ms |
832 KB |
Output is correct |
11 |
Correct |
19 ms |
724 KB |
Output is correct |
12 |
Correct |
20 ms |
832 KB |
Output is correct |
13 |
Correct |
1 ms |
724 KB |
Output is correct |
14 |
Correct |
1 ms |
748 KB |
Output is correct |
15 |
Correct |
2 ms |
852 KB |
Output is correct |
16 |
Correct |
2 ms |
852 KB |
Output is correct |
17 |
Correct |
21 ms |
7252 KB |
Output is correct |
18 |
Correct |
24 ms |
7128 KB |
Output is correct |
19 |
Correct |
22 ms |
7124 KB |
Output is correct |
20 |
Correct |
2 ms |
852 KB |
Output is correct |
21 |
Correct |
2 ms |
724 KB |
Output is correct |
22 |
Correct |
1 ms |
724 KB |
Output is correct |
23 |
Correct |
1 ms |
724 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
20 ms |
6852 KB |
Output is correct |
26 |
Correct |
21 ms |
8404 KB |
Output is correct |
27 |
Correct |
24 ms |
8372 KB |
Output is correct |
28 |
Correct |
23 ms |
8124 KB |
Output is correct |
29 |
Correct |
26 ms |
8124 KB |
Output is correct |
30 |
Correct |
28 ms |
9048 KB |
Output is correct |
31 |
Correct |
2 ms |
724 KB |
Output is correct |
32 |
Correct |
1 ms |
596 KB |
Output is correct |
33 |
Correct |
15 ms |
5508 KB |
Output is correct |
34 |
Correct |
1 ms |
596 KB |
Output is correct |
35 |
Correct |
19 ms |
6940 KB |
Output is correct |
36 |
Correct |
0 ms |
340 KB |
Output is correct |
37 |
Correct |
1 ms |
340 KB |
Output is correct |
38 |
Correct |
21 ms |
7204 KB |
Output is correct |
39 |
Correct |
18 ms |
7168 KB |
Output is correct |
40 |
Correct |
20 ms |
7028 KB |
Output is correct |
41 |
Correct |
18 ms |
6908 KB |
Output is correct |
42 |
Correct |
19 ms |
7108 KB |
Output is correct |
43 |
Correct |
1 ms |
340 KB |
Output is correct |
44 |
Correct |
1 ms |
352 KB |
Output is correct |
45 |
Correct |
1 ms |
416 KB |
Output is correct |
46 |
Correct |
1 ms |
352 KB |
Output is correct |
47 |
Correct |
1 ms |
340 KB |
Output is correct |
48 |
Correct |
1 ms |
356 KB |
Output is correct |
49 |
Correct |
21 ms |
8232 KB |
Output is correct |
50 |
Correct |
20 ms |
7836 KB |
Output is correct |
51 |
Correct |
21 ms |
7852 KB |
Output is correct |
52 |
Correct |
22 ms |
7892 KB |
Output is correct |
53 |
Correct |
27 ms |
7756 KB |
Output is correct |
54 |
Correct |
24 ms |
7892 KB |
Output is correct |
55 |
Correct |
23 ms |
7892 KB |
Output is correct |
56 |
Correct |
2 ms |
852 KB |
Output is correct |
57 |
Correct |
2 ms |
852 KB |
Output is correct |
58 |
Correct |
72 ms |
6344 KB |
Output is correct |
59 |
Correct |
85 ms |
6388 KB |
Output is correct |
60 |
Correct |
118 ms |
6372 KB |
Output is correct |
61 |
Correct |
70 ms |
6356 KB |
Output is correct |
62 |
Correct |
82 ms |
6376 KB |
Output is correct |
63 |
Correct |
110 ms |
6324 KB |
Output is correct |
64 |
Correct |
559 ms |
6988 KB |
Output is correct |
65 |
Correct |
600 ms |
7240 KB |
Output is correct |
66 |
Correct |
810 ms |
6940 KB |
Output is correct |
67 |
Correct |
15 ms |
5524 KB |
Output is correct |
68 |
Correct |
21 ms |
8264 KB |
Output is correct |
69 |
Correct |
25 ms |
8468 KB |
Output is correct |
70 |
Correct |
31 ms |
8224 KB |
Output is correct |
71 |
Correct |
24 ms |
8320 KB |
Output is correct |
72 |
Correct |
23 ms |
8128 KB |
Output is correct |
73 |
Correct |
26 ms |
8268 KB |
Output is correct |
74 |
Correct |
23 ms |
8220 KB |
Output is correct |