#include <bits/stdc++.h>
using namespace std;
#define n_l '\n'
#define dbg(...) cout << "[" << #__VA_ARGS__ << "]: "; cout << to_string(__VA_ARGS__) << endl
template <typename T, size_t N> int SIZE(const T (&t)[N]){ return N; } template<typename T> int SIZE(const T &t){ return t.size(); } string to_string(const string s, int x1=0, int x2=1e9){ return '"' + ((x1 < s.size()) ? s.substr(x1, x2-x1+1) : "") + '"'; } string to_string(const char* s) { return to_string((string) s); } string to_string(const bool b) { return (b ? "true" : "false"); } string to_string(const char c){ return string({c}); } template<size_t N> string to_string(const bitset<N> &b, int x1=0, int x2=1e9){ string t = ""; for(int __iii__ = min(x1,SIZE(b)), __jjj__ = min(x2, SIZE(b)-1); __iii__ <= __jjj__; ++__iii__){ t += b[__iii__] + '0'; } return '"' + t + '"'; } template <typename A, typename... C> string to_string(const A (&v), int x1=0, int x2=1e9, C... coords); int l_v_l_v_l = 0, t_a_b_s = 0; template <typename A, typename B> string to_string(const pair<A, B> &p) { l_v_l_v_l++; string res = "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; l_v_l_v_l--; return res; } template <typename A, typename... C> string to_string(const A (&v), int x1, int x2, C... coords) { int rnk = rank<A>::value; string tab(t_a_b_s, ' '); string res = ""; bool first = true; if(l_v_l_v_l == 0) res += n_l; res += tab + "["; x1 = min(x1, SIZE(v)), x2 = min(x2, SIZE(v)); auto l = begin(v); advance(l, x1); auto r = l; advance(r, (x2-x1) + (x2 < SIZE(v))); for (auto e = l; e != r; e = next(e)) { if (!first) { res += ", "; } first = false; l_v_l_v_l++; if(e != l){ if(rnk > 1) { res += n_l; t_a_b_s = l_v_l_v_l; }; } else{ t_a_b_s = 0; } res += to_string(*e, coords...); l_v_l_v_l--; } res += "]"; if(l_v_l_v_l == 0) res += n_l; return res; } void dbgm(){;} template<typename Heads, typename... Tails> void dbgm(Heads H, Tails... T){ cout << to_string(H) << " | "; dbgm(T...); }
#define dbgm(...) cout << "[" << #__VA_ARGS__ << "]: "; dbgm(__VA_ARGS__); cout << endl
#define int long long int
#define MP make_pair
#define pb push_back
#define REP(i,n) for(int (i) = 0; (i) < (n); (i)++)
void fastio() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
}
const double EPS = 0.00001;
const int INF = 1e16;
const int N = 205;
const int M = 1005;
const int ALPH = 26;
const int LGN = 25;
const int MOD = 1e9+7;
int n,m,q;
vector<array<int,4> > edges;
vector<int> SP[2][N], SPR[2][N];
int ans = INF;
void dijkstra(vector<vector<array<int,2> > > &adj, vector<int> &sp, int src) {
sp.assign(n+3, INF);
vector<bool> vis(n+3, 0);
sp[src] = 0;
while(src) {
int cur = src;
src = 0;
vis[cur] = 1;
for(auto c : adj[cur]) {
sp[c[0]] = min(sp[c[0]] , sp[cur] + c[1]);
}
for(int i = 1; i<=n; i++) {
if(vis[i]) continue;
if(sp[src] > sp[i]) {
src = i;
}
}
}
}
bool dfs(int node, int fin, vector<int> &spath, vector<int> &sp, vector<vector<array<int,3> > > &adj, vector<int> &vis) {
vis[node] = 1;
if(node == fin) {
return 1;
}
for(auto c : adj[node]) {
if(vis[c[0]] || sp[node] + c[1] != sp[c[0]]) continue;
if(dfs(c[0], fin, spath, sp, adj, vis)) {
spath[c[2]] = 1;
return 1;
}
}
return 0;
}
void start_dfs(vector<int> &spat0, vector<int> &spat1, vector<int> &sp0, vector<int> &sp1) {
vector<int> vis(n+3, 0);
vector<vector<array<int,3> > > adj(n+3, vector<array<int,3> > ());
spat0.assign(m+3, 0); spat1.assign(m + 3, 0);
for(int i = 0; i<m; i++) {
auto eg = edges[i];
adj[eg[0]].pb({eg[1], eg[2], i});
}
dfs(1, n, spat0, sp0, adj, vis);
vis.assign(n+3, 0);
dfs(n, 1, spat1, sp1, adj, vis);
}
void new_dijk(vector<int> &sp, int ind, int src) {
vector<vector<array<int,2> > > adj(n+3, vector<array<int,2> >());
// adj[edges[ind][1]].pb({edges[ind][0], edges[ind][2]});
for(int i = 0; i<m; i++) {
if(i == ind) continue;
auto &eg = edges[i];
adj[eg[0]].pb({eg[1], eg[2]});
}
dijkstra(adj, sp, src);
}
inline void solve() {
cin>>n>>m;
edges.resize(m);
REP(i,m) {
cin>>edges[i][0]>>edges[i][1]>>edges[i][2]>>edges[i][3];
}
for(int i = 0; i<=n; i++) {
vector<vector<array<int,2> > > adj(n+3, vector<array<int,2> >());
for(auto eg : edges) {
if(eg[0] == i || eg[1] == i) continue;
adj[eg[0]].pb({eg[1], eg[2]});
}
dijkstra(adj, SP[0][i], 1);
dijkstra(adj, SP[1][i], n);
adj.assign(n+3, vector<array<int,2> > ());
for(auto eg : edges) {
if(eg[0] == i || eg[1] == i) continue;
adj[eg[1]].pb({eg[0], eg[2]});
}
dijkstra(adj, SPR[0][i], 1);
dijkstra(adj, SPR[1][i], n);
}
vector<int> spath[2];
start_dfs(spath[0], spath[1], SP[0][0], SP[1][0]);
// dbg(SP[0][0]);
ans = min(ans , SP[0][0][n] + SP[1][0][1]); //initial state
// dbg(ans);
// dbg(spath[0]); dbg(spath[1]);
for(int i = 0; i<m; i++) {
auto eg = edges[i];
int cost;
// dbg(eg);
// cout<<"find AB\n";
if(spath[0][i]) {
vector<int> sp;
new_dijk(sp, i, 1);
cost = sp[n];
}
else {
cost = SP[0][0][n];
}
// dbg(cost);
int pathAB = min( (SP[0][eg[0]][eg[1]] + SPR[1][eg[1]][eg[0]] + eg[2]) , cost);
// cout<<"find BA\n";
if(spath[1][i]) {
vector<int> sp;
new_dijk(sp, i, n);
cost = sp[1];
// cout<<"CRIT\n";
}
else {
cost = SP[1][0][1];
}
// dbg(cost);
int pathBA = min(SP[1][eg[0]][eg[1]] + SPR[0][eg[1]][eg[0]] + eg[2], cost);
int ret = pathAB + pathBA;
// dbg(pathAB); dbg(pathBA);
ret += eg[3];
// dbg(ret);
ans = min(ans , ret);
}
if(ans == INF) ans = -1;
cout<<ans<<"\n";
}
signed main() {
fastio();
solve();
}
Compilation message
ho_t4.cpp: In function 'std::string to_string(std::string, int, int)':
ho_t4.cpp:6:208: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
6 | template <typename T, size_t N> int SIZE(const T (&t)[N]){ return N; } template<typename T> int SIZE(const T &t){ return t.size(); } string to_string(const string s, int x1=0, int x2=1e9){ return '"' + ((x1 < s.size()) ? s.substr(x1, x2-x1+1) : "") + '"'; } string to_string(const char* s) { return to_string((string) s); } string to_string(const bool b) { return (b ? "true" : "false"); } string to_string(const char c){ return string({c}); } template<size_t N> string to_string(const bitset<N> &b, int x1=0, int x2=1e9){ string t = ""; for(int __iii__ = min(x1,SIZE(b)), __jjj__ = min(x2, SIZE(b)-1); __iii__ <= __jjj__; ++__iii__){ t += b[__iii__] + '0'; } return '"' + t + '"'; } template <typename A, typename... C> string to_string(const A (&v), int x1=0, int x2=1e9, C... coords); int l_v_l_v_l = 0, t_a_b_s = 0; template <typename A, typename B> string to_string(const pair<A, B> &p) { l_v_l_v_l++; string res = "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; l_v_l_v_l--; return res; } template <typename A, typename... C> string to_string(const A (&v), int x1, int x2, C... coords) { int rnk = rank<A>::value; string tab(t_a_b_s, ' '); string res = ""; bool first = true; if(l_v_l_v_l == 0) res += n_l; res += tab + "["; x1 = min(x1, SIZE(v)), x2 = min(x2, SIZE(v)); auto l = begin(v); advance(l, x1); auto r = l; advance(r, (x2-x1) + (x2 < SIZE(v))); for (auto e = l; e != r; e = next(e)) { if (!first) { res += ", "; } first = false; l_v_l_v_l++; if(e != l){ if(rnk > 1) { res += n_l; t_a_b_s = l_v_l_v_l; }; } else{ t_a_b_s = 0; } res += to_string(*e, coords...); l_v_l_v_l--; } res += "]"; if(l_v_l_v_l == 0) res += n_l; return res; } void dbgm(){;} template<typename Heads, typename... Tails> void dbgm(Heads H, Tails... T){ cout << to_string(H) << " | "; dbgm(T...); }
| ~~~^~~~~~~~~~
ho_t4.cpp: In function 'void solve()':
ho_t4.cpp:13:26: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
13 | #define REP(i,n) for(int (i) = 0; (i) < (n); (i)++)
| ^
ho_t4.cpp:94:5: note: in expansion of macro 'REP'
94 | REP(i,m) {
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
78 ms |
1620 KB |
Output is correct |
2 |
Correct |
12 ms |
1608 KB |
Output is correct |
3 |
Correct |
83 ms |
1952 KB |
Output is correct |
4 |
Correct |
87 ms |
1748 KB |
Output is correct |
5 |
Correct |
3 ms |
500 KB |
Output is correct |
6 |
Correct |
20 ms |
1628 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
480 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
126 ms |
1888 KB |
Output is correct |
11 |
Correct |
127 ms |
1696 KB |
Output is correct |
12 |
Correct |
125 ms |
1876 KB |
Output is correct |
13 |
Correct |
44 ms |
1620 KB |
Output is correct |
14 |
Correct |
63 ms |
1776 KB |
Output is correct |
15 |
Correct |
63 ms |
1724 KB |
Output is correct |
16 |
Correct |
64 ms |
1616 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
377 ms |
6352 KB |
Output is correct |
2 |
Correct |
379 ms |
7136 KB |
Output is correct |
3 |
Correct |
387 ms |
7436 KB |
Output is correct |
4 |
Correct |
85 ms |
1804 KB |
Output is correct |
5 |
Correct |
75 ms |
1664 KB |
Output is correct |
6 |
Correct |
29 ms |
1616 KB |
Output is correct |
7 |
Correct |
8 ms |
1628 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
332 ms |
7808 KB |
Output is correct |
10 |
Correct |
326 ms |
7372 KB |
Output is correct |
11 |
Correct |
354 ms |
7568 KB |
Output is correct |
12 |
Correct |
352 ms |
7472 KB |
Output is correct |
13 |
Correct |
349 ms |
7772 KB |
Output is correct |
14 |
Correct |
367 ms |
7628 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
89 ms |
1620 KB |
Output is correct |
2 |
Correct |
31 ms |
1684 KB |
Output is correct |
3 |
Correct |
384 ms |
6204 KB |
Output is correct |
4 |
Correct |
23 ms |
1616 KB |
Output is correct |
5 |
Correct |
582 ms |
7284 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
322 ms |
7212 KB |
Output is correct |
9 |
Correct |
327 ms |
7308 KB |
Output is correct |
10 |
Correct |
428 ms |
7184 KB |
Output is correct |
11 |
Correct |
428 ms |
7096 KB |
Output is correct |
12 |
Correct |
433 ms |
7348 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 |
0 ms |
480 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
495 ms |
7328 KB |
Output is correct |
20 |
Correct |
489 ms |
7272 KB |
Output is correct |
21 |
Correct |
493 ms |
7236 KB |
Output is correct |
22 |
Correct |
433 ms |
7172 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
78 ms |
1620 KB |
Output is correct |
2 |
Correct |
12 ms |
1608 KB |
Output is correct |
3 |
Correct |
83 ms |
1952 KB |
Output is correct |
4 |
Correct |
87 ms |
1748 KB |
Output is correct |
5 |
Correct |
3 ms |
500 KB |
Output is correct |
6 |
Correct |
20 ms |
1628 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
480 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
126 ms |
1888 KB |
Output is correct |
11 |
Correct |
127 ms |
1696 KB |
Output is correct |
12 |
Correct |
125 ms |
1876 KB |
Output is correct |
13 |
Correct |
44 ms |
1620 KB |
Output is correct |
14 |
Correct |
63 ms |
1776 KB |
Output is correct |
15 |
Correct |
63 ms |
1724 KB |
Output is correct |
16 |
Correct |
64 ms |
1616 KB |
Output is correct |
17 |
Correct |
377 ms |
6352 KB |
Output is correct |
18 |
Correct |
379 ms |
7136 KB |
Output is correct |
19 |
Correct |
387 ms |
7436 KB |
Output is correct |
20 |
Correct |
85 ms |
1804 KB |
Output is correct |
21 |
Correct |
75 ms |
1664 KB |
Output is correct |
22 |
Correct |
29 ms |
1616 KB |
Output is correct |
23 |
Correct |
8 ms |
1628 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
332 ms |
7808 KB |
Output is correct |
26 |
Correct |
326 ms |
7372 KB |
Output is correct |
27 |
Correct |
354 ms |
7568 KB |
Output is correct |
28 |
Correct |
352 ms |
7472 KB |
Output is correct |
29 |
Correct |
349 ms |
7772 KB |
Output is correct |
30 |
Correct |
367 ms |
7628 KB |
Output is correct |
31 |
Correct |
89 ms |
1620 KB |
Output is correct |
32 |
Correct |
31 ms |
1684 KB |
Output is correct |
33 |
Correct |
384 ms |
6204 KB |
Output is correct |
34 |
Correct |
23 ms |
1616 KB |
Output is correct |
35 |
Correct |
582 ms |
7284 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
322 ms |
7212 KB |
Output is correct |
39 |
Correct |
327 ms |
7308 KB |
Output is correct |
40 |
Correct |
428 ms |
7184 KB |
Output is correct |
41 |
Correct |
428 ms |
7096 KB |
Output is correct |
42 |
Correct |
433 ms |
7348 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 |
0 ms |
480 KB |
Output is correct |
47 |
Correct |
0 ms |
348 KB |
Output is correct |
48 |
Correct |
0 ms |
348 KB |
Output is correct |
49 |
Correct |
495 ms |
7328 KB |
Output is correct |
50 |
Correct |
489 ms |
7272 KB |
Output is correct |
51 |
Correct |
493 ms |
7236 KB |
Output is correct |
52 |
Correct |
433 ms |
7172 KB |
Output is correct |
53 |
Correct |
372 ms |
7396 KB |
Output is correct |
54 |
Correct |
380 ms |
7464 KB |
Output is correct |
55 |
Correct |
376 ms |
7396 KB |
Output is correct |
56 |
Correct |
82 ms |
1620 KB |
Output is correct |
57 |
Correct |
79 ms |
1620 KB |
Output is correct |
58 |
Correct |
559 ms |
6484 KB |
Output is correct |
59 |
Correct |
560 ms |
6364 KB |
Output is correct |
60 |
Correct |
568 ms |
6292 KB |
Output is correct |
61 |
Correct |
572 ms |
6188 KB |
Output is correct |
62 |
Correct |
575 ms |
6348 KB |
Output is correct |
63 |
Correct |
574 ms |
6108 KB |
Output is correct |
64 |
Correct |
652 ms |
6324 KB |
Output is correct |
65 |
Correct |
649 ms |
6380 KB |
Output is correct |
66 |
Correct |
622 ms |
6240 KB |
Output is correct |
67 |
Correct |
13 ms |
5492 KB |
Output is correct |
68 |
Correct |
334 ms |
7492 KB |
Output is correct |
69 |
Correct |
340 ms |
7508 KB |
Output is correct |
70 |
Correct |
349 ms |
7464 KB |
Output is correct |
71 |
Correct |
352 ms |
7460 KB |
Output is correct |
72 |
Correct |
353 ms |
7672 KB |
Output is correct |
73 |
Correct |
346 ms |
7648 KB |
Output is correct |
74 |
Correct |
353 ms |
7540 KB |
Output is correct |