File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -29,11 +29,15 @@ template < class T > struct closed_plus
2929
3030 T operator ()(const T& a, const T& b) const
3131 {
32- using namespace std ;
3332 if (a == inf)
3433 return inf;
3534 if (b == inf)
3635 return inf;
36+ // saturate to avoid signed overflow (e.g. negative cycles)
37+ if (b > 0 && a > (std::numeric_limits< T >::max)() - b)
38+ return inf;
39+ if (b < 0 && a < (std::numeric_limits< T >::lowest)() - b)
40+ return (std::numeric_limits< T >::lowest)();
3741 return a + b;
3842 }
3943};
Original file line number Diff line number Diff line change @@ -76,10 +76,10 @@ struct Highway
7676 }
7777
7878 string name;
79- double miles;
80- int speed_limit;
81- int lanes;
82- bool divided;
79+ double miles = 0 ;
80+ int speed_limit = 65 ;
81+ int lanes = 4 ;
82+ bool divided = true ;
8383};
8484
8585std::ostream& operator <<(std::ostream& out, const Highway& highway)
You can’t perform that action at this time.
0 commit comments