1+ #include < fast_io.h>
2+ #include < fast_io_device.h>
3+ #include < fast_io_dsal/queue.h>
4+ #include < fast_io_dsal/vector.h>
5+ #include < fast_io_dsal/bitvec.h>
6+ #include < fast_io_driver/timer.h>
7+
8+ struct node
9+ {
10+ std::size_t to, weight;
11+ };
12+
13+ using namespace fast_io ::io;
14+
15+ int main ()
16+ {
17+ ::fast_io::timer timer (u8" spfa_optimize_fastio_bitvec" );
18+ fast_io::ibuf_file ibf (" graph.txt" );
19+ std::size_t m, n;
20+ scan (ibf, m, n);
21+ ::fast_io::vector<::fast_io::vector<node>> graph (n);
22+ std::size_t const average{(m / n + 1 ) * 13 / 10 };
23+ for (auto &v : graph)
24+ {
25+ v.reserve (average);
26+ }
27+ for (std::size_t i{}; i != m; ++i)
28+ {
29+ std::size_t a, b, w;
30+ scan (ibf, a, b, w);
31+ graph[a].push_back ({b, w});
32+ }
33+ ::fast_io::vector<std::size_t > relax (n, SIZE_MAX );
34+ ::fast_io::bitvec occupied (n);
35+ ::fast_io::queue<std::size_t > queue;
36+ occupied.set_front ();
37+ for (queue.push (relax.front () = 0 ); !queue.is_empty (); queue.pop ())
38+ {
39+ auto front{queue.front ()};
40+ auto minimum_weight{relax[front]};
41+ for (auto e : graph[front])
42+ {
43+ if (minimum_weight + e.weight < relax[e.to ])
44+ {
45+ relax[e.to ] = minimum_weight + e.weight ;
46+ if (!occupied.test (e.to ))
47+ {
48+ occupied.set (e.to );
49+ queue.push (e.to );
50+ }
51+ }
52+ }
53+ occupied.reset (front);
54+ }
55+ fast_io::obuf_file obf (" spfa.txt" );
56+ if (relax.back () == SIZE_MAX )
57+ {
58+ print (obf, " no answer\n " );
59+ }
60+ else
61+ {
62+ println (obf, relax.back ());
63+ }
64+ }
0 commit comments