├── README.md
├── hack
├── Asterix the Gaul.cpp
├── DHL.cpp
└── readme.md
├── problems-of-the-week
├── Augean Stable.cpp
├── Ceryneian Hind.cpp
├── Deck of Cards.cpp
├── Greyjoy.cpp
├── Hands.cpp
├── Knights.cpp
├── Ludo Bagman.cpp
├── Motorcycles.cpp
├── Octopussy.cpp
├── Rubeus Hagrid.cpp
├── San Francisco.cpp
├── Strikesback.cpp
└── Tracking.cpp
├── problems
├── algocoön group.pdf
├── ant challenge.pdf
├── antenna.pdf
├── asterix and the chariot race.pdf
├── asterix in switzerland.pdf
├── asterix_the_gaul.pdf
├── beach_bars.pdf
├── bistro.pdf
├── boats.pdf
├── buddy selection.pdf
├── build_the_sum.pdf
├── burning_coins.pdf
├── car sharing.pdf
├── casino royale.pdf
├── ceryneian hind.pdf
├── clues.pdf
├── coin cossing tournament.pdf
├── deck_of_cards.pdf
├── dhl.pdf
├── diet.pdf
├── dominoes.pdf
├── even_matrices.pdf
├── even_pairs.pdf
├── evolution.pdf
├── first steps with bgl.pdf
├── firsthit.pdf
├── fleetrace.pdf
├── germs.pdf
├── greyjoy.pdf
├── h1n1.pdf
├── hands.pdf
├── hiking-maps.pdf
├── hit.pdf
├── idefix and the mansion of the gods.pdf
├── important bridges.pdf
├── inball.pdf
├── knights.pdf
├── light the stage.pdf
├── london.pdf
├── lord voldemort.pdf
├── ludo bagman.pdf
├── marathon.pdf
├── motorcycles.pdf
├── moving_books.pdf
├── nemean lion.pdf
├── new york.pdf
├── octopussy.pdf
├── on her majesty’s secret service.pdf
├── placing knights.pdf
├── real estate market.pdf
├── revenge of the sith.pdf
├── rubeus_hagrid.pdf
├── san_francisco.pdf
├── search_snippets.pdf
├── severus_snape.pdf
├── shopping trip.pdf
├── strikesback.pdf
├── suez.pdf
├── the augean stables.pdf
├── the fighting pits of meereen .pdf
├── the_great_game.pdf
├── tiles.pdf
├── tracking.pdf
├── what is the maximum.pdf
└── world cup.pdf
├── resume
└── README.md
├── week1
├── Build the Sum.cpp
├── Dominoes.cpp
├── Even Matrices.cpp
└── Even Pairs.cpp
├── week10
├── Clues.cpp
├── DHL.cpp
├── Fleetrace.cpp
└── New York.cpp
├── week11
├── Asterix in Switzerland.cpp
├── Sith.cpp
├── The Fighting Pits of Meereen.cpp
└── Worldcup.cpp
├── week12
├── Car Sharing.cpp
├── Lord Voldemort.cpp
├── Nemean Lion.cpp
├── Nemean Lion_4.cpp
└── On Her Majesty's Secret Service.cpp
├── week13
├── Asterix and the Chariot Race.cpp
├── Evolution.cpp
├── Idéfix.cpp
└── Marathon.cpp
├── week2
├── Beach Bars.cpp
├── Burning Coins.cpp
├── Search Snippets.cpp
└── The Great Game.cpp
├── week3
├── Ant Challenge.cpp
├── Buddy Selection.cpp
├── First steps with BGL.cpp
└── Important Bridges.cpp
├── week4
├── Antenna.cpp
├── First Hit.cpp
├── Hiking Map.cpp
└── Hit.cpp
├── week5
├── Asterix the Gaul.cpp
├── Boats.cpp
├── Moving Books.cpp
└── Severus Snape.cpp
├── week6
├── Diet.cpp
├── Inball.cpp
├── Maximize it!.cpp
└── Suez.cpp
├── week7
├── Coin Tossing Tournament.cpp
├── London.cpp
├── Shopping Trip.cpp
└── Tiles.cpp
├── week8
├── Bistro.cpp
├── Germs.cpp
├── H1N1.cpp
└── Light the Stage.cpp
└── week9
├── Algocoon.cpp
├── Casino Royale.cpp
├── Placing Knights.cpp
└── Real Estate Market.cpp
/README.md:
--------------------------------------------------------------------------------
1 | # ALGO Lab ETHZ 2022
2 |
3 |
4 |
5 |  
6 |
7 |
8 |
9 | My solutions for the course algo lab 2022 given at ETHZ. This course deals with various topics such as dynamic programming, binary search, greedy algorithms, graphs, flows, geometry and linear programming.
10 |
11 |
12 |
13 | [Link to the course](https://cadmo.ethz.ch/education/lectures/HS22/algolab/index.html) • [Boost Graph Library](https://www.boost.org/doc/libs/1_78_0/libs/graph/doc/index.html) • [CGAL](https://www.cgal.org/)
14 |
15 |
16 |
17 |
18 |
19 | ## Problems with solutions
20 |
21 | | Name | Challenge | Solution | Points | Week | Theme |
22 | | ------------- |---------------------- |------------------|-----------------|------------------|-----------------------|
23 | | Build the sum | [File](./problems/build_the_sum.pdf) | [C++ code](./week1/Build%20the%20Sum.cpp) | 100 | 1 | STL |
24 | | Dominoes | [File](./problems/dominoes.pdf) | [C++ code](./week1/Dominoes.cpp) | 100 | 1 | STL |
25 | | Even Pairs | [File](./problems/even_pairs.pdf) | [C++ code](./week1/Even%20Pairs.cpp) | 100 | 1 | Combinatorics - Prefix sum |
26 | | Even Matrices | [File](./problems/even_matrices.pdf) | [C++ code](./week1/Even%20Matrices.cpp) | 100 | 1 | Combinatorics - Prefix sum |
27 | | Beach Bars | [File](./problems/beach_bars.pdf) | [C++ code](./week2/Beach%20Bars.cpp) | 100 | 2 | Two pointers |
28 | | Burning Coins | [File](./problems/burning_coins.pdf) | [C++ code](./week2/Burning%20Coins.cpp) | 100 | 2 | DP |
29 | | Search Snippets | [File](./problems/search_snippets.pdf) | [C++ code](./week2/Search%20Snippets.cpp) | 100 | 2 | Two pointers |
30 | | The Great Game | [File](./problems/the_great_game.pdf) | [C++ code](./week2/The%20Great%20Game.cpp) | 100 | 2 | DP |
31 | | Ant Challenge | [File](./problems/ant%20challenge.pdf) | [C++ code](./week3/Ant%20Challenge.cpp) | 100 | 3 | Graph - MST - Shortest Path |
32 | | Buddy Selection | [File](./problems/buddy%20selection.pdf) | [C++ code](./week3/Buddy%20Selection.cpp) | 100 | 3 | Graph - Matching |
33 | | First step with BGL | [File](./problems/first%20steps%20with%20bgl.pdf) | [C++ code](./week3/First%20steps%20with%20BGL.cpp) | 100 | 3 | Graph - BGL |
34 | | Important Bridges | [File](./problems/important%20bridges.pdf) | [C++ code](./week3/Important%20Bridges.cpp) | 100 | 3 | Graph - DFS |
35 | | Antenna | [File](./problems/antenna.pdf) | [C++ code](./week4/Antenna.cpp) | 100 | 4 | Geometry |
36 | | First Hit | [File](./problems/firsthit.pdf) | [C++ code](./week4/First%20Hit.cpp) | 100 | 4 | Geometry |
37 | | Hiking Map | [File](./problems/hiking-maps.pdf) | [C++ code](./week4/Hiking%20Map.cpp) | 100 | 4 | Geometry - Two pointers |
38 | | Hit | [File](./problems/hit.pdf) | [C++ code](./week4/First%20Hit.cpp) | 100 | 4 | Geometry |
39 | | Asterix the Gaul | [File](./problems/asterix_the_gaul.pdf) | [C++ code](./week5/Asterix%20the%20Gaul.cpp) | 100 | 5 | Binary Search - Split & List |
40 | | Boats | [File](./problems/boats.pdf) | [C++ code](./week5/Boats.cpp) | 100 | 5 | Greedy |
41 | | Moving Books | [File](./problems/moving_books.pdf) | [C++ code](./week5/Moving%20Books.cpp) | 100 | 5 | Greedy |
42 | | Severus Snape | [File](./problems/severus_snape.pdf) | [C++ code](./week5/Severus%20Snape.cpp) | 100 | 5 | Greedy - DP |
43 | | Diet | [File](./problems/diet.pdf) | [C++ code](./week6/Diet.cpp) | 100 | 6 | Linear programming |
44 | | Inball | [File](./problems/inball.pdf) | [C++ code](./week6/Inball.cpp) | 100 | 6 | Linear programming |
45 | | Maximize it!| [File](./problems/what%20is%20the%20maximum.pdf) | [C++ code](./week6/Maximize%20it!.cpp) | 100 | 6 | Linear programming |
46 | | Suez | [File](./problems/suez.pdf) | [C++ code](./week6/Suez.cpp) | 100 | 6 | Linear programming |
47 | | Coin Tossing Tournament | [File](./problems/coin%20cossing%20tournament.pdf) | [C++ code](./week7/Coin%20Tossing%20Tournament.cpp) | 100 | 7 | Flow |
48 | | London | [File](./problems/london.pdf) | [C++ code](./week7/London.cpp) | 100 | 7 | Flow |
49 | | Shopping Trip | [File](./problems/shopping%20trip.pdf) | [C++ code](./week7/Shopping%20Trip.cpp) | 100 | 7 | Flow |
50 | | Tiles | [File](./problems/tiles.pdf) | [C++ code](./week7/Tiles.cpp) | 100 | 7 | Flow |
51 | | Bistro | [File](./problems/bistro.pdf) | [C++ code](./week8/Bistro.cpp) | 100 | 8 | Advanced geometry |
52 | | Germs | [File](./problems/germs.pdf) | [C++ code](./week8/Germs.cpp) | 100 | 8 | Advanced geometry |
53 | | H1N1 | [File](./problems/h1n1.pdf) | [C++ code](./week8/H1N1.cpp) | 100 | 8 | Advanced geometry |
54 | | Light the Stage | [File](./problems/light%20the%20stage.pdf) | [C++ code](./week8/Light%20the%20Stage.cpp) | 100 | 8 | Advanced geometry |
55 | | Algocoon | [File](./problems/algoco%C3%B6n%20group.pdf) | [C++ code](./week9/Algocoon.cpp) | 100 | 9 | Advanced flows |
56 | | Casino Royale | [File](./problems/casino%20royale.pdf) | [C++ code](./week9/Casino%20Royale.cpp) | 100 | 9 | Advanced flows |
57 | | Placing Knights | [File](./problems/placing%20knights.pdf) | [C++ code](./week9/Placing%20Knights.cpp) | 100 | 9 | Advanced flows |
58 | | Real Estate Market | [File](./problems/real%20estate%20market.pdf) | [C++ code](./week9/Real%20Estate%20Market.cpp) | 100 | 9 | Advanced flows |
59 | | Clues | [File](./problems/clues.pdf) | [C++ code](./week10/Clues.cpp) | 100 | 10 | Graphs - Matchings - Advanced geometry |
60 | | DHL | [File](./problems/dhl.pdf) | [C++ code](./week10/DHL.cpp) | 100 | 10 | DP |
61 | | New York | [File](./problems/new%20york.pdf) | [C++ code](./week10/New%20York.cpp) | 100 | 10 | Two pointers - Graph - DFS |
62 | | Fleetrace | [File](./problems/fleetrace.pdf) | [C++ code](./week10/Fleetrace.cpp) | 100 | 10 | Advanced flows |
63 | | Asterix in Switzerland | [File](./problems/asterix%20in%20switzerland.pdf) | [C++ code](./week11/Asterix%20in%20Switzerland.cpp) | 100 | 11 | Flow |
64 | | Sith | [File](./problems/revenge%20of%20the%20sith.pdf) | [C++ code](./week11/Sith.cpp) | 100 | 11 | Advanced geometry - Graph - Binary search |
65 | | The Fighting Pits of Meereen | [File](./problems/the%20fighting%20pits%20of%20meereen%20.pdf) | [C++ code](./week11/The%20Fighting%20Pits%20of%20Meereen.cpp) | 100 | 11 | DP |
66 | | World Cup | [Files](./problems/world%20cup.pdf) | [C++ code](./week11/Worldcup.cpp) | 100 | 11 | Advanced geometry - Linear programming
67 | | Car Sharing | [Files](./problems/car%20sharing.pdf) | [C++ code](./week12/Car%20Sharing.cpp) | 100 | 12 | Advanced flows
68 | | Lord Voldemort | [Files](./problems/lord%20voldemort.pdf) | [C++ code](./week12/Lord%20Voldemort.cpp) | 100 | 12 | DP
69 | | On Her Majesty's Secret Service | [Files](./problems/on%20her%20majesty%E2%80%99s%20secret%20service.pdf) | [C++ code](./week12/On%20Her%20Majesty's%20Secret%20Service.cpp) | 100 | 12 | Graph - BS
70 | | Nemean Lion| [Files](./problems/nemean%20lion.pdf) | [C++ code](./week12/Nemean%20Lion.cpp) | 100 | 12 | Advanced geometry
71 | | Marathon | [Files](./problems/marathon.pdf) | [C++ code](./week13/Marathon.cpp) | 100 | 13 | Shortest path - flows
72 | | Asterix and the Chariot Race | [Files](./problems/asterix%20and%20the%20chariot%20race.pdf) | [C++ code](./week13/Asterix%20and%20the%20Chariot%20Race.cpp) | 100 | 13 | DP - Trees
73 | | Evolution | [Files](./problems/evolution.pdf) | [C++ code](./week13/Evolution.cpp) | 100 | 13 | Binary Search - Trees
74 | | Idéfix | [Files](./problems/idefix%20and%20the%20mansion%20of%20the%20gods.pdf) | [C++ code](./week13/Id%C3%A9fix.cpp) | 100 | 13 | Advanced geometry - BS
75 |
76 |
77 | ## Problems of the week with solutions
78 |
79 | | Name | Challenge | Solution | Points | Week | Theme |
80 | | ------------- |---------------------- |------------------|-----------------|------------------|-----------------------|
81 | | Deck of Cards | [File](./problems/deck_of_cards.pdf) | [C++ code](./problems-of-the-week/Deck%20of%20Cards.cpp) | 100 | 2 | Two pointers |
82 | | San Francisco | [File](./problems/san_francisco.pdf) | [C++ code](./problems-of-the-week/San%20Francisco.cpp) | 100 | 3 | DP - Graph |
83 | | Greyjoy | [File](./problems/greyjoy.pdf) | [C++ code](./problems-of-the-week/Greyjoy.cpp) | 100 | 4 | Two pointers - Sorting |
84 | | Tracking | [File](./problems/tracking.pdf) | [C++ code](./problems-of-the-week/Tracking.cpp) | 100 | 5 | Graph - Shortest Path |
85 | | Motorcycles| [File](./problems/motorcycles.pdf) | [C++ code](./problems-of-the-week/Motorcycles.cpp) | 100 | 6 | Geometry |
86 | | Octopussy | [File](./problems/octopussy.pdf) | [C++ code](./problems-of-the-week/Octopussy.cpp) | 100 | 7 | Greedy |
87 | | Augean Stable | [File](./problems/the%20augean%20stables.pdf) | [C++ code](./problems-of-the-week/Augean%20Stable.cpp) | 100 | 8 | Linear programming |
88 | | Knights | [File](./problems/knights.pdf) | [C++ code](./problems-of-the-week/Knights.cpp) | 100 | 9 | Flows |
89 | | Hands | [File](./problems/hands.pdf) | [C++ code](./problems-of-the-week/Hands.cpp) | 100 | 10 | Union find & Advanced geometry |
90 | | Ceryneian Hind | [File](./problems/ceryneian%20hind.pdf) | [C++ code](./problems-of-the-week/Ceryneian%20Hind.cpp) | 100 | 11 | Flows |
91 | | Rubeus Hagrid | [File](./problems/rubeus_hagrid.pdf) | [C++ code](./problems-of-the-week/Rubeus%20Hagrid.cpp) | 100 | 12 | Greedy |
92 | | Strikesback | [File](./problems/strikesback.pdf) | [C++ code](./problems-of-the-week/Strikesback.cpp) | 100 | 13 | Advanced geometry & Linear programming |
93 | | Ludo Bagman | [File](./problems/ludo%20bagman.pdf) | [C++ code](./problems-of-the-week/Ludo%20Bagman.cpp) | 100 | 14 | Advanced flows |
94 |
95 |
96 |
--------------------------------------------------------------------------------
/hack/Asterix the Gaul.cpp:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | #define int long
4 |
5 | using namespace std;
6 |
7 | struct Move {
8 | int d;
9 | int t;
10 | };
11 |
12 | int n,m,D,T;
13 | vector movement;
14 | vector bonus;
15 |
16 | void generate(vector&set, int from, int to, int b, Move mv) {
17 | if(mv.t > T) return;
18 | if(from == to) {
19 | set.push_back(mv);
20 | return;
21 | }
22 |
23 | Move new_move = Move{mv.d+movement[from].d+b,mv.t+movement[from].t};
24 |
25 | generate(set,from+1,to,b,new_move);
26 | generate(set,from+1,to,b,mv);
27 | }
28 |
29 | bool compute(int b) {
30 |
31 | vector subset1(0);
32 | vector subset2(0);
33 |
34 | generate(subset1,0,n/2,b,Move{0,0});
35 | generate(subset2,n/2,n,b,Move{0,0});
36 |
37 | auto comp = [](Move m1, Move m2){
38 | return m1.t < m2.t;
39 | };
40 |
41 | sort(subset1.begin(),subset1.end(),comp);
42 | sort(subset2.begin(),subset2.end(),comp);
43 |
44 | for(int i = 1; i < subset1.size();i++) subset1[i].d = max(subset1[i].d,subset1[i-1].d);
45 | for(int i = 1; i < subset2.size();i++) subset2[i].d = max(subset2[i].d,subset2[i-1].d);
46 |
47 | for(auto move : subset1) {
48 | auto move2 = lower_bound(subset2.begin(), subset2.end(), T - move.t,[&](const Move &move, int value){
49 | return move.t < value;
50 | }) - 1;
51 |
52 | if(move.d + move2->d >= D)
53 | return true;
54 | }
55 |
56 | return false;
57 | }
58 |
59 | void solve() {
60 | cin >> n >> m >> D >> T;
61 |
62 | movement = vector(n);
63 | for(int i = 0; i < n;i++) cin >> movement[i].d >> movement[i].t;
64 |
65 | bonus = vector(m+2);
66 | for(int i = 1; i <= m;i++) cin >> bonus[i];
67 | bonus.back() = 0;
68 |
69 | // Binary search over the solution space
70 | int start = 0;
71 | int end = m+2;
72 | while(start != end) {
73 | int middle = (start + end) / 2;
74 | if(compute(bonus[middle])) {
75 | end = middle;
76 | } else {
77 | start = middle+1;
78 | }
79 | }
80 |
81 | if(start == m+2) {
82 | cout << "Panoramix captured" << "\n";
83 | } else {
84 | cout << start << "\n";
85 | }
86 | }
87 |
88 | signed main() {
89 | ios_base::sync_with_stdio(false);
90 | cin.tie(0);
91 |
92 | int t;
93 | cin >> t;
94 |
95 | while(t--)
96 | solve();
97 | }
98 |
--------------------------------------------------------------------------------
/hack/DHL.cpp:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | using namespace std;
4 |
5 | void solve() {
6 |
7 | int n;
8 | cin >> n;
9 |
10 | vector a(n+1,0);
11 | vector b(n+1,0);
12 |
13 | for(int i = 1; i <= n;i++) {
14 | cin >> a[i];
15 | a[i] += a[i-1];
16 | }
17 | for(int j = 1; j <= n;j++) {
18 | cin >> b[j];
19 | b[j] += b[j-1];
20 | }
21 |
22 | vector> dp(n+1,vector(n+1,INT_MAX));
23 | dp[0][0] = 0;
24 |
25 | for(int i = 1; i <= n;i++) {
26 | for(int j = 1; j <= n;j++) {
27 | for(int k = i-1; k >= 0;k--) {
28 | for(int l = j-1; l >= 0;l--) {
29 | if(dp[k][l] != INT_MAX) {
30 | dp[i][j] = min(dp[i][j],dp[k][l] + (a[i]-a[k]-(i-k)) * (b[j]-b[l]-(j-l)) );
31 | }
32 | if((a[i]-a[k]-(i-k)) >= 50 && (b[j]-b[l]-(j-l)) >= 50) {
33 | break;
34 | }
35 | }
36 | }
37 | }
38 | }
39 |
40 | cout << dp.back().back() << "\n";
41 | }
42 |
43 | int main() {
44 |
45 | ios_base::sync_with_stdio(false);
46 | cin.tie(0);
47 |
48 | int t;
49 | cin >> t;
50 |
51 | while(t--)
52 | solve();
53 | }
54 |
--------------------------------------------------------------------------------
/hack/readme.md:
--------------------------------------------------------------------------------
1 | # Hacks
2 |
3 | ### Asterix and the Gaul
4 |
5 | This code fails the sample test but get 100 pts
6 |
7 | ### DHL
8 |
9 | The submitted code is wrong but get 99 pts (last point is a challenge)
10 |
--------------------------------------------------------------------------------
/problems-of-the-week/Augean Stable.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include
4 | #include
5 |
6 | using namespace std;
7 |
8 | typedef int IT;
9 | typedef CGAL::Gmpz ET;
10 |
11 | typedef CGAL::Quadratic_program Program;
12 | typedef CGAL::Quadratic_program_solution Solution;
13 |
14 | typedef struct stall {
15 | int x,k,l,m;
16 | } stall;
17 |
18 | const int h1 = 0;
19 | const int h2 = 1;
20 | const int h3 = 2;
21 |
22 | bool possible(const vector &stalls, int a, int p) {
23 |
24 | Program lp(CGAL::SMALLER, true, 0, true, 1);
25 |
26 | for(int i = 0; i < stalls.size();i++) {
27 | lp.set_a(h1,i,-stalls[i].k - a*a);
28 | lp.set_a(h2,i,-stalls[i].l - p*p);
29 | lp.set_a(h3,i,-stalls[i].m - a*p);
30 | lp.set_b(i,-stalls[i].x);
31 | }
32 |
33 | Solution s = CGAL::solve_linear_program(lp, ET());
34 | return !s.is_infeasible();
35 | }
36 |
37 | void solve() {
38 |
39 | int n;
40 | cin >> n;
41 |
42 | vector stalls(n);
43 |
44 | for(int i = 0; i < n;i++) {
45 | int f,c,k,l,m;
46 | cin >> f >> c >> k >> l >> m;
47 | stalls[i] = {f-c,k,l,m};
48 | }
49 |
50 | vector a(25);
51 | vector p(25);
52 |
53 | for(int i = 1; i <= 24;i++)
54 | cin >> a[i];
55 |
56 | for(int i = 1; i <= 24;i++)
57 | cin >> p[i];
58 |
59 | partial_sum(a.begin(), a.end(), a.begin());
60 | partial_sum(p.begin(), p.end(), p.begin());
61 |
62 | int best_score = INT_MAX;
63 |
64 | int left = 0;
65 | int right = 48;
66 | int bound = 24;
67 |
68 | vector cache(25,-1);
69 |
70 | while(left <= right) {
71 |
72 | int hours = (left + right + 1) / 2;
73 | bool can = false;
74 |
75 | for(int ai = max(0, hours - bound); ai <= min(hours,bound); ai++) {
76 | const int pi = hours - ai;
77 |
78 | if (cache[ai] > pi)
79 | continue;
80 |
81 | if(possible(stalls,a[ai],p[pi])) {
82 | can = true;
83 | break;
84 | }
85 | else {
86 | cache[ai] = max(cache[ai], pi);
87 | }
88 | }
89 | if(can) {
90 | best_score = hours;
91 | right = hours-1;
92 | }
93 | else {
94 | left = hours+1;
95 | }
96 | }
97 |
98 | if(best_score == INT_MAX) {
99 | cout << "Impossible!" << "\n";
100 | }
101 | else {
102 | cout << best_score << "\n";
103 | }
104 |
105 | return;
106 | }
107 |
108 | signed main() {
109 |
110 | ios_base::sync_with_stdio(false);
111 | cin.tie(NULL);
112 |
113 | int t;
114 | cin >> t;
115 |
116 | while(t--)
117 | solve();
118 |
119 | }
120 |
--------------------------------------------------------------------------------
/problems-of-the-week/Ceryneian Hind.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include
4 |
5 | using namespace std;
6 |
7 | typedef boost::adjacency_list_traits traits;
8 | typedef boost::adjacency_list>>> graph;
12 |
13 | typedef traits::vertex_descriptor vertex_desc;
14 | typedef traits::edge_descriptor edge_desc;
15 |
16 | class edge_adder {
17 | graph &G;
18 |
19 | public:
20 | explicit edge_adder(graph &G) : G(G) {}
21 |
22 | void add_edge(int from, int to,int capacity) {
23 | auto c_map = boost::get(boost::edge_capacity, G);
24 | auto r_map = boost::get(boost::edge_reverse, G);
25 | const auto e = boost::add_edge(from, to, G).first;
26 | const auto rev_e = boost::add_edge(to, from, G).first;
27 | c_map[e] = capacity;
28 | c_map[rev_e] = 0;
29 | r_map[e] = rev_e;
30 | r_map[rev_e] = e;
31 | }
32 | };
33 |
34 | void solve() {
35 |
36 | int n,m;
37 | cin >> n >> m;
38 |
39 | vector scores = vector(n);
40 | for(int i = 0; i < n;i++) cin >> scores[i];
41 |
42 | int sum_positive = accumulate(scores.begin(),scores.end(),0,[](int a, int b) -> int {
43 | return max(a,0)+max(b,0);
44 | });
45 |
46 | graph G(n);
47 | edge_adder adder(G);
48 |
49 | auto v_source = boost::add_vertex(G);
50 | auto v_sink = boost::add_vertex(G);
51 |
52 | for(int i = 0; i < m;i++) {
53 | int u,v;
54 | cin >> u >> v;
55 | adder.add_edge(u,v,sum_positive);
56 | }
57 |
58 | for(int i = 0; i < n;i++) {
59 | if(scores[i] >= 0) {
60 | adder.add_edge(v_source,i,scores[i]);
61 | } else {
62 | adder.add_edge(i,v_sink,abs(scores[i]));
63 | }
64 | }
65 |
66 | int flow = boost::push_relabel_max_flow(G,v_source,v_sink);
67 | int ans = sum_positive - flow;
68 |
69 | if(ans == 0) {
70 | cout << "impossible" << "\n";
71 | } else {
72 | cout << ans << "\n";
73 | }
74 | }
75 |
76 | int main() {
77 |
78 | ios_base::sync_with_stdio(false);
79 | cin.tie(0);
80 |
81 | int t; cin >> t;
82 | while(t--)
83 | solve();
84 |
85 | return 0;
86 | }
87 |
--------------------------------------------------------------------------------
/problems-of-the-week/Deck of Cards.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include
4 | #include
5 | #include
6 | using namespace std;
7 |
8 |
9 | void solve() {
10 |
11 | int n,k;
12 | cin >> n >> k;
13 |
14 | vector a(0);
15 |
16 | for(int i = 0; i < n;i++) {
17 | int tmp;
18 | cin >> tmp;
19 | a.push_back(tmp);
20 | }
21 |
22 | int left = -1;
23 | int right = 0;
24 |
25 | int best = INT_MAX;
26 | int l1 = -1,l2 = -1;
27 |
28 | int sum = 0;
29 |
30 | while(left != n-1) {
31 |
32 | left++;
33 | sum += a[left];
34 |
35 | if(abs(sum - k) < best) {
36 | best = abs(sum - k);
37 | l1 = right;
38 | l2 = left;
39 | }
40 |
41 | while(left != right && sum - k > 0) {
42 |
43 | sum -= a[right];
44 | right++;
45 |
46 | if(abs(sum - k) < best) {
47 | best = abs(sum - k);
48 | l1 = right;
49 | l2 = left;
50 | }
51 | }
52 |
53 | }
54 |
55 | cout << l1 << " " << l2 << "\n";
56 | }
57 |
58 | int main() {
59 |
60 | int t;
61 | cin >> t;
62 |
63 | while(t--)
64 | solve();
65 |
66 | return 0;
67 | }
68 |
--------------------------------------------------------------------------------
/problems-of-the-week/Greyjoy.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include
4 |
5 | using namespace std;
6 |
7 | #define int long long
8 |
9 | typedef struct {
10 | int value;
11 | int position;
12 | int way;
13 | } island;
14 |
15 | void solve() {
16 |
17 | int n,k,w;
18 | cin >> n >> k >> w;
19 |
20 | vector c(n);
21 | for(int i = 0; i < n;i++) {
22 | cin >> c[i];
23 | }
24 |
25 | vector> ways(w,vector(0));
26 | vector islands(0);
27 |
28 | for(int i = 0; i < w;i++) {
29 | int l;
30 | cin >> l;
31 | vector tmp(l,0);
32 | int sum = 0;
33 | for(int j = 0; j < l;j++) {
34 | cin >> tmp[j];
35 | if(j != 0) {
36 | sum += c[tmp[j]];
37 | islands.push_back({sum,j,i});
38 | }
39 | }
40 | ways[i] = tmp;
41 | }
42 |
43 | int best = 0;
44 |
45 | for(int i = 0; i < w;i++) {
46 |
47 | int start = -1;
48 | int end = 0;
49 | int score = 0;
50 |
51 | int length = ways[i].size();
52 |
53 | while(start < length - 1) {
54 |
55 | start++;
56 | score += c[ways[i][start]];
57 |
58 | while(score > k) {
59 | score -= c[ways[i][end]];
60 | end++;
61 | }
62 |
63 | if(score == k) {
64 | best = max(best,start - end + 1);
65 | }
66 | }
67 | }
68 |
69 | sort(islands.begin(),islands.end(),[](island a, island b) -> bool {
70 | return a.value < b.value;
71 | });
72 |
73 | vector> is(1,{islands[0]});
74 |
75 | for(int i = 1; i < islands.size();i++) {
76 | if(islands[i].value == islands[i-1].value) {
77 | is.back().push_back(islands[i]);
78 | } else{
79 | is.push_back({islands[i]});
80 | }
81 | }
82 |
83 | int left = 0;
84 | int right = is.size()-1;
85 |
86 | int goal = k - c[0];
87 |
88 | while(left <= right) {
89 | if(is[left][0].value + is[right][0].value < goal) {
90 | left++;
91 | }
92 | else if(is[left][0].value + is[right][0].value > goal) {
93 | right--;
94 | }
95 | else {
96 | for(auto i1 : is[left]) {
97 | for(auto i2 : is[right]) {
98 | if(i1.way != i2.way && i1.value + i2.value == goal) {
99 | best = max(best,i1.position + i2.position + 1);
100 | }
101 | }
102 | }
103 | left++;
104 | right--;
105 | }
106 | }
107 |
108 | cout << best << endl;
109 | }
110 |
111 | signed main() {
112 |
113 | int t;
114 | cin >> t;
115 |
116 | while(t--)
117 | solve();
118 | }
119 |
--------------------------------------------------------------------------------
/problems-of-the-week/Hands.cpp:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | #include
4 | #include
5 | #include
6 | #include
7 |
8 | #include
9 |
10 | #define int long
11 |
12 | typedef CGAL::Exact_predicates_inexact_constructions_kernel K;
13 | typedef CGAL::Triangulation_vertex_base_with_info_2 Vb;
14 | typedef CGAL::Triangulation_face_base_2 Fb;
15 | typedef CGAL::Triangulation_data_structure_2 Tds;
16 | typedef CGAL::Delaunay_triangulation_2 Triangulation;
17 | typedef CGAL::Point_2 Point;
18 |
19 | using namespace std;
20 |
21 | typedef struct Edge {
22 | int x;
23 | int y;
24 | int w;
25 | } Edge;
26 |
27 | int maxFamilies(vector &c, int k) {
28 |
29 | int o = c[k];
30 |
31 | if(k == 4) {
32 | if(c[3] <= c[1]) {
33 | o += c[3];
34 | o += ((c[1]-c[3])/2+c[2]) / 2;
35 | }
36 | else{
37 | o += c[1];
38 | o += ((c[3]-c[1])+c[2]) / 2;
39 | }
40 | }
41 |
42 | else if(k == 3) {
43 | if(c[2] <= c[1]) {
44 | o += c[2];
45 | o += (c[1] - c[2]) / 3;
46 | }
47 | else {
48 | o += c[1];
49 | o += (c[2] - c[1]) / 2;
50 | }
51 | }
52 |
53 | else if(k == 2) {
54 | o += c[1] / 2;
55 | }
56 |
57 | return o;
58 | }
59 | void solve() {
60 |
61 | int n,k,f0,s0;
62 | cin >> n >> k >> f0 >> s0;
63 |
64 | vector> points(n);
65 | for(int i = 0; i < n;i++) {
66 | int x,y;
67 | cin >> x >> y;
68 | points[i] = {Point(x,y),i};
69 | }
70 |
71 | Triangulation t;
72 | t.insert(points.begin(),points.end());
73 |
74 | vector edges(0);
75 | for(auto e = t.finite_edges_begin(); e != t.finite_edges_end(); ++e) {
76 | int p1 = e->first->vertex((e->second + 1) % 3)->info();
77 | int p2 = e->first->vertex((e->second + 2) % 3)->info();
78 | if(p1 > p2) swap(p1,p2);
79 | edges.push_back({p1,p2, long(t.segment(e).squared_length())});
80 | }
81 |
82 | sort(edges.begin(),edges.end(),[](const Edge &e1, const Edge &e2) -> bool {
83 | return e1.w < e2.w;
84 | });
85 |
86 | boost::disjoint_sets_with_storage<> uf(n);
87 |
88 | int n_components = n;
89 |
90 | vector sizeComponent(n,1);
91 | vector componentOfSize(k+1,0);
92 | componentOfSize[1] = n;
93 |
94 | int s_sol = 0;
95 | int f_sol = 0;
96 |
97 | for(auto e : edges) {
98 | // Get Index
99 | int c1 = uf.find_set(e.x);
100 | int c2 = uf.find_set(e.y);
101 | // Ignore if they are similar
102 | if(c1 == c2) continue;
103 |
104 | int fMax = maxFamilies(componentOfSize,k);
105 |
106 | // Case 1
107 | if(fMax >= f0) {
108 | s_sol = e.w;
109 | }
110 | // Case 2
111 | if(e.w >= s0) {
112 | f_sol = max(fMax,f_sol);
113 | }
114 | // Update the union find
115 | uf.link(c1,c2);
116 | int c = uf.find_set(c1);
117 |
118 | componentOfSize[sizeComponent[c1]]--;
119 | componentOfSize[sizeComponent[c2]]--;
120 | sizeComponent[c] = min(k,sizeComponent[c1] + sizeComponent[c2]);
121 | componentOfSize[sizeComponent[c]] += 1;
122 |
123 | n_components--;
124 | if(n_components == 1) break;
125 | }
126 |
127 | f_sol = max(maxFamilies(componentOfSize,k),f_sol);
128 |
129 | cout << s_sol << " " << f_sol << "\n";
130 |
131 | return;
132 | }
133 |
134 | signed main() {
135 |
136 | ios_base::sync_with_stdio(false);
137 | cin.tie(0);
138 |
139 | int t;
140 | cin >> t;
141 |
142 | while(t--)
143 | solve();
144 |
145 | return 0;
146 | }
147 |
--------------------------------------------------------------------------------
/problems-of-the-week/Knights.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include
4 |
5 | #include
6 | #include
7 |
8 | using namespace std;
9 |
10 | #define int long
11 |
12 | typedef boost::adjacency_list_traits traits;
13 | typedef boost::adjacency_list>>> graph;
17 |
18 | typedef traits::vertex_descriptor vertex_desc;
19 | typedef traits::edge_descriptor edge_desc;
20 |
21 | class edge_adder {
22 |
23 | graph &G;
24 |
25 | public:
26 | explicit edge_adder(graph &G) : G(G) {}
27 |
28 | void add_edge(int from, int to, int weight) {
29 | auto c_map = boost::get(boost::edge_capacity, G);
30 | auto r_map = boost::get(boost::edge_reverse, G);
31 | const auto e = boost::add_edge(from, to, G).first;
32 | const auto rev_e = boost::add_edge(to, from, G).first;
33 | c_map[e] = weight;
34 | c_map[rev_e] = 0;
35 | r_map[e] = rev_e;
36 | r_map[rev_e] = e;
37 | }
38 | };
39 |
40 | typedef struct Point {
41 | int x;
42 | int y;
43 | } Point;
44 |
45 | void solve() {
46 |
47 | int column,row,k,c;
48 | cin >> column >> row >> k >> c;
49 |
50 | vector positions(k);
51 | for(int i = 0; i < k;i++) {
52 | int x,y; cin >> x >> y;
53 | positions[i] = {x,y};
54 | }
55 |
56 | if(column*row == 0) {
57 | cout << 0 << "\n";
58 | return;
59 | }
60 |
61 | graph G(2*column*row);
62 | edge_adder adder(G);
63 |
64 | const vertex_desc v_source = boost::add_vertex(G);
65 | const vertex_desc v_sink = boost::add_vertex(G);
66 |
67 | const int size = row * column;
68 |
69 | // Add bind in and out in intersections
70 | for(int i = 0; i < row*column;i++) {
71 | adder.add_edge(i,i+size,c);
72 | }
73 |
74 | // Bind corridors with intersections
75 | for(int i = 0; i < row-1;i++) {
76 | for(int j = 0; j < column;j++) {
77 | const int i1 = i*column + j;
78 | const int i2 = (i+1)*column + j;
79 | adder.add_edge(i1 + size, i2, 1);
80 | adder.add_edge(i2 + size, i1, 1);
81 | }
82 | }
83 |
84 | for(int j = 0; j < row;j++) {
85 | for(int i = 0; i < column-1;i++) {
86 | const int i1 = j*column + i;
87 | const int i2 = j*column + i + 1;
88 | adder.add_edge(i1 + size, i2, 1);
89 | adder.add_edge(i2 + size, i1, 1);
90 | }
91 | }
92 |
93 | // Bind corridors with sink
94 | for(int i = 0; i < row;i++) {
95 | adder.add_edge(i*column + size,v_sink,1);
96 | adder.add_edge(i*column + column-1 + size,v_sink,1);
97 | }
98 |
99 | for(int i = 0; i < column;i++) {
100 | adder.add_edge(i + size,v_sink,1);
101 | adder.add_edge(column*(row-1) + i + size,v_sink,1);
102 | }
103 |
104 | // Bind starting points for knights
105 | for(int i = 0; i < k;i++) {
106 | adder.add_edge(v_source,positions[i].y * column + positions[i].x,1);
107 | }
108 |
109 | // Get the max flow & output it
110 | long flow = boost::push_relabel_max_flow(G, v_source, v_sink);
111 | cout << flow << "\n";
112 |
113 | return;
114 | }
115 |
116 | signed main() {
117 |
118 | int t;
119 | cin >> t;
120 |
121 | while(t--)
122 | solve();
123 |
124 | return 0;
125 | }
126 |
--------------------------------------------------------------------------------
/problems-of-the-week/Ludo Bagman.cpp:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | #include
4 | #include
5 | #include
6 | #include
7 | #include
8 |
9 | using namespace std;
10 |
11 | typedef boost::adjacency_list_traits traits;
12 | typedef boost::adjacency_list > > > > graph;
17 |
18 | typedef boost::graph_traits::edge_descriptor edge_desc;
19 | typedef boost::graph_traits::out_edge_iterator out_edge_it;
20 | typedef traits::vertex_descriptor vertex_desc;
21 |
22 |
23 | class edge_adder {
24 | graph &G;
25 |
26 | public:
27 | explicit edge_adder(graph &G) : G(G) {}
28 | void add_edge(int from, int to, long capacity, long cost) {
29 | auto c_map = boost::get(boost::edge_capacity, G);
30 | auto r_map = boost::get(boost::edge_reverse, G);
31 | auto w_map = boost::get(boost::edge_weight, G);
32 | const edge_desc e = boost::add_edge(from, to, G).first;
33 | const edge_desc rev_e = boost::add_edge(to, from, G).first;
34 | c_map[e] = capacity;
35 | c_map[rev_e] = 0;
36 | r_map[e] = rev_e;
37 | r_map[rev_e] = e;
38 | w_map[e] = cost;
39 | w_map[rev_e] = -cost;
40 | }
41 | };
42 |
43 | struct Match {
44 | int u;
45 | int v;
46 | int r;
47 | };
48 |
49 | void solve() {
50 |
51 | int e,w,m,d,p,l;
52 | cin >> e >> w >> m >> d >> p >> l;
53 |
54 | vector matches = vector(m+d);
55 | for(int i = 0; i < m + d;i++) {
56 | cin >> matches[i].u;
57 | cin >> matches[i].v;
58 | cin >> matches[i].r;
59 | }
60 |
61 | graph G(2*(e+w));
62 | edge_adder adder(G);
63 |
64 | vertex_desc v_source = boost::add_vertex(G);
65 | vertex_desc v_source_2 = boost::add_vertex(G);
66 | vertex_desc v_sink = boost::add_vertex(G);
67 | vertex_desc v_sink_2 = boost::add_vertex(G);
68 |
69 | unsigned int offset = e + w;
70 | unsigned int max_difficulty = 1e4;
71 |
72 | // Start
73 | adder.add_edge(v_source,v_source_2,p - e*l,0);
74 | for(int i = 0; i < e;i++) adder.add_edge(v_source,i,l,0);
75 | for(int i = 0; i < e;i++) adder.add_edge(v_source_2,i,INT_MAX,0);
76 | for(int i = 0; i < e;i++) adder.add_edge(v_source_2,i+offset,INT_MAX,0);
77 |
78 | // Middle
79 | for(int i = 0; i < m;i++) adder.add_edge(matches[i].u, e + matches[i].v, 1, matches[i].r);
80 | for(int i = m; i < m+d;i++) adder.add_edge(offset + matches[i].u, offset + e + matches[i].v, 1, matches[i].r);
81 | for(int i = e; i < e + w;i++) adder.add_edge(i, offset + i, INT_MAX, 0);
82 |
83 | // End
84 | for(int i = e; i < e + w;i++) adder.add_edge(i,v_sink,l,0);
85 | for(int i = e; i < e + w;i++) adder.add_edge(offset + i,v_sink_2,INT_MAX,0);
86 |
87 | adder.add_edge(v_sink_2,v_sink,p - w*l,0);
88 |
89 | int flow = boost::push_relabel_max_flow(G, v_source, v_sink);
90 | if(flow != p || p < l*max(e,w)) {
91 | cout << "No schedule!" << "\n";
92 | } else {
93 | boost::successive_shortest_path_nonnegative_weights(G, v_source, v_sink);
94 | int cost = boost::find_flow_cost(G);
95 | cout << cost << "\n";
96 | }
97 |
98 | return;
99 | }
100 |
101 | int main() {
102 |
103 | ios_base::sync_with_stdio(false);
104 | cin.tie(0);
105 |
106 | int t;
107 | cin >> t;
108 |
109 | while(t--)
110 | solve();
111 |
112 | return 0;
113 | }
114 |
--------------------------------------------------------------------------------
/problems-of-the-week/Motorcycles.cpp:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | #include
4 | #include
5 | #include
6 | #include
7 | #include
8 |
9 | typedef CGAL::Exact_predicates_exact_constructions_kernel K;
10 | typedef CGAL::Ray_2 Ray;
11 | typedef CGAL::Point_2 Point;
12 | typedef CGAL::Line_2 Line;
13 |
14 | using namespace std;
15 |
16 | typedef struct Biker {
17 | Ray r;
18 | Line l;
19 | int index;
20 | long int y0;
21 | } Biker;
22 |
23 |
24 | void solve() {
25 |
26 | int n;
27 | cin >> n;
28 |
29 | vector motos(0);
30 |
31 | for(int i = 0; i < n;i++) {
32 | long y0,x1,y1;
33 | cin >> y0 >> x1 >> y1;
34 | Line l;
35 | if(y0 < y1) {
36 | l = Line(Point(0,y0),Point(x1,y1));
37 | }
38 | else {
39 | l = Line(Point(0,-y0),Point(x1,-y1));
40 | }
41 | Ray r = Ray(Point(0,y0),Point(x1,y1));
42 | motos.push_back({r,l,i,y0});
43 | }
44 |
45 | sort(motos.begin(),motos.end(),[](const Biker &b1, const Biker &b2) -> bool {
46 | return b1.y0 > b2.y0;
47 | });
48 |
49 | vector sol(n,true);
50 |
51 | for(int i = 1; i < n;i++) {
52 |
53 | for(int j = i-1; j >= 0;j--) {
54 | if(!sol[motos[j].index]) {
55 | continue;
56 | }
57 | if(!CGAL::do_intersect(motos[i].r,motos[j].r)){
58 | break;
59 | }
60 |
61 | auto result = CGAL::compare_slope(motos[i].l, motos[j].l);
62 | if (result == CGAL::Comparison_result::LARGER) {
63 | sol[motos[i].index] = false;
64 | break;
65 | }
66 | else {
67 | sol[motos[j].index] = false;
68 | }
69 | }
70 | }
71 |
72 | for(int i = 0; i < sol.size();i++) {
73 | if(sol[i])
74 | cout << i << " ";
75 | }
76 | cout << "\n";
77 |
78 | return;
79 | }
80 |
81 | signed main() {
82 | ios_base::sync_with_stdio(false);
83 | cin.tie(0);
84 |
85 | int t;
86 | cin >> t;
87 |
88 | while(t--)
89 | solve();
90 |
91 | return 0;
92 | }
93 |
--------------------------------------------------------------------------------
/problems-of-the-week/Octopussy.cpp:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | using namespace std;
4 |
5 | void solve() {
6 | int n; cin >> n;
7 |
8 | vector t(n);
9 | for(int i = 0; i < n;i++)
10 | cin >> t[i];
11 |
12 | for(int i = 1; i < n;i++) {
13 | int parent = (i - 1) / 2;
14 | t[i] = min(t[i],t[parent]-1);
15 | }
16 |
17 | sort(t.begin(),t.end());
18 |
19 | for(int i = 0; i < n;i++) {
20 | if(t[i] <= i) {
21 | cout << "no" << "\n";
22 | return;
23 | }
24 | }
25 |
26 | cout << "yes" << "\n";
27 | return;
28 | }
29 |
30 | signed main() {
31 |
32 | int t;
33 | cin >> t;
34 |
35 | while(t--)
36 | solve();
37 |
38 | return 0;
39 | }
40 |
--------------------------------------------------------------------------------
/problems-of-the-week/Rubeus Hagrid.cpp:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | #define int long
4 |
5 | using namespace std;
6 |
7 | struct Edge {
8 | int to;
9 | int time;
10 | };
11 |
12 | struct Vertex {
13 | int t;
14 | int s;
15 | };
16 |
17 | vector g;
18 | vector> graph;
19 | vector ver;
20 |
21 | int totalValue;
22 | int timeSpent;
23 |
24 | void traverse(int idx) {
25 | ver[idx].t = 0;
26 | ver[idx].s = 1;
27 |
28 | for(Edge e : graph[idx]) {
29 | traverse(e.to);
30 | ver[idx].t += 2*e.time + ver[e.to].t;
31 | ver[idx].s += ver[e.to].s;
32 | }
33 |
34 | sort(graph[idx].begin(),graph[idx].end(),[](const Edge &e1, const Edge &e2) -> bool {
35 | int lost1 = ver[e1.to].s * (ver[e2.to].t + 2*e2.time);
36 | int lost2 = ver[e2.to].s * (ver[e1.to].t + 2*e1.time);
37 | return lost2 < lost1;
38 | });
39 | }
40 |
41 | void traverse2(int idx) {
42 | totalValue -= timeSpent;
43 |
44 | for(Edge e : graph[idx]) {
45 | timeSpent += e.time;
46 | traverse2(e.to);
47 | timeSpent += e.time;
48 | }
49 | }
50 |
51 |
52 | void solve() {
53 | int n; cin >> n;
54 |
55 | g = vector(n+1,0l);
56 | for(int i = 1; i <= n;i++) cin >> g[i];
57 |
58 | graph = vector>(n+1,vector(0));
59 | for(int i = 0; i < n;i++) {
60 | int u,v,l;
61 | cin >> u >> v >> l;
62 | graph[u].push_back({v,l});
63 | }
64 |
65 | ver = vector(n+1);
66 |
67 | timeSpent = 0;
68 | totalValue = accumulate(g.begin(),g.end(),0l);
69 |
70 | traverse(0);
71 | traverse2(0);
72 |
73 | cout << totalValue << "\n";
74 | }
75 |
76 | signed main() {
77 | ios_base::sync_with_stdio(false);
78 | cin.tie(0);
79 |
80 | int t; cin >> t;
81 |
82 | while(t--)
83 | solve();
84 | }
85 |
--------------------------------------------------------------------------------
/problems-of-the-week/San Francisco.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include
4 | #include
5 |
6 | #define int long long
7 |
8 | using namespace std;
9 |
10 | void solve() {
11 |
12 | int n,m,x,k;
13 | cin >> n >> m >> x >> k;
14 |
15 | vector>> graph = vector>>(n,vector>(0));
16 | vector hasOutgoing = vector(n,false);
17 |
18 | for(int i = 0; i < m;i++) {
19 | int from,to,reward;
20 | cin >> from >> to >> reward;
21 | graph[to].push_back(make_pair(from,reward));
22 | hasOutgoing[from] = true;
23 | }
24 |
25 | vector> dp = vector>(k+1,vector(n,LLONG_MIN));
26 | dp[0][0] = 0;
27 |
28 | for(int step = 1; step <= k;step++) {
29 |
30 | for(int i = 1; i < n;i++) {
31 | for(auto transition : graph[i]) {
32 | int from = transition.first;
33 | int reward = transition.second;
34 | dp[step][i] = max(dp[step][i],dp[step-1][from] + reward);
35 | }
36 |
37 | if(dp[step][i] >= x) {
38 | cout << step << "\n";
39 | return;
40 | }
41 | }
42 |
43 | for(int i = 1; i < n;i++) {
44 | if(!hasOutgoing[i]) {
45 | dp[step][0] = max(dp[step][0],dp[step][i]);
46 | }
47 | }
48 | }
49 |
50 | cout << "Impossible" << "\n";
51 |
52 | return;
53 | }
54 |
55 | signed main() {
56 |
57 | ios_base::sync_with_stdio(false);
58 | cin.tie(NULL);
59 |
60 | int t;
61 | cin >> t;
62 |
63 | while(t--)
64 | solve();
65 |
66 | return 0;
67 | }
68 |
--------------------------------------------------------------------------------
/problems-of-the-week/Strikesback.cpp:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | #include
4 | #include
5 | #include
6 | #include
7 | #include
8 |
9 | #define int long
10 |
11 | using namespace std;
12 |
13 | typedef CGAL::Exact_predicates_inexact_constructions_kernel K;
14 | typedef CGAL::Delaunay_triangulation_2 Triangulation;
15 | typedef CGAL::Point_2 Point;
16 |
17 | typedef CGAL::Gmpq IT;
18 | typedef CGAL::Gmpq ET;
19 |
20 | typedef CGAL::Quadratic_program Program;
21 | typedef CGAL::Quadratic_program_solution Solution;
22 |
23 | struct Positions {
24 | Point p;
25 | double max_dist;
26 | };
27 |
28 | struct Asteroid {
29 | Point p;
30 | int d;
31 | };
32 |
33 | void solve() {
34 | int a,s,b;
35 | long e;
36 | cin >> a >> s >> b;
37 | cin >> e;
38 |
39 | vector asteroids = vector(a);
40 | vector positions = vector(s);
41 | vector hunters = vector(b);
42 |
43 | for(int i = 0; i < a; i++) {
44 | int x,y,d;
45 | cin >> x >> y >> d;
46 | asteroids[i] = Asteroid{Point(x,y),d};
47 | }
48 |
49 | for(int i = 0; i < s; i++) {
50 | int x,y;
51 | cin >> x >> y;
52 | positions[i] = {Point(x,y),DBL_MAX};
53 | }
54 |
55 | for(int i = 0; i < b; i++) {
56 | int x,y;
57 | cin >> x >> y;
58 | hunters[i] = Point(x,y);
59 | }
60 |
61 | Triangulation t;
62 | t.insert(hunters.begin(), hunters.end());
63 |
64 | if(b > 0) {
65 | for(int i = 0; i < s;i++) {
66 | Point tmp = t.nearest_vertex(positions[i].p)->point();
67 | positions[i].max_dist = squared_distance(tmp,positions[i].p);
68 | }
69 | }
70 |
71 | Program lp(CGAL::SMALLER, true, 0, false);
72 |
73 | for(int i = 0; i < a;i++) {
74 | for(int j = 0; j < s;j++) {
75 | if(positions[j].max_dist >= CGAL::squared_distance(positions[j].p, asteroids[i].p)) {
76 | lp.set_a(j,i,-CGAL::Gmpq(1, max(K::FT(1), CGAL::squared_distance(positions[j].p, asteroids[i].p))));
77 | }
78 | }
79 | lp.set_b(i,-asteroids[i].d);
80 | }
81 |
82 | for(int j = 0; j < s;j++) {
83 | lp.set_a(j,a,1);
84 | }
85 | lp.set_b(a,e);
86 |
87 | Solution solution = CGAL::solve_linear_program(lp, ET());
88 | if (solution.is_infeasible()) {
89 | cout << "n" << "\n";
90 | } else {
91 | cout << "y" << "\n";
92 | }
93 | }
94 |
95 | signed main() {
96 | ios_base::sync_with_stdio(false);
97 | cin.tie(0);
98 |
99 | int t;
100 | cin >> t;
101 |
102 | while(t--)
103 | solve();
104 | }
105 |
--------------------------------------------------------------------------------
/problems-of-the-week/Tracking.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include
4 |
5 | typedef boost::adjacency_list > graph;
7 |
8 | using namespace std;
9 |
10 | int dijkstra_dist(const graph &G, int s, int t) {
11 | int n = boost::num_vertices(G);
12 | std::vector dist_map(n);
13 |
14 | boost::dijkstra_shortest_paths(G, s,
15 | boost::distance_map(boost::make_iterator_property_map(
16 | dist_map.begin(), boost::get(boost::vertex_index, G))));
17 |
18 | return dist_map[t];
19 | }
20 |
21 |
22 | void solve() {
23 | int n,m,k,x,y;
24 | cin >> n >> m >> k >> x >> y;
25 |
26 | graph G(n*(k+1));
27 |
28 | for(int i = 0; i < m;i++) {
29 | int a,b,c,d;
30 | cin >> a >> b >> c >> d;
31 |
32 | for(int j = 0; j <= k; j++) {
33 | boost::add_edge(j*n+a ,min(j+d,k)*n + b,c,G);
34 | boost::add_edge(j*n+b ,min(j+d,k)*n + a,c,G);
35 | }
36 | }
37 |
38 | cout << dijkstra_dist(G,x,k*n + y) << "\n";
39 | }
40 |
41 | signed main() {
42 | ios_base::sync_with_stdio(false);
43 | cin.tie(0);
44 |
45 | int t;
46 | cin >> t;
47 |
48 | while(t--)
49 | solve();
50 | }
51 |
--------------------------------------------------------------------------------
/problems/algocoön group.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/francois141/algo-lab/8c5980fc979bb496b69565b18432dfc30198b408/problems/algocoön group.pdf
--------------------------------------------------------------------------------
/problems/ant challenge.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/francois141/algo-lab/8c5980fc979bb496b69565b18432dfc30198b408/problems/ant challenge.pdf
--------------------------------------------------------------------------------
/problems/antenna.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/francois141/algo-lab/8c5980fc979bb496b69565b18432dfc30198b408/problems/antenna.pdf
--------------------------------------------------------------------------------
/problems/asterix and the chariot race.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/francois141/algo-lab/8c5980fc979bb496b69565b18432dfc30198b408/problems/asterix and the chariot race.pdf
--------------------------------------------------------------------------------
/problems/asterix in switzerland.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/francois141/algo-lab/8c5980fc979bb496b69565b18432dfc30198b408/problems/asterix in switzerland.pdf
--------------------------------------------------------------------------------
/problems/asterix_the_gaul.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/francois141/algo-lab/8c5980fc979bb496b69565b18432dfc30198b408/problems/asterix_the_gaul.pdf
--------------------------------------------------------------------------------
/problems/beach_bars.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/francois141/algo-lab/8c5980fc979bb496b69565b18432dfc30198b408/problems/beach_bars.pdf
--------------------------------------------------------------------------------
/problems/bistro.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/francois141/algo-lab/8c5980fc979bb496b69565b18432dfc30198b408/problems/bistro.pdf
--------------------------------------------------------------------------------
/problems/boats.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/francois141/algo-lab/8c5980fc979bb496b69565b18432dfc30198b408/problems/boats.pdf
--------------------------------------------------------------------------------
/problems/buddy selection.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/francois141/algo-lab/8c5980fc979bb496b69565b18432dfc30198b408/problems/buddy selection.pdf
--------------------------------------------------------------------------------
/problems/build_the_sum.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/francois141/algo-lab/8c5980fc979bb496b69565b18432dfc30198b408/problems/build_the_sum.pdf
--------------------------------------------------------------------------------
/problems/burning_coins.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/francois141/algo-lab/8c5980fc979bb496b69565b18432dfc30198b408/problems/burning_coins.pdf
--------------------------------------------------------------------------------
/problems/car sharing.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/francois141/algo-lab/8c5980fc979bb496b69565b18432dfc30198b408/problems/car sharing.pdf
--------------------------------------------------------------------------------
/problems/casino royale.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/francois141/algo-lab/8c5980fc979bb496b69565b18432dfc30198b408/problems/casino royale.pdf
--------------------------------------------------------------------------------
/problems/ceryneian hind.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/francois141/algo-lab/8c5980fc979bb496b69565b18432dfc30198b408/problems/ceryneian hind.pdf
--------------------------------------------------------------------------------
/problems/clues.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/francois141/algo-lab/8c5980fc979bb496b69565b18432dfc30198b408/problems/clues.pdf
--------------------------------------------------------------------------------
/problems/coin cossing tournament.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/francois141/algo-lab/8c5980fc979bb496b69565b18432dfc30198b408/problems/coin cossing tournament.pdf
--------------------------------------------------------------------------------
/problems/deck_of_cards.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/francois141/algo-lab/8c5980fc979bb496b69565b18432dfc30198b408/problems/deck_of_cards.pdf
--------------------------------------------------------------------------------
/problems/dhl.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/francois141/algo-lab/8c5980fc979bb496b69565b18432dfc30198b408/problems/dhl.pdf
--------------------------------------------------------------------------------
/problems/diet.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/francois141/algo-lab/8c5980fc979bb496b69565b18432dfc30198b408/problems/diet.pdf
--------------------------------------------------------------------------------
/problems/dominoes.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/francois141/algo-lab/8c5980fc979bb496b69565b18432dfc30198b408/problems/dominoes.pdf
--------------------------------------------------------------------------------
/problems/even_matrices.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/francois141/algo-lab/8c5980fc979bb496b69565b18432dfc30198b408/problems/even_matrices.pdf
--------------------------------------------------------------------------------
/problems/even_pairs.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/francois141/algo-lab/8c5980fc979bb496b69565b18432dfc30198b408/problems/even_pairs.pdf
--------------------------------------------------------------------------------
/problems/evolution.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/francois141/algo-lab/8c5980fc979bb496b69565b18432dfc30198b408/problems/evolution.pdf
--------------------------------------------------------------------------------
/problems/first steps with bgl.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/francois141/algo-lab/8c5980fc979bb496b69565b18432dfc30198b408/problems/first steps with bgl.pdf
--------------------------------------------------------------------------------
/problems/firsthit.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/francois141/algo-lab/8c5980fc979bb496b69565b18432dfc30198b408/problems/firsthit.pdf
--------------------------------------------------------------------------------
/problems/fleetrace.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/francois141/algo-lab/8c5980fc979bb496b69565b18432dfc30198b408/problems/fleetrace.pdf
--------------------------------------------------------------------------------
/problems/germs.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/francois141/algo-lab/8c5980fc979bb496b69565b18432dfc30198b408/problems/germs.pdf
--------------------------------------------------------------------------------
/problems/greyjoy.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/francois141/algo-lab/8c5980fc979bb496b69565b18432dfc30198b408/problems/greyjoy.pdf
--------------------------------------------------------------------------------
/problems/h1n1.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/francois141/algo-lab/8c5980fc979bb496b69565b18432dfc30198b408/problems/h1n1.pdf
--------------------------------------------------------------------------------
/problems/hands.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/francois141/algo-lab/8c5980fc979bb496b69565b18432dfc30198b408/problems/hands.pdf
--------------------------------------------------------------------------------
/problems/hiking-maps.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/francois141/algo-lab/8c5980fc979bb496b69565b18432dfc30198b408/problems/hiking-maps.pdf
--------------------------------------------------------------------------------
/problems/hit.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/francois141/algo-lab/8c5980fc979bb496b69565b18432dfc30198b408/problems/hit.pdf
--------------------------------------------------------------------------------
/problems/idefix and the mansion of the gods.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/francois141/algo-lab/8c5980fc979bb496b69565b18432dfc30198b408/problems/idefix and the mansion of the gods.pdf
--------------------------------------------------------------------------------
/problems/important bridges.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/francois141/algo-lab/8c5980fc979bb496b69565b18432dfc30198b408/problems/important bridges.pdf
--------------------------------------------------------------------------------
/problems/inball.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/francois141/algo-lab/8c5980fc979bb496b69565b18432dfc30198b408/problems/inball.pdf
--------------------------------------------------------------------------------
/problems/knights.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/francois141/algo-lab/8c5980fc979bb496b69565b18432dfc30198b408/problems/knights.pdf
--------------------------------------------------------------------------------
/problems/light the stage.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/francois141/algo-lab/8c5980fc979bb496b69565b18432dfc30198b408/problems/light the stage.pdf
--------------------------------------------------------------------------------
/problems/london.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/francois141/algo-lab/8c5980fc979bb496b69565b18432dfc30198b408/problems/london.pdf
--------------------------------------------------------------------------------
/problems/lord voldemort.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/francois141/algo-lab/8c5980fc979bb496b69565b18432dfc30198b408/problems/lord voldemort.pdf
--------------------------------------------------------------------------------
/problems/ludo bagman.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/francois141/algo-lab/8c5980fc979bb496b69565b18432dfc30198b408/problems/ludo bagman.pdf
--------------------------------------------------------------------------------
/problems/marathon.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/francois141/algo-lab/8c5980fc979bb496b69565b18432dfc30198b408/problems/marathon.pdf
--------------------------------------------------------------------------------
/problems/motorcycles.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/francois141/algo-lab/8c5980fc979bb496b69565b18432dfc30198b408/problems/motorcycles.pdf
--------------------------------------------------------------------------------
/problems/moving_books.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/francois141/algo-lab/8c5980fc979bb496b69565b18432dfc30198b408/problems/moving_books.pdf
--------------------------------------------------------------------------------
/problems/nemean lion.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/francois141/algo-lab/8c5980fc979bb496b69565b18432dfc30198b408/problems/nemean lion.pdf
--------------------------------------------------------------------------------
/problems/new york.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/francois141/algo-lab/8c5980fc979bb496b69565b18432dfc30198b408/problems/new york.pdf
--------------------------------------------------------------------------------
/problems/octopussy.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/francois141/algo-lab/8c5980fc979bb496b69565b18432dfc30198b408/problems/octopussy.pdf
--------------------------------------------------------------------------------
/problems/placing knights.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/francois141/algo-lab/8c5980fc979bb496b69565b18432dfc30198b408/problems/placing knights.pdf
--------------------------------------------------------------------------------
/problems/real estate market.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/francois141/algo-lab/8c5980fc979bb496b69565b18432dfc30198b408/problems/real estate market.pdf
--------------------------------------------------------------------------------
/problems/revenge of the sith.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/francois141/algo-lab/8c5980fc979bb496b69565b18432dfc30198b408/problems/revenge of the sith.pdf
--------------------------------------------------------------------------------
/problems/rubeus_hagrid.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/francois141/algo-lab/8c5980fc979bb496b69565b18432dfc30198b408/problems/rubeus_hagrid.pdf
--------------------------------------------------------------------------------
/problems/san_francisco.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/francois141/algo-lab/8c5980fc979bb496b69565b18432dfc30198b408/problems/san_francisco.pdf
--------------------------------------------------------------------------------
/problems/search_snippets.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/francois141/algo-lab/8c5980fc979bb496b69565b18432dfc30198b408/problems/search_snippets.pdf
--------------------------------------------------------------------------------
/problems/severus_snape.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/francois141/algo-lab/8c5980fc979bb496b69565b18432dfc30198b408/problems/severus_snape.pdf
--------------------------------------------------------------------------------
/problems/shopping trip.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/francois141/algo-lab/8c5980fc979bb496b69565b18432dfc30198b408/problems/shopping trip.pdf
--------------------------------------------------------------------------------
/problems/strikesback.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/francois141/algo-lab/8c5980fc979bb496b69565b18432dfc30198b408/problems/strikesback.pdf
--------------------------------------------------------------------------------
/problems/suez.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/francois141/algo-lab/8c5980fc979bb496b69565b18432dfc30198b408/problems/suez.pdf
--------------------------------------------------------------------------------
/problems/the augean stables.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/francois141/algo-lab/8c5980fc979bb496b69565b18432dfc30198b408/problems/the augean stables.pdf
--------------------------------------------------------------------------------
/problems/the fighting pits of meereen .pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/francois141/algo-lab/8c5980fc979bb496b69565b18432dfc30198b408/problems/the fighting pits of meereen .pdf
--------------------------------------------------------------------------------
/problems/the_great_game.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/francois141/algo-lab/8c5980fc979bb496b69565b18432dfc30198b408/problems/the_great_game.pdf
--------------------------------------------------------------------------------
/problems/tiles.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/francois141/algo-lab/8c5980fc979bb496b69565b18432dfc30198b408/problems/tiles.pdf
--------------------------------------------------------------------------------
/problems/tracking.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/francois141/algo-lab/8c5980fc979bb496b69565b18432dfc30198b408/problems/tracking.pdf
--------------------------------------------------------------------------------
/problems/what is the maximum.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/francois141/algo-lab/8c5980fc979bb496b69565b18432dfc30198b408/problems/what is the maximum.pdf
--------------------------------------------------------------------------------
/problems/world cup.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/francois141/algo-lab/8c5980fc979bb496b69565b18432dfc30198b408/problems/world cup.pdf
--------------------------------------------------------------------------------
/resume/README.md:
--------------------------------------------------------------------------------
1 | # ALGO Lab ETHZ 2022
2 |
3 | ## STL
4 |
5 | #### Sort an array
6 |
7 | ```c++
8 | sort(array.begin(),array.end(), [](const int &e1, const int &e2) -> bool {
9 | return e1 < e2;
10 | });
11 | ```
12 |
13 | #### Sum of all elements in an array
14 |
15 | ```c++
16 | int sum = accumulate(array.begin(),array.end(),0);
17 | ```
18 |
19 | #### Prefix sum with stl
20 |
21 | ```c++
22 | partial_sum(array.begin(),array.end(),array.begin());
23 | ```
24 |
25 | #### Shuffles all values in an array (avoid adversary input)
26 |
27 | ```c++
28 | random_shuffle(array.begin(),array.end());
29 | ```
30 |
31 | #### Check if an elements is in array (must be sorted)
32 |
33 | ```c++
34 | bool present = binary_search(array.begin(),array.end(), element);
35 | ```
36 |
37 | #### Find first element bigger equal value (must be sorted)
38 |
39 | This return an iterator. We use * to dereference the iterator.
40 |
41 | ```c++
42 | int returned_value = *min_value(array.begin(),array.end(),value);
43 | ```
44 |
45 | #### Find first element bigger than value (must be sorted)
46 |
47 | This return an iterator. We use * to dereference the iterator.
48 |
49 | ```c++
50 | int returned_value = *max_value(array.begin(),array.end(),value);
51 | ```
52 |
53 | #### Intersection of two sets
54 |
55 | ```c++
56 | std::vector v_intersection;
57 | std::set_intersection(array1.begin(), array1.end(), array2.begin(), array2.end(), std::back_inserter(v_intersection));
58 | ```
59 |
60 | #### Union of two sets
61 |
62 | ```c++
63 | std::vector v_union;
64 | std::set_union(array1.begin(), array1.end(), array2.begin(), array2.end(), std::back_inserter(v_union));
65 | ```
66 | ## Boost
67 |
68 | ### Basics
69 |
70 | #### Declaration of graphs
71 |
72 | ```cpp
73 | #include
74 |
75 | // Create undirected graph
76 | typedef boost::adjacency_list graph;
77 |
78 | // Create directed graph
79 | typedef boost::adjacency_list graph;
80 |
81 | // Create directed weighted graph
82 | typedef boost::adjacency_list > weighted_graph;
83 | ```
84 |
85 | #### Manipulation of edges with propery map
86 | ```cpp
87 | // Graph
88 | typedef boost::adjacency_list> graph;
89 |
90 | // Iterator over descriptor
91 | typedef boost::graph_traits::edge_iterator edge_it;
92 |
93 | // Descriptor
94 | typedef boost::graph_traits::edge_descriptor edge_descriptor;
95 |
96 | // Edge weight
97 | typedef boost::property_map::type edge_weights_map;
98 |
99 | // Retrieve the property map
100 | edge_weights_map edge_map = get(boost::edge_weight,G);
101 |
102 | edge_it e_beg, e_end;
103 | for (boost::tie(e_beg, e_end) = boost::edges(G); e_beg != e_end; ++e_beg) {
104 | // Output from & to
105 | std::cout << boost::source(*e_beg, G) << " " << boost::target(*e_beg, G) << "\n";
106 | // Output weight of the edge
107 | std::cout << edge_map[*e_beg] << "\n";
108 | }
109 |
110 | ```
111 |
112 | #### Iterator over all edges from a single vertex
113 |
114 | ```cpp
115 | // Graph
116 | typedef boost::adjacency_list> graph;
117 |
118 | // Iterator over descriptor
119 | typedef boost::graph_traits::out_edge_iterator out_edge_it;
120 |
121 | out_edge_it e_beg, e_end;
122 | for (boost::tie(e_beg, e_end) = boost::out_edges(G); e_beg != e_end; ++e_beg) {
123 | std::cout << boost::target(*e_beg, G) << "\n";
124 |
125 | }
126 | ```
127 |
128 | ### Algorithms
129 |
130 | #### Dijkstra - shortest path from one node to all other in the graph
131 |
132 | ```cpp
133 |
134 | #include
135 |
136 | std::vector dijkstra_dist(const weighted_graph &G, int s, int t) {
137 | int n = boost::num_vertices(G);
138 | std::vector dist_map(n);
139 |
140 | boost::dijkstra_shortest_paths(G, s,boost::distance_map(boost::make_iterator_property_map(dist_map.begin(),boost::get(boost::vertex_index, G))));
141 |
142 | return dist_map;
143 | }
144 |
145 | ```
146 |
147 | #### MST - Kruskal algorithm
148 |
149 | ```cpp
150 |
151 | #include
152 |
153 | vector kruskal(const weighted_graph &G) {
154 | std::vector mst;
155 |
156 | boost::kruskal_minimum_spanning_tree(G, std::back_inserter(mst));
157 |
158 | return mst;
159 | }
160 | ```
161 |
162 |
163 | #### Maximum matching
164 |
165 | ```cpp
166 |
167 | #include
168 |
169 | int maximum_matching(const graph &G) {
170 | int n = boost::num_vertices(G);
171 | std::vector mate_map(n);
172 |
173 | const vertex_desc NULL_VERTEX = boost::graph_traits::null_vertex();
174 |
175 | boost::edmonds_maximum_cardinality_matching(G, boost::make_iterator_property_map(mate_map.begin(), boost::get(boost::vertex_index, G)));
176 | int matching_size = boost::matching_size(G, boost::make_iterator_property_map(mate_map.begin(), boost::get(boost::vertex_index, G)));
177 |
178 | return matching_size;
179 | }
180 |
181 | ```
182 |
183 | #### Bipartitness of a graph
184 |
185 | ```cpp
186 | bool is_bipartite = boost::is_bipartite(G);
187 | ```
188 |
189 | #### Connected components
190 |
191 | ```cpp
192 |
193 | #include
194 |
195 | void strong_connected_comp(const graph &G) {
196 | int n = boost::num_vertices(G);
197 |
198 | std::vector scc_map(n);
199 | int nscc = boost::connected_components(G,boost::make_iterator_property_map(scc_map.begin(), boost::get(boost::vertex_index, G)));
200 |
201 | std::cout << "Number of connected components: " << nscc << "\n";
202 | for (int i = 0; i < n; ++i) {
203 | std::cout << i << " " << scc_map[i] << "\n";
204 | }
205 | }
206 | ```
207 |
208 |
209 | #### Strongly connected components
210 |
211 | ```cpp
212 |
213 | #include
214 |
215 | void strong_connected_comp(const graph &G) {
216 | int n = boost::num_vertices(G);
217 |
218 | std::vector scc_map(n);
219 | int nscc = boost::strong_components(G,boost::make_iterator_property_map(scc_map.begin(), boost::get(boost::vertex_index, G)));
220 |
221 | std::cout << "Number of connected components: " << nscc << "\n";
222 | for (int i = 0; i < n; ++i) {
223 | std::cout << i << " " << scc_map[i] << "\n";
224 | }
225 | }
226 | ```
227 |
228 | ## CGAL
229 |
230 | ### Basics
231 |
232 | #### Kernels
233 |
234 | ```cpp
235 | typedef CGAL::Exact_predicates_inexact_constructions_kernel K;
236 | typedef CGAL::Exact_predicates_exact_constructions_kernel K;
237 | typedef CGAL::Exact_predicates_exact_constructions_kernel_with_sqrt K;
238 | ```
239 |
240 | #### Basics types
241 |
242 | ```cpp
243 | typedef K::Point_2 Point;
244 | typedef K::Line_2 Line;
245 | typedef K::Ray_2 Ray;
246 | typedef K::Triangle_2 Triangle;
247 | typedef K::Circle_2 Circle;
248 | typedef K::FT FT;
249 |
250 | Point point = Point(x,y);
251 | Line line = Line(p1, p2);
252 | Ray ray = Ray(start_point, direction);
253 | Triangle triangle = Triangle(p1,p2,p3);
254 | Circle circle = Circle(c1,c2,c3);
255 |
256 | ```
257 |
258 | #### Ceil & floor
259 |
260 | ```cpp
261 | double floor_to_double(const K::FT& x)
262 | {
263 | double a = std::floor(CGAL::to_double(x));
264 | while (a > x) a -= 1;
265 | while (a+1 <= x) a += 1;
266 | return a;
267 | }
268 |
269 | double ceil_to_double(const K::FT& x)
270 | {
271 | double a = std::ceil(CGAL::to_double(x));
272 | while (a > x) a -= 1;
273 | while (a+1 <= x) a += 1;
274 | return a;
275 | }
276 | ```
277 |
278 | ### Algorithms
279 |
280 | #### Intersections & distance
281 |
282 | ```cpp
283 |
284 | // Check for intersection
285 | if (CGAL::do_intersect(s[i],s[j])) {
286 | // Compute intersection
287 | auto o = CGAL::intersection(s[i],s[j]);
288 | if (const Point* op = boost::get(&*o))
289 | std::cout << "point: " << *op << "\n";
290 | else if (const Segment* os = boost::get(&*o))
291 | std::cout << "segment: " << os->source() << " " << os->target() << "\n";
292 | else
293 | throw std::runtime_error("strange segment intersection");
294 | }
295 |
296 | // Get distance between two points
297 | auto distance = CGAL::squared_distance(p1,p2);
298 | ```
299 |
300 | #### Get minimum enclosing cercle
301 |
302 | ```cpp
303 |
304 | #include
305 | #include
306 |
307 | typedef CGAL::Min_circle_2_traits_2 Traits;
308 | typedef CGAL::Min_circle_2 Min_circle;
309 |
310 | Min_circle mc(Points.begin(), Points.end(), true);
311 |
312 | Traits::Circle c = mc.circle();
313 | std::cout << c.center() << " " << c.squared_radius() << "\n";
314 | ```
315 |
316 | ### Triangulation
317 |
318 | ### Definitions
319 |
320 | #### Basic triangulation with no information
321 |
322 | ```cpp
323 | #include
324 | #include
325 |
326 | // Definitions
327 | typedef CGAL::Exact_predicates_inexact_constructions_kernel K;
328 | typedef CGAL::Delaunay_triangulation_2 Triangulation;
329 |
330 | // Triangulation object
331 | Triangulation t;
332 | // Insert points
333 | t.insert(points.begin(),points.end());
334 | ```
335 |
336 | #### Basic triangulation with vertex information
337 |
338 | ```cpp
339 | #include
340 | #include
341 | // Also include this file
342 | #include
343 |
344 | // Definitions
345 | typedef CGAL::Exact_predicates_inexact_constructions_kernel K;
346 | typedef CGAL::Triangulation_vertex_base_with_info_2 Vb;
347 | typedef CGAL::Triangulation_face_base_2 Fb;
348 | typedef CGAL::Triangulation_data_structure_2 Tds;
349 | typedef CGAL::Delaunay_triangulation_2 Triangulation;
350 |
351 | typedef K::Point_2 Point;
352 |
353 | vector> points;
354 |
355 | // Triangulation object
356 | Triangulation t;
357 | // Insert points
358 | t.insert(points.begin(),points.end());
359 | ```
360 |
361 | #### Iterate over faces, edges & vertices
362 |
363 | ```cpp
364 | typedef Triangulation::Finite_faces_iterator Face_iterator;
365 | typedef Triangulation::Finite_edges_iterator Edte_iterator;
366 | typedef Triangulation::Finite_vertices_iterator Vertex_iterator;
367 |
368 | for (Face_iterator f = t.finite_faces_begin(); f != t.finite_faces_end(); ++f) {
369 | std::cout << t.triangle(f) << endl;
370 | }
371 |
372 | for (Edge_iterator e = t.finite_edges_begin(); e != t.finite_edges_end(); ++e) {
373 | std::cout << t.segment(e) << endl;
374 |
375 | // Get edge length
376 | auto length = t.segment(e).squared_length();
377 |
378 | // Get vertices
379 | Triangulation::Vertex_handle v1 = e->first->vertex((e->second + 1) % 3);
380 | Triangulation::Vertex_handle v2 = e->first->vertex((e->second + 2) % 3);
381 |
382 | // Print point
383 | std::cout << "e = " << v1->point() << " <-> " << v2->point() << std::endl;
384 |
385 | // Print informations
386 | std::cout << "i = " << v1->info() << " <-> " << v2->info() << std::endl;
387 | }
388 |
389 | for (Vertex_iterator v = t.finite_vertices_begin(); e != t.finite_vertices_end(); ++e) {
390 | std::cout << v->point() << " " v->info() << endl;
391 | }
392 | ```
393 |
394 | #### Incident edges from a vertex
395 |
396 |
397 | ```cpp
398 | Triangulation::Vertex_handle v = handle;
399 |
400 | Triangulation::Edge_circulator c = t.incident_edges(v);
401 | do {
402 | if (t.is_infinite(c)) { ... }
403 | // Do something with the edges here
404 | } while (++c != t.incident_edges(v));
405 | ```
406 |
--------------------------------------------------------------------------------
/week1/Build the Sum.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include
4 |
5 | using namespace std;
6 |
7 | void solve() {
8 |
9 | int n;
10 | cin >> n;
11 |
12 | vector a(n);
13 |
14 | for(int i = 0; i < n;i++) {
15 | cin >> a[i];
16 | }
17 |
18 | cout << accumulate(a.begin(),a.end(),0) << "\n";
19 | }
20 |
21 | int main() {
22 |
23 | int t;
24 | cin >> t;
25 |
26 | while(t--)
27 | solve();
28 |
29 | return 0;
30 | }
31 |
--------------------------------------------------------------------------------
/week1/Dominoes.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 |
4 | using namespace std;
5 |
6 | void solve() {
7 |
8 | int n;
9 | cin >> n;
10 |
11 | vector a(n);
12 | for(int i = 0; i < n;i++) {
13 | cin >> a[i];
14 | }
15 |
16 | int position = 0;
17 | int until = 1;
18 |
19 | while(position < n && position < until) {
20 | until = max(until,position + a[position]);
21 | position++;
22 | }
23 |
24 | cout << min(position,n) << "\n";
25 |
26 | return;
27 | }
28 |
29 |
30 | int main() {
31 |
32 | int t;
33 | cin >> t;
34 |
35 | while(t--)
36 | solve();
37 |
38 | return 0;
39 | }
40 |
--------------------------------------------------------------------------------
/week1/Even Matrices.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 |
4 | using namespace std;
5 |
6 | void solve() {
7 |
8 | int n;
9 | cin >> n;
10 | n++;
11 |
12 | vector> matrix = vector>(n,vector(n,0));
13 |
14 | for(int i = 1; i < n;i++) {
15 | for(int j = 1; j < n;j++) {
16 | cin >> matrix[i][j];
17 | matrix[i][j] += matrix[i-1][j] + matrix[i][j-1] - matrix[i-1][j-1];
18 | }
19 | }
20 |
21 | unsigned int count = 0;
22 |
23 | for(int i = 0; i < n; i++) {
24 | for(int j = 0; j < i; j++) {
25 | unsigned int evens = 0;
26 | unsigned int odds = 0;
27 | for(int k = 0; k < n; k++) {
28 | (matrix[i][k]-matrix[j][k]) % 2 == 0 ? evens++ : odds++;
29 | }
30 | count += (evens*(evens-1) + odds*(odds-1)) / 2;
31 | }
32 | }
33 |
34 | cout << count << "\n";
35 | }
36 |
37 | int main() {
38 |
39 | int t;
40 | cin >> t;
41 |
42 | while(t--) {
43 | solve();
44 | }
45 |
46 | }
47 |
--------------------------------------------------------------------------------
/week1/Even Pairs.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include
4 | #include
5 |
6 | using namespace std;
7 |
8 |
9 | void solve() {
10 |
11 | int n;
12 | cin >> n;
13 |
14 | vector a(n);
15 |
16 | for(int i = 0; i < n;i++) {
17 | cin >> a[i];
18 | }
19 |
20 | partial_sum(a.begin(), a.end(), a.begin(),plus());
21 |
22 | int evens = count_if(a.begin(), a.end(),[](int x) -> int {
23 | return x % 2 == 0;
24 | });
25 |
26 | int odds = count_if(a.begin(), a.end(),[](int x) -> int {
27 | return x % 2 == 1;
28 | });
29 |
30 | cout << (evens*(evens-1) + odds*(odds-1)) / 2 + evens << "\n";
31 |
32 | return;
33 | }
34 |
35 |
36 | int main() {
37 |
38 | int t;
39 | cin >> t;
40 |
41 | while(t--)
42 | solve();
43 |
44 | return 0;
45 | }
46 |
--------------------------------------------------------------------------------
/week10/Clues.cpp:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | #include
4 | #include
5 | #include
6 | #include
7 | #include
8 |
9 | #include
10 | #include
11 | #include
12 | #include
13 |
14 | typedef CGAL::Exact_predicates_inexact_constructions_kernel K;
15 |
16 | typedef int Index;
17 | typedef CGAL::Triangulation_vertex_base_with_info_2 Vb;
18 | typedef CGAL::Triangulation_face_base_2 Fb;
19 | typedef CGAL::Triangulation_data_structure_2 Tds;
20 | typedef CGAL::Delaunay_triangulation_2 Triangulation;
21 | typedef CGAL::Point_2 Point;
22 |
23 | typedef Triangulation::Finite_faces_iterator Face_iterator;
24 |
25 | typedef boost::adjacency_list graph;
26 |
27 | using namespace std;
28 |
29 | #define int long
30 |
31 | void solve() {
32 |
33 | int n,m,r;
34 | cin >> n >> m >> r;
35 |
36 | vector> radios(0);
37 | vector> clues(0);
38 |
39 | for(int i = 0; i < n;i++) {
40 | int x,y;
41 | cin >> x >> y;
42 | radios.push_back(make_pair(Point(x,y),i));
43 | }
44 |
45 | sort(radios.begin(),radios.end(), [](auto r1, auto r2) {
46 | return r1.first.x() < r2.first.x();
47 | });
48 |
49 | for(int i = 0; i < m;i++) {
50 | int x,y,z,t;
51 | cin >> x >> y >> z >> t;
52 | clues.push_back(make_pair(Point(x,y),Point(z,t)));
53 | }
54 |
55 | graph G(n);
56 |
57 | for(int i = 0; i < n;i++) {
58 | for(int j = i+1; j < n;j++) {
59 | if(radios[j].first.x() - radios[i].first.x() > r)
60 | break;
61 | if(CGAL::squared_distance(radios[i].first,radios[j].first) <= r*r) {
62 | boost::add_edge(i,j,G);
63 | }
64 | }
65 | }
66 |
67 | bool is_bipartite = boost::is_bipartite(G);
68 |
69 | Triangulation t;
70 | t.insert(radios.begin(),radios.end());
71 |
72 | graph G2(n);
73 |
74 | for(auto e = t.finite_edges_begin(); e != t.finite_edges_end(); ++e) {
75 | Index i1 = e->first->vertex((e->second+1)%3)->info();
76 | Index i2 = e->first->vertex((e->second+2)%3)->info();
77 | if(t.segment(e).squared_length() <= r*r) {
78 | boost::add_edge(i1,i2,G2);
79 | }
80 | }
81 |
82 | vectorcomponent(num_vertices(G2));
83 | boost::connected_components(G2, &component[0]);
84 |
85 | for(int i = 0; i < m;i++) {
86 |
87 | auto p1 = clues[i].first;
88 | auto p2 = clues[i].second;
89 |
90 | if(!is_bipartite) {
91 | cout << "n";
92 | continue;
93 | }
94 |
95 | if(CGAL::squared_distance(p1,p2) <= r*r) {
96 | cout << "y";
97 | continue;
98 | }
99 |
100 | auto n1 = t.nearest_vertex(p1);
101 | auto n2 = t.nearest_vertex(p2);
102 |
103 | if(CGAL::squared_distance(p1,n1->point()) > r*r || CGAL::squared_distance(p2,n2->point()) > r*r) {
104 | cout << "n";
105 | continue;
106 | }
107 |
108 | if(component[n1->info()] == component[n2->info()]) {
109 | cout << "y";
110 | }
111 | else {
112 | cout << "n";
113 | }
114 | }
115 | cout << "\n";
116 | }
117 |
118 |
119 | signed main() {
120 | int t; cin >> t;
121 |
122 | while(t--) {
123 | solve();
124 | }
125 | return 0;
126 | }
127 |
--------------------------------------------------------------------------------
/week10/DHL.cpp:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | #define int long
4 |
5 | using namespace std;
6 |
7 | void solve() {
8 | int n;
9 | cin >> n;
10 |
11 | vector a(n,0);
12 | vector b(n,0);
13 |
14 | for(int i = 0; i < n;i++) cin >> a[i];
15 | for(int i = 0; i < n;i++) cin >> b[i];
16 |
17 | vector> dp(n + 1, vector(n + 1, INT_MAX));
18 | dp[0][0] = 0;
19 |
20 | for (int i = 1; i <= n; i++) {
21 | for (int j = 1; j <= n; j++) {
22 | int pred = min({dp[i - 1][j], dp[i][j - 1], dp[i - 1][j - 1]});
23 | dp[i][j] = pred + (a[i - 1] - 1) * (b[j - 1] - 1);
24 | }
25 | }
26 |
27 | cout << dp.back().back() << "\n";
28 | }
29 |
30 | signed main() {
31 | ios_base::sync_with_stdio(false);
32 | cin.tie(0);
33 |
34 | int t;
35 | cin >> t;
36 |
37 | while(t--)
38 | solve();
39 | }
40 |
--------------------------------------------------------------------------------
/week10/Fleetrace.cpp:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | #include
4 | #include
5 | #include
6 | #include
7 | #include
8 |
9 | using namespace std;
10 |
11 | typedef boost::adjacency_list_traits traits;
12 | typedef boost::adjacency_list > > > > graph;
17 |
18 | typedef boost::graph_traits::edge_descriptor edge_desc;
19 | typedef boost::graph_traits::out_edge_iterator out_edge_it;
20 |
21 | class edge_adder {
22 | graph &G;
23 |
24 | public:
25 | explicit edge_adder(graph &G) : G(G) {}
26 | void add_edge(int from, int to, long capacity, long cost) {
27 | auto c_map = boost::get(boost::edge_capacity, G);
28 | auto r_map = boost::get(boost::edge_reverse, G);
29 | auto w_map = boost::get(boost::edge_weight, G); // new!
30 | const edge_desc e = boost::add_edge(from, to, G).first;
31 | const edge_desc rev_e = boost::add_edge(to, from, G).first;
32 | c_map[e] = capacity;
33 | c_map[rev_e] = 0; // reverse edge has no capacity!
34 | r_map[e] = rev_e;
35 | r_map[rev_e] = e;
36 | w_map[e] = cost; // new assign cost
37 | w_map[rev_e] = -cost; // new negative cost
38 | }
39 | };
40 |
41 | void solve() {
42 |
43 | int b,s,p;
44 | cin >> b >> s >> p;
45 |
46 | vector>> pairs(b,vector>(0));
47 | for(int i = 0; i < p;i++) {
48 | int bi,si,pi;
49 | cin >> bi >> si >> pi;
50 | pairs[bi].push_back(make_pair(si,50-pi));
51 | }
52 |
53 | graph G(b+s);
54 |
55 | edge_adder adder(G);
56 |
57 | auto v_source = boost::add_vertex(G);
58 | auto v_sink = boost::add_vertex(G);
59 |
60 | for(int i = 0; i < pairs.size();i++) {
61 | for(int j = 0; j < pairs[i].size();j++) {
62 | auto e = pairs[i][j];
63 | adder.add_edge(i,b+e.first,1,e.second);
64 | }
65 | }
66 |
67 | for(int i = 0; i < b;i++) {
68 | adder.add_edge(v_source,i,1,0);
69 | adder.add_edge(i,v_sink,1,50);
70 | }
71 | for(int i = 0; i < s;i++) {
72 | adder.add_edge(b+i,v_sink,1,0);
73 | }
74 |
75 | boost::successive_shortest_path_nonnegative_weights(G, v_source, v_sink);
76 | int cost = boost::find_flow_cost(G);
77 |
78 | cout << 50*b - cost << "\n";
79 | }
80 |
81 | int main() {
82 |
83 | ios_base::sync_with_stdio(false);
84 | cin.tie(0);
85 |
86 | int t; cin >> t;
87 |
88 | while(t--)
89 | solve();
90 |
91 | return 0;
92 | }
93 |
--------------------------------------------------------------------------------
/week10/New York.cpp:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | using namespace std;
4 |
5 | vector> graph;
6 |
7 | vector t;
8 | set s;
9 | multiset values;
10 | deque path;
11 | vector p;
12 |
13 | int n,m,k;
14 |
15 | void dfs(int idx) {
16 |
17 | if(path.size() == m) {
18 |
19 | int minT = *values.begin();
20 | int maxT = *values.rbegin();
21 |
22 | if(maxT - minT <= k) {
23 | s.insert(path[0]);
24 | }
25 | }
26 |
27 | for(int next : graph[idx]) {
28 |
29 | path.push_back(next);
30 | values.insert(t[next]);
31 |
32 | if(path.size() > m) {
33 | values.erase(values.find(t[path[0]]));
34 | path.pop_front();
35 | }
36 |
37 | dfs(next);
38 |
39 | if(path[0] != 0) {
40 | int p_idx = p[path[0]];
41 | path.push_front(p_idx);
42 | values.insert(t[p_idx]);
43 | }
44 |
45 | values.erase(values.find(t[next]));
46 | path.pop_back();
47 | }
48 | }
49 |
50 | void solve() {
51 |
52 | cin >> n >> m >> k;
53 |
54 | t = vector(n);
55 | for(int i = 0; i < n;i++) cin >> t[i];
56 |
57 | p = vector(n,0);
58 | graph = vector>(n,vector(0));
59 | for(int i = 0; i < n-1;i++) {
60 | int from,to; cin >> from >> to;
61 | graph[from].push_back(to);
62 | p[to] = from;
63 | }
64 |
65 | s = set();
66 | values = multiset();
67 |
68 | path = deque(0);
69 | path.push_back(0);
70 | values.insert(t[0]);
71 |
72 | dfs(0);
73 |
74 | if(s.empty()) {
75 | cout << "Abort mission";
76 | }
77 | else {
78 | for(int tmp : s) {
79 | cout << tmp << " ";
80 | }
81 | }
82 | cout << "\n";
83 |
84 | return;
85 | }
86 |
87 | signed main() {
88 | ios_base::sync_with_stdio(false);
89 | cin.tie(0);
90 |
91 | int t; cin >> t;
92 |
93 | while(t--)
94 | solve();
95 |
96 | return 0;
97 | }
98 |
--------------------------------------------------------------------------------
/week11/Asterix in Switzerland.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include
4 |
5 | #include
6 | #include
7 |
8 | using namespace std;
9 |
10 | #define int long
11 |
12 | typedef boost::adjacency_list_traits traits;
13 | typedef boost::adjacency_list>>> graph;
17 |
18 | typedef traits::vertex_descriptor vertex_desc;
19 | typedef traits::edge_descriptor edge_desc;
20 |
21 | class edge_adder {
22 |
23 | graph &G;
24 |
25 | public:
26 | explicit edge_adder(graph &G) : G(G) {}
27 |
28 | void add_edge(int from, int to, int weight) {
29 | auto c_map = boost::get(boost::edge_capacity, G);
30 | auto r_map = boost::get(boost::edge_reverse, G);
31 | const auto e = boost::add_edge(from, to, G).first;
32 | const auto rev_e = boost::add_edge(to, from, G).first;
33 | c_map[e] = weight;
34 | c_map[rev_e] = 0;
35 | r_map[e] = rev_e;
36 | r_map[rev_e] = e;
37 | }
38 | };
39 |
40 | void solve() {
41 |
42 | int n,m;
43 | cin >> n >> m;
44 |
45 | graph G(n);
46 | edge_adder adder(G);
47 |
48 | auto v_source = boost::add_vertex(G);
49 | auto v_sink = boost::add_vertex(G);
50 |
51 | int sum = 0;
52 |
53 | vector