We need to compute the minimum distance to start at house (call it H), visit both shops (A and B), and return to H, traveling only along the three edges: H-A of length d1, H-B of length d2, and A-B of length d3. He can travel arbitrarily, repeating edges if needed. Essentially, we need the shortest closed walk that starts and ends at H and visits both vertices A and B at least once.
Given a graph with three vertices and three edges forming a triangle? Actually the graph: vertices H, A, B. Edges: H-A (d1), H-B (d2), A-B (d3). It's a complete graph on three vertices, but with given edge lengths. So it's a triangle. So we need the shortest closed walk from H that covers all vertices? But we need to visit both shops (A and B), not necessarily all edges. Since it's a closed walk starting and ending at H, we want to minimize total distance while ensuring we include both A and B at least once.
We can think of possible routes: Since we can repeat edges, we basically have to decide the order of visiting A and B. The walk is a sequence of edges, starting and ending at H. The minimal total distance will be the minimum over possible sequences.
Classic problem from Codeforces: "Patrick and Shopping". Actually I recall a problem: "Patrick and Shopping" (CF 599A). The solution is min(2d1+2d2, d1+d2+d3, 2d1+2d3, 2d2+2d3) etc? Let's recall: There are several possible routes:
-
Go H -> A -> H -> B -> H. Total = 2d1 + 2d2.
-
Go H -> A -> B -> H. Total = d1 + d3 + d2.
-
Go H -> B -> A -> H. Same as above.
-
But also could go H -> A -> B -> A -> H? That would be d1 + d3 + d3 + d1 = 2d1 + 2d3? Actually H->A (d1), A->B (d3), B->A (d3), A->H (d1) = 2d1 + 2d3. But we need to visit both shops. In that route, we visited both A and B? Actually we visited A initially, then B, then back to A, then H. So both visited. Could be optimal if d2 is huge and d3 is small. Similarly H->B->A->B->H = 2d2+2d3.
-
Also could go H -> A -> H -> B -> A -> H? That might be longer.
But we can also consider route: H -> A -> B -> A -> H might be minimal if d2 is large. But note we already considered 2d1+2d3. Similarly 2d2+2d3.
Also H -> A -> B -> H (d1+d2+d3) is triangle.
Also could go H -> B -> H -> A -> H = 2d2+2d1, already considered.
But there is also possibility: H -> A -> B -> A -> H is not symmetric? Actually it's 2d1+2d3, but we could also do H -> B -> A -> B -> H = 2d2+2d3.
Could there be a route like H -> A -> B -> H but then maybe back to A? Actually we need return to H, so must end at H.
Another possibility: H -> A -> H -> B -> A -> H? That would be d1 + d1 + d2 + d3 + d1? Let's compute: H->A (d1), A->H (d1), H->B (d2), B->A (d3), A->H (d1) = 2*d1 + d2 + d3. That is likely worse than min.
But to be thorough, we need to consider all possible closed walks from H that cover both A and B. Since the graph is small, we can think in terms of "cover" required vertices. The minimal closed walk covering a set of vertices is essentially the shortest closed trail (or walk) that visits those vertices. This is akin to the "shortest closed walk that visits a set of vertices" which can be solved by considering the shortest paths between required vertices. However, because we can repeat vertices, the optimal solution will be either:
-
Go to A and return, go to B and return: 2d1+2d2.
-
Go to one shop, then the other, then back home via the triangle: d1+d2+d3.
-
Go to one shop, then the other, then back home via the first shop: that would be d1 + d3 + d3 + d1? Wait, if we go H->A->B->A->H, total = d1 + d3 + d3 + d1 = 2d1 + 2d3. That's like going to A, then to B, then back to A, then home. But we could also go H->A->B->H? That's covered.
But note 2d1+2d3 may be better than d1+d2+d3 if d2 > d1 + d3? Actually let's compare: 2d1+2d3 vs d1+d2+d3. The difference: (2d1+2d3) - (d1+d2+d3) = d1 + d3 - d2. So if d2 > d1 + d3, then 2d1+2d3 < d1+d2+d3? Let's check: If d2 > d1 + d3, then d1 + d3 - d2 is negative, so 2d1+2d3 is less. But is that possible? In a triangle, by triangle inequality? The edges may not satisfy triangle inequality; they are arbitrary lengths (they are just lengths of roads; they can be any positive numbers, not necessarily satisfying triangle inequality). So it's possible that d2 > d1 + d3. Then the direct edge from H to B is longer than going via A? Actually d2 is length from H to B, d1 from H to A, d3 from A to B. So if d2 > d1 + d3, then it's longer to go directly H-B than go H-A-B. So then going H->A->B would be cheaper than going directly H->B. In that case, the route H->A->B->H would be d1 + d3 + d2, but if d2 is huge, that might be large. But maybe it's better to go H->A->B->A->H = 2d1+2d3. That avoids the large d2 entirely, only uses d1 and d3 twice. But note we must start and end at H. In H->A->B->A->H, we visited both A and B, but we ended at H. That seems valid.
Similarly if d1 > d2 + d3, then 2d2+2d3 could be optimal.
But there's also route H->A->B->H that uses d2, but we might consider using the triangle but not returning directly: H->A->B->A->H is as above. Could also do H->B->A->B->H = 2d2+2d3.
Could there be a route H->A->B->A->H? That is 2d1+2d3. But note we could also do H->A->B->H but using a different path from B to H: could be B->A->H? That would be d3 + d1. Actually H->A->B->A->H is exactly the same as H->A->B and then back via A. But the direct from B to H is d2; but if d2 is longer, we may want to go B->A->H. That route is H->A->B->A->H which we considered. But maybe also H->B->A->H (which is d2+d3+d1) same as triangle route.
What about H->A->H->B->H? That is 2d1+2d2. Or H->A->B->H->A->H? That would include extra.
Also, maybe a route like H->A->B->H->A->B->H? But that is obviously longer.
We need to find minimum. So we consider four candidate distances:
-
2d1 + 2d2
-
d1 + d2 + d3
-
2d1 + 2d3
-
2d2 + 2d3
But are these all possibilities? Could there be a route like H->A->B->H that uses the direct H-B but then maybe also go H->A again? For instance H->A->B->H->A->H? That would be d1+d3+d2 + d1? Actually H->A (d1), A->B (d3), B->H (d2), then H->A (d1), A->H (d1) total = d1+d3+d2+d1+d1 = d1+d2+d3+2d1? That is 2d1 + d2 + d3. But we already have 2d1+2d3? Not that.
What about H->A->H->B->A->H? That is d1 + d1 + d2 + d3 + d1 = 3*d1 + d2 + d3.
But these are larger.
But wait, there is also possibility H->A->B->A->B->H? That would be d1+d3+d3+d3? Actually it's not symmetrical.
But we can also consider routes that go from H to one shop, then directly to the other, then back to H, but maybe it's cheaper to go to a shop twice to avoid a long edge. In particular, if both d1 and d2 are huge relative to d3, the best might be to go from H to A, then to B, then back to A, then to H? But that's 2d1+2d3. Or go H to B, then to A, then to H? That's d1+d2+d3, but if d1 and d2 are huge, maybe 2d3+2min(d1,d2)? Actually consider scenario: d1=100, d2=100, d3=1. Then options:
- 2d1+2d2 = 400
- d1+d2+d3 = 201
- 2d1+2d3 = 202
- 2d2+2d3 = 202
So minimum is 201. That's d1+d2+d3.
Consider d1=1, d2=100, d3=1. Then options:
- 2+200=202
- 1+100+1=102
- 21+21=4? Wait 21+21=4. That is 4. But does that route H->A->B->A->H = 1+1+1+1=4. But check: H to A (d1=1), A to B (d3=1), B to A (d3=1), A to H (d1=1). Total=4. That visits both shops (A and B). And returns to H. That seems valid. But is it allowed? The route: start H, go to A (shop 1), then to B (shop 2), then back to A (again), then to H. It visits both shops at least once. It uses the edges: H-A, A-B, B-A, A-H. That's allowed. So total=4. But wait, is there any requirement that each shop must be visited exactly once? No, can be multiple times. So indeed 4 is possible. But does this make sense? With d1=1, d2=100, d3=1, the direct H-B edge is 100, but we avoid it completely. So minimum is 4. The answer would be 4. That's consistent with the second example: Input: 1 1 5 gives output 4. That example: d1=1, d2=1, d3=5. Options: 21+21=4, 1+1+5=7, 21+25=12, 21+25=12. So min=4, matches.
So indeed candidate set includes 2d1+2d3 and 2d2+2d3.
But are there other candidates? Possibly 2d3 + 2d1? Already covered.
Could there be a route like H->A->B->H->A->H? That is d1+d3+d2+d1+d1 = 2*d1 + d2 + d3? Compare to others.
But maybe 2d1 + d2 + d3 could be better than the four we listed? Let's see if it can ever be minimal. Suppose d1 is very small, d2 huge, d3 huge. But then 2d1 + d2 + d3 might be huge. 2d1+2d3 might be huge if d3 huge. 2d1+2d2 huge. d1+d2+d3 huge. So no.
But maybe d1 is small, d2 moderate, d3 small. For instance d1=1, d2=5, d3=1. Candidates: 2+10=12, 1+5+1=7, 2+2=4, 25+21=12. So min=4. So 2d1+2d3 wins. 2d1+d2+d3=12+5+1=8, not minimal.
What about d1=10, d2=1, d3=10. Candidates: 20+2=22, 10+1+10=21, 20+20=40, 2+20=22. So min=21. That's d1+d2+d3. 2d1+2d3=40, 2d2+2d3=22. So 21 minimal.
What about d1=10, d2=100, d3=10. Candidates: 20+200=220, 10+100+10=120, 20+20=40, 200+20=220. So min=40 via 2d1+2d3. So 2d1+2d3 wins.
Thus the four candidates seem sufficient.
But is there a route that yields something like 2d1 + 2d2? Already considered.
But what about the possibility of going H->A->B->H using the triangle but also maybe visiting one shop twice? Actually the minimal closed walk covering both A and B in a graph with 3 nodes can be found by considering all possible orders of visiting the required vertices and returning to start, allowing revisits. Since we must start and end at H, and need to cover A and B, the walk can be thought as: start H, go to one required vertex (say A), then go to the other (B), then back to H. But we can also go via intermediate vertices; however the graph is complete (all edges exist). So the shortest path between any two vertices is simply the direct edge, but if there's a shorter path via another vertex, we could use that. Since we are allowed to repeat edges, the shortest distance from H to A is d1, but could we go H->B->A which is d2+d3? That might be shorter than d1 if d1 > d2+d3. Similarly, the shortest distance from A to B is min(d3, d1+d2). But we can use those shortcuts in constructing the walk. For example, if d1 > d2+d3, then the shortest path from H to A is via B: H->B->A distance d2+d3. That could be used in a route: H->B->A->H? That would be (d2+d3) + d1? Actually after H->B->A we can go A->H directly d1, total = d2+d3+d1. But if we also need to visit B? In that route we visited B as intermediate? Actually H->B->A visits B (since we passed through B) and A, then H. That is a valid route: H->B->A->H. But total = d2+d3+d1, same as triangle route. However, what about H->B (d2) then B->A (d3) then A->H (d1). That's same. But if we consider that the shortest path from H to A is d1 (but could be longer than d2+d3), we might choose to go H->B->A instead of H->A directly. But then we are effectively using a path that visits both shops: starting at H, go to B (shop), then to A (shop), then back to H. That's the triangle route, but with order B then A. So it's still d1+d2+d3.
But could we use a path that uses shorter "detours" to reduce total? For instance, consider that the shortest path from H to A is min(d1, d2+d3). The shortest path from A to B is min(d3, d1+d2). And from B to H is min(d2, d1+d3). So when constructing a closed walk that starts at H, visits A and B, and returns to H, we can decide to use the shortest paths between consecutive nodes in the sequence. However, the sequence of vertices visited includes the start H, then some order of A, B, and then back to H. The walk can be represented as H -> X -> Y -> H, where X and Y are A and B in some order, but we can also have extra stops if needed? But to minimize distance, we wouldn't want to visit an extra vertex unless it provides a shorter path. Actually the optimal walk can be described as: we choose an ordering of the required vertices (A, B) and then go from H to first, then to second, then back to H. But between each consecutive pair, we can take the shortest path (which may pass through other vertices). However, the shortest path from first to second might pass through H or through the other? But we must ensure that we still visit both shops, but if the shortest path from A to B goes through H, then that path would revisit H early, but that's allowed. However, if we go H->A via shortest path (which could be H->B->A) then we would visit B before A, meaning the ordering might be effectively different. So to capture all possibilities, we can consider that we need to traverse a closed walk that starts and ends at H and covers both A and B. This can be modelled as finding the minimum length of a closed walk that covers a set of vertices. In a graph with all edge weights positive, the optimal solution is the shortest closed walk that visits all required vertices, which is essentially the minimum length of a closed walk that includes both A and B, and that can be reduced to either a simple cycle or a union of cycles? Another approach: Since we can repeat vertices, the optimal walk can be thought of as a combination of shortest paths. The classic solution for this specific problem (CF 599A) is: min( d1 + d2 + d3, 2d1 + 2d2, 2d1 + 2d3, 2d2 + 2d3). I recall that indeed.
But we should verify if any other pattern could yield less. Consider if d1, d2, d3 are such that one edge is extremely large and the other two are small. For instance d1=100, d2=1, d3=1. Then candidates: 200+2=202, 100+1+1=102, 200+2=202, 2+2=4. Wait 2d2+2d3 = 2+2=4. That's 4! But can we achieve 4? Let's test: 2d2+2d3 = 21+21=4. That route would be H->B->A->B->H? Actually H->B (d2=1), B->A (d3=1), A->B (d3=1), B->H (d2=1) = total 4. That visits both A and B? Yes, we visited B, then A (via B->A), then back to B and H. So both shops visited. So 4 is achievable. But is there any shorter? Could we do H->B->A->H? That would be 1+1+100=102. So 4 is better. So indeed 2d2+2d3 is valid.
What about route H->A->B->H? That is 100+1+1=102.
So min is 4.
Now consider d1=5, d2=5, d3=100. Then candidates: 10+10=20, 5+5+100=110, 10+200=210, 10+200=210. So min=20 (2d1+2d2). That is go H->A->H->B->H or H->B->H->A->H. So that's good.
Now consider d1=1, d2=2, d3=3. Candidates: 2+4=6, 1+2+3=6, 2+6=8, 4+6=10. So min=6 (both 2d1+2d2 and d1+d2+d3 give 6). So fine.
Thus the four are enough.
But wait, what about route H->A->B->H but taking a detour for one leg that is shorter? For example, if d1 is large, shortest path from H to A might be H->B->A (d2+d3). So maybe a route: H->B->A->H could be d2+d3 + d1. That's just the triangle route with order swapped. But could we also do H->B->A->B->H? That would be d2 + d3 + d3 + d2 = 2d2+2d3. Already considered.
But what about route H->A (using H->B->A?) Actually if we decide to go H->A using the shortest path (maybe via B), that means we go H->B->A, which visits B before A. Then we could go A->H directly (d1) or A->B->H? But that would involve extra. So the route H->B->A->H yields d2+d3+d1. So that's same as d1+d2+d3. No extra candidate.
What about route H->A->B->A->H but using a different path for B->A? Already direct d3.
What about route H->A->H->B->A->H? That is d1+d1+d2+d3+d1 = 3*d1 + d2 + d3. That's larger unless d1 is 0? Not.
What about route that goes H->A->B->H->B? That ends not at H.
So the four candidates cover all.
But could there be a route like H->A->B->A->B->H? That would be d1 + d3 + d3 + d3? Actually H->A (d1), A->B (d3), B->A (d3), A->B (d3), B->H (d2). Total = d1 + 3d3 + d2. Compare to d1+d2+d3: it adds 2d3. So worse unless d3 negative, impossible.
What about H->A->H->B->A->B->H? Too long.
Thus answer = min( d1 + d2 + d3, 2d1 + 2d2, 2d1 + 2d3, 2d2 + 2d3 ).
But is there a scenario where a combination like 2*d1 + d2 + d3 is better? Only if d1 < 0? Not.
But let's double-check mathematically. We want to minimize total distance of a closed walk that starts and ends at H and visits both A and B. Since the graph has only 3 vertices, any walk can be described as a sequence of edges. Because we can revisit vertices, the walk could be arbitrarily long. But the shortest such walk will be simple in the sense that it does not contain cycles that can be removed without breaking the condition. More formally, the optimal walk can be assumed to have at most 2 visits to H? Actually maybe not. Consider a case where the shortest path from H to A is via B, but that already visits B. So a minimal walk could be: H -> B -> A -> H. That visits H twice? Actually H at start, then B, then A, then back to H (end). So H appears at start and end only. That's three vertices visited.
What about H -> A -> H -> B -> H? That visits H three times? Actually start H, then A, then back to H, then B, then back to H. So H appears as start, after first return, and at end. That's allowed.
Now think about the walk as a multigraph where we traverse edges. The total distance is sum of edges traversed. Since edges are positive, we want to minimize sum. The walk must connect H to A and B. In graph theory, the minimum length of a closed walk starting and ending at a given vertex that visits a set of required vertices is equal to the length of the shortest closed walk that contains those vertices. This is related to the "traveling salesman problem" on a small graph. Since the graph is complete (with 3 nodes) but with distances possibly not satisfying triangle inequality, the TSP tour (starting at H, visiting A and B, returning to H) would be the minimum over orderings of the direct distances: d(H,A)+d(A,B)+d(B,H) and d(H,B)+d(B,A)+d(A,H). But that's the triangle routes, which are d1+d3+d2 (both orders same sum). However, in TSP you typically cannot revisit vertices; you must have a Hamiltonian cycle covering all vertices exactly once (and returning to start). But here we can revisit, so we might consider non-simple tours that can use vertices multiple times. This can lead to tours that are not simple cycles: e.g., go H-A-H-B-H, which is not a simple cycle because it revisits H. That's like two separate cycles from H to each shop. So the minimal could be less than the TSP tour if the direct distances are large. Indeed the TSP tour length is d1+d2+d3 (since the triangle has all three vertices, the tour would be H-A-B-H or H-B-A-H, both sum to d1+d2+d3). But we can also do H-A-H-B-H, which sum to 2d1+2d2. This could be smaller if d3 is large. Also we can do H-A-B-A-H (or H-B-A-B-H), which sum to 2d1+2d3 or 2d2+2d3. This could be smaller if one of d1 or d2 is large.
Thus the four candidates capture all ways to "cover" both A and B with a closed walk: either go from H to each separately (two cycles) or go to one, then the other, then back (triangle) or go to one, then the other, then back via the first (which essentially uses one of the direct H-? edges twice and the shop-shop edge twice) or the symmetric.
But is there a route like H->A->B->H->A? That doesn't end at H.
Thus answer is min of those four.
Edge case: What if one of d1, d2, d3 is 0? According to constraints they are >=1, so no zero.
So solution: read three ints, compute the four values, output the min.
But wait, check if any other candidate like 2d1 + d2 + d3? Could that be less than min of the four in some scenario? Let's try to find if there is a walk that yields total = 2d1 + d2 + d3. For example, H->A->B->H (d1+d3+d2) then extra? No, that's d1+d2+d3. To get 2d1 + d2 + d3, we could do H->A->H->B->A->H? That's d1 (to A) + d1 (back) + d2 (to B) + d3 (to A) + d1 (to H) = 2d1 + d2 + d3 +? Actually compute: H->A: d1; A->H: d1; H->B: d2; B->A: d3; A->H: d1. That's 3d1 + d2 + d3, not 2d1+d2+d3. To get 2d1+d2+d3, perhaps H->A->B->H->A->H? That would be d1 + d3 + d2 + d1 + d1? = 3d1+d2+d3. No.
What about H->A->B->A->H? That is 2d1+2d3. Not.
What about H->B->A->H->B->H? That's 2d2+2d1? Not.
So 2*d1+d2+d3 doesn't appear to be a natural closed walk covering both shops, because you need to start and end at H, and you must visit both A and B. Typically the walk will have even number of traversals of each edge? Not necessarily. But we can attempt to construct a walk with total T. Represent the walk as a multiset of edges traversed, with the condition that the degree of H must be even (since start and end at H, the number of times we leave H equals number of times we arrive H, so total degree of H in the traversed multigraph is even). For A and B, the degree may be odd or even? Actually since we are not required to start/end at them, but we might pass through them. For any vertex except start/end, the number of times we enter equals number of times we leave, so degree is even. For H, also even because start = end. So all vertices must have even degree in the traversed multigraph (Eulerian circuit condition for closed walk). Wait, any closed walk (starting and ending at same vertex) in an undirected graph will have even degree for all vertices? Let's think: In a walk, edges are traversed, and we count each traversal as contributing to the degree count for the vertex. For a closed walk that starts and ends at the same vertex, each time we visit a vertex (except maybe start/end) we arrive and depart, contributing 2 to its degree. For the start/end vertex, the initial departure and final arrival also contribute 2, so indeed each vertex has even degree in the multiset of traversed edges. So the multiset of edges traversed must form an Eulerian multigraph where all vertices have even degree. That is a necessary condition. Conversely, any connected multigraph with all vertices even degree has an Eulerian circuit. So the problem reduces to: we need to select a multiset of edges (with multiplicities) that includes at least one edge incident to A and at least one edge incident to B? Actually we need to ensure that both shops are visited. In an Eulerian circuit, if a vertex has degree zero, it's not visited. So we need to construct an Eulerian multigraph on {H, A, B} where each vertex has even degree (>=0), and A and B have degree >0 (since we need to visit them; actually we could visit a shop even if its degree is >0; if degree is 0, we never go there). Also, H must have degree >0 because we start and end there, but we can have degree 0? Actually if we start at H and never leave, we can't visit shops. So H degree >0. So we need to pick a multiset of edges (with non-negative integer counts) such that degrees are even, A degree >0, B degree >0, H degree >0, and the sum of lengths times multiplicities is minimized.
This is a nice approach. Since there are only three edges, we can denote x = number of times we traverse edge H-A, y = number of times traverse H-B, z = number of times traverse A-B. All are nonnegative integers, and the total distance = xd1 + yd2 + z*d3.
Degree conditions:
deg(H) = x + y must be even.
deg(A) = x + z must be even.
deg(B) = y + z must be even.
Also, we need to visit A and B: so deg(A) > 0 and deg(B) > 0. Also deg(H) > 0 (but if deg(H)=0, we wouldn't start? Actually starting at H with deg(H)=0 means we don't move, but then we can't visit shops, so need deg(H)>0). And we must be able to form an Eulerian circuit that starts and ends at H. Provided the graph (the multiset) is connected? Actually Eulerian circuit exists if the multigraph is connected (ignoring isolated vertices) and all vertices have even degree. However, connectivity: We need to be able to traverse all edges and start/end at H. If the multigraph is not connected, we cannot have a single closed walk covering all edges? Actually if it's not connected, there is no Eulerian circuit that covers all edges because you cannot go from one component to another. But we could have edges in multiple components, but we need a single walk that traverses all chosen edges (since total distance is sum of traversals). For a closed walk to traverse all edges exactly as many times as chosen, the underlying multigraph must be connected (or else you cannot go between components). However, it's possible that some vertices have degree 0 and are isolated, but they wouldn't be visited. So we need the subgraph consisting of edges with positive multiplicity to be connected and include H, A, B? Actually if A or B have degree >0, they must be in the same component as H, because otherwise we cannot start at H and visit them. So we need connectivity across all vertices with positive degree. So the multigraph must be connected on the set {H, A, B} (if all have degree >0). But it could be that one of A or B has degree 0? That would violate visit requirement. So both A and B have degree >0, so they are in the component. Thus the multigraph must be connected.
Now we want to minimize total distance given these constraints. Since edges are positive, we want to minimize the multiplicities while satisfying parity and positivity and connectivity.
Let's solve this integer optimization problem.
Variables x, y, z ∈ Z_{\ge 0}. Need:
- x+y even
- x+z even
- y+z even
This implies all pairwise sums even, which is equivalent to x, y, z all having the same parity (all even or all odd). Because if x+y even and x+z even, then subtracting gives (y-z) even => y and z same parity. Then x shares parity with y (since x+y even) so x parity same as y. So all three same parity.
- x+z > 0 (since deg(A)>0), but more strictly, deg(A) = x+z > 0. Similarly y+z > 0, and x+y > 0 (since deg(H)>0).
- Additionally, connectivity: The graph must be connected. With three vertices, connectivity means that not all edges are zero? Actually if we have only edges H-A (x>0) and H-B (y>0) but no A-B (z=0), the graph is still connected because H connects to both A and B via H. That's connected (since H is connected to A and B). If we have only A-B (z>0) and no edges to H, then H would be isolated, so not connected. So as long as H has at least one edge to either A or B, and A and B are connected to H (directly or via each other), the graph is connected. More formally, the graph is connected if there is a path between any two vertices. With three vertices, the condition is: not (x=0 and y=0 and z=0) obviously; and H must be connected to the rest. If x>0 or y>0 then H is connected to at least one of A,B. Then if both A and B are connected to H, either directly (x>0 for A, y>0 for B) or via each other (z>0 and one of x or y >0). For example, if x>0 and y=0 but z>0, then H connected to A, A connected to B via z, so H connected to B via A, so graph connected. Similarly if y>0 and x=0 and z>0, connected. If only x>0 and y=0 and z=0, then A is connected to H, but B is isolated (since no edges incident to B), so graph not connected, but B's degree would be 0, violating requirement that deg(B)>0. So that case cannot happen because we require deg(B)>0. Similarly only y>0, x=0, z=0 violates deg(A)>0. If only z>0, then H has degree 0, violating deg(H)>0. So connectivity automatically enforced if deg(A)>0, deg(B)>0, deg(H)>0 and all three vertices have positive degree, and the graph has edges among them? Could there be a case where all degrees positive but graph is disconnected? With three vertices, if we have edges only between A and B (z>0) and between H and itself? No self-loops. So if only z>0, then deg(H)=0. So not positive. If we have edges H-A (x>0) and B only connected via? If we have x>0 and also some edges only incident to B? To have deg(B)>0, we need either y>0 or z>0. If we have x>0 and y>0, graph connected. If we have x>0 and z>0, then H-A and A-B gives connectivity to B (since B is incident to z). So graph connected. Similarly y>0 and z>0 gives connectivity. So as long as each vertex has positive degree, the graph will be connected automatically? Let's check edge case: Suppose x>0, y=0, z>0: deg(H)=x>0, deg(A)=x+z>0, deg(B)=z>0. Graph: edges: H-A and A-B. This yields path H-A-B, so connected. Similarly x=0, y>0, z>0 gives H-B-A, connected. If x>0, y>0, z=0, then edges: H-A and H-B, connected via H. So indeed if all three vertices have positive degree, the graph is connected. However, is it possible that a vertex has positive degree but still isolated? No, if it has positive degree, it is incident to at least one edge, which connects it to another vertex. So as long as the graph is not composed of separate components that each contain at least one vertex, but with three vertices and edges connecting them, having all degrees positive forces that there is at least one edge incident to each vertex, and those edges must connect to other vertices. Could it be that the edges are such that A and B are connected to each other (z>0) but H only has edges to A (x>0) and no edge to B? That's connected. Could it be that H only has edges to A (x>0) and B only has edges to H? That would require y>0, so then both x and y >0, then H connects to both, connected. So yes, positivity of degrees ensures connectivity because the graph is small. But careful: Suppose x>0, y>0, z=0: deg(H)=x+y>0, deg(A)=x>0, deg(B)=y>0, so all positive, graph connected via H. So condition deg>0 for all three ensures connectivity. Therefore, we only need to enforce x,y,z >=0 integers, all same parity, and x+z>0, y+z>0, x+y>0. Minimize total = xd2 + z*d3.
Now we can try to find minimal solution.
We can consider two cases: all variables even or all odd.
Since we want to minimize, likely we will have small multiplicities: 0,1,2 maybe.
Because the distances are positive, we want as few traversals as possible, but we must satisfy parity and positivity.
Let's enumerate small possibilities:
First, note that we must visit both shops. The simplest idea: Use exactly one traversal of each edge? That would be x=y=z=1. Then degrees: deg(H)=2 (even), deg(A)=2 (even), deg(B)=2 (even). So parity condition satisfied (all odd? Actually 1,1,1 are all odd, so parity condition holds). So (1,1,1) is a valid multigraph: one copy of each edge. This yields total = d1+d2+d3. This corresponds to walking an Eulerian circuit on the triangle? Indeed, one copy of each edge forms a connected graph where each vertex degree is 2 (since each vertex has two incident edges: H has edges to A and B, degree 2; A has edges to H and B, degree 2; B has edges to H and A, degree 2). This graph is Eulerian (each degree even). An Eulerian circuit would be a closed walk that traverses each edge exactly once. That's exactly the triangle tour H-A-B-H or H-B-A-H. So (1,1,1) is valid and yields total d1+d2+d3.
Second, consider using two copies of two edges, and zero for the third? For instance, x=2, y=2, z=0. Then degrees: H:4 even, A:2 even, B:2 even. Parity: 2,2,0? Wait z=0 is even, but we need all same parity: 2,2,0 are not all same parity because 0 is even, 2 is even, so all even? Actually parity refers to even/odd. 2 is even, 0 is even. So all even => same parity (all even). So parity condition holds. However, we need deg(A)=x+z=2>0, deg(B)=y+z=2>0, deg(H)=4>0. So valid. This multigraph: edges: two copies of H-A and two copies of H-B. That's like two separate loops? Actually two parallel edges between H and A, two between H and B. Graph is connected (H connects to both). It is Eulerian? H degree 4, A degree 2, B degree 2, all even. There exists an Eulerian circuit that starts at H and traverses all edges. For example, go H-A-H-B-H-A-H-B-H? Actually you need to traverse each edge exactly once (but there are multiple edges). An Eulerian circuit exists. The total distance = 2d1 + 2d2. This corresponds to route H-A-H-B-H (but that uses only one copy each? Actually H-A-H-B-H uses each edge H-A twice? Let's see: H-A (d1), A-H (d1) => that's two traversals of H-A? Actually in a walk, each traversal counts. The walk H->A->H->B->H uses H-A edge twice (once each direction) and H-B edge twice? Actually H->A uses H-A once, A->H uses the same edge H-A again (back), so that's two traversals of H-A. Then H->B and B->H are two traversals of H-B. So total 2d1 + 2d2. So yes (2,2,0) corresponds to that walk.
Similarly, (2,0,2): x=2, y=0, z=2. Then degrees: H:2, A:4, B:2? Actually deg(H)=x+y=2, deg(A)=x+z=4, deg(B)=y+z=2. All even? 2,4,2 all even, parity okay. But we need y=0, so deg(B)=z=2>0, deg(A)=2+2=4>0, deg(H)=2>0, valid. However, does y=0 mean no edge between H and B? But there is edge A-B (z=2) and H-A (x=2). So graph: two parallel edges H-A, two parallel edges A-B. Is H connected to B? Yes, via A. So connected. This yields total = 2d1 + 2d3. Corresponds to walk H-A-B-A-H (but using each edge twice?) Let's think: H-A (d1), A-B (d3), B-A (d3), A-H (d1) = 2d1 + 2d3. That's two traversals of H-A and two traversals of A-B. Indeed (2,0,2) works. But note parity: all even.
Similarly, (0,2,2): total 2d2 + 2d3.
What about using odd numbers other than (1,1,1)? For example, (1,1,0): x=1, y=1, z=0. Degrees: H=2 even, A=1 odd, B=1 odd. So A and B have odd degree => not allowed for Eulerian circuit because all vertices must have even degree for closed walk? Wait earlier we argued that any closed walk yields even degrees at all vertices. Indeed, if we traverse edges and return to start, the number of times we enter and leave each vertex is equal, so degree (count of edge traversals incident) must be even. So degrees must be even. So (1,1,0) is not valid because deg(A) and deg(B) are odd. Could we have a closed walk that yields those degrees? Let's test: Consider walk H->A->H->B->H. That's H-A-H-B-H. The traversed edges: H-A (once going H->A), A-H (once going A->H), H-B (once going H->B), B-H (once going B->H). So edges used: H-A twice? Actually careful: In the sequence, edges are: first: H-A (d1) is one traversal of H-A in direction H->A. Then A-H (d1) is a second traversal of H-A? Actually the edge H-A is undirected, but each time you travel it, you count one traversal. So H-A is traversed twice: once from H to A, once from A to H. So that's two traversals of H-A. Similarly H-B is traversed twice. So multiplicities: x=2, y=2, z=0. That's (2,2,0). So indeed (1,1,0) would correspond to using each edge H-A and H-B only once each, which would not allow returning to H after visiting both? Let's see if a closed walk with x=1,y=1,z=0 is possible: It would mean we traverse H-A exactly once and H-B exactly once. Starting at H, if we traverse H-A once, we go to A. From A, we have only traversed H-A once (used), and we have no other edges incident to A (since z=0). So we cannot leave A (we would be stuck). So not possible. So indeed degrees must be even.
Thus the parity condition (all even) is necessary. However, we earlier derived that all three must have same parity from the three equations. For (1,1,1), all are odd, which also yields even degrees? Wait deg(H)=1+1=2 even, deg(A)=1+1=2 even, deg(B)=1+1=2 even. So (1,1,1) gives even degrees even though the variables are odd? Because the sum of two odds is even. So the condition for even degrees is: x+y even, x+z even, y+z even. That's exactly the parity condition we wrote. It does not require each variable individually to be even; it requires pairwise sums even. That is equivalent to x, y, z all having the same parity (all even or all odd). So the variables can be all odd. For (1,1,1), they are all odd, but each degree is even. So allowed. So parity condition: x ≡ y (mod 2) and x ≡ z (mod 2). So all same parity.
Thus possible multiplicities can be (odd, odd, odd) or (even, even, even). So minimal total might come from small odd numbers like (1,1,1) or small even numbers like (2,2,0), (2,0,2), (0,2,2), (2,2,2?) but (2,2,2) would be larger.
Could there be a solution with (1,1,3) or something? That would have all odd, but degrees: H=2, A=4, B=4? Actually x=1,y=1,z=3 gives deg(H)=2 even, deg(A)=4 even, deg(B)=4 even. So parity condition satisfied. Total = d1+d2+3*d3. That's larger than d1+d2+d3 unless d3 is negative, so not minimal.
What about (1,3,1) etc.
Could there be a solution with (0,0,2)? z=2, x=y=0. Then deg(H)=0 not >0, so invalid.
(0,2,0): y=2, x=z=0 => deg(H)=2, deg(A)=0 (violates A>0), invalid.
(2,0,0): invalid similarly.
So the only viable configurations with small total are those where we have at most two edges with positive multiplicity, but we need all three vertices to have positive degree. If we use only two edges, say H-A and H-B (x>0,y>0,z=0), then deg(A)=x>0, deg(B)=y>0, deg(H)=x+y>0. For degrees to be even, we need x and y even? Because x+z = x must be even (since deg(A) even) so x even. Similarly y even (since y+z = y even). Also x+y even automatically if both even. So we need x even, y even, and positive. Minimum even positive is 2. So (2,2,0) is minimal for that pair. Similarly, for H-A and A-B: x>0, z>0, y=0. Then deg(H)=x must be even (since x+y = x even), deg(A)=x+z must be even, deg(B)=z must be even (since y+z = z even). So x even, z even, and positive. Minimum even positive is 2. So (2,0,2). For H-B and A-B: (0,2,2). So those are minimal for two-edge configurations.
For three-edge configuration, we could have (1,1,1) (all odd) which uses each edge once. But could we have (1,1,1) as the minimal three-edge? Yes. Could there be a configuration with one edge used 0 and the others used 1? No because then a vertex degree would be odd. For example, (1,1,0) yields deg(A)=1 odd, invalid. So (1,1,1) is the only minimal all-odd configuration with all positive and small numbers.
But is (1,1,1) always valid? Yes. But wait, connectivity condition: (1,1,1) yields a connected graph (triangle). So it's valid.
Now are there any configurations with all even numbers that are smaller than (2,2,0) or (2,0,2) etc? For example, (2,0,0) invalid because deg(A)=0. So no.
What about (2,2,2) total = 2*(d1+d2+d3) > (1,1,1) obviously.
Thus the candidate totals are exactly:
-
From (1,1,1): d1+d2+d3
-
From (2,2,0): 2d1+2d2
-
From (2,0,2): 2d1+2d3
-
From (0,2,2): 2d2+2d3
Now, could there be a configuration like (1,3,1) that gives total d1+3d2+d3? That could be less than some of the above if d2 is very small? But it's at least d1+d2+d3 plus 2d2, so larger. So not minimal.
But what about (1,1,3) vs (1,1,1) obviously larger.
Thus the minimum over all integer solutions should be the minimum of these four. However, we should double-check if there is any configuration that yields a total less than these four. Could there be a configuration with (0,2,4) etc? No, larger.
But wait, consider case where d1 is extremely large, d2 is moderate, d3 is moderate. The best might be to use (0,2,2) = 2d2+2d3. But could we use (1,1,1) = d1+d2+d3, which might be larger if d1 huge. Could we use something like (1,2,1)? Check parity: 1 (odd), 2 (even), 1 (odd) -> not all same parity, so parity condition fails. But maybe we can have a closed walk that does not correspond to a simple Eulerian multigraph? Actually any closed walk must have even degree at all vertices. So parity condition is necessary. So (1,2,1) would give deg(A)=1+1=2 even, deg(B)=2+1=3 odd, so invalid.
What about (2,1,1)? deg(H)=3 odd, invalid.
Thus the only low multiplicities that satisfy even degrees are those we listed.
But what about using a configuration where we use edges more than once but not all even? Must have even degrees, which forces the parity conditions as described. So minimal multiplicities that achieve positive degrees and even degrees are exactly: either use all three edges each once (odd multiplicities but each vertex degree even due to sums) or use two edges each twice (even multiplicities). There is also possibility of using one edge four times and others zero? But then the zero-edge vertices would have degree zero, violating positivity. So not allowed.
Could there be a configuration with (4,0,2)? That would have x=4, z=2, y=0. Then deg(H)=4 even, deg(A)=6 even, deg(B)=2 even. Positive? deg(B)=2>0, okay. But total = 4d1+2d3, which is >= 2d1+2d3 because 4d1 >= 2d1. So not better.
(2,2,2) total = 2*(d1+d2+d3) > (1,1,1) unless d1+d2+d3 negative (impossible). So not minimal.
Thus indeed the minimum is min of the four.
But hold on: What about configuration (0,4,2)? That would be 4d2+2d3. That's larger than 2d2+2d3.
Thus the answer is min(d1+d2+d3, 2d1+2d2, 2d1+2d3, 2d2+2d3).
We must also consider whether there is a possibility of using edges with multiplicity 0 but other edges with multiplicity 1? Already invalid because degree would be odd.
Now let's verify with some edge cases.
Case 1: d1=1, d2=2, d3=3 -> min of: 6, 6, 8, 10 =6.
Case 2: d1=1, d2=100, d3=1 -> min: 102, 202, 4, 202? Actually compute: 2d1+2d3 = 2+2=4; 2d2+2d3 = 200+2=202; d1+d2+d3=102; 2d1+2d2=2+200=202; min=4. Good.
Case 3: d1=100, d2=1, d3=1 -> min: 102, 202, 202, 4? Actually 2d2+2d3 = 2+2=4; so min=4. Valid.
Case 4: d1=100, d2=100, d3=1 -> min: 201, 400, 202, 202? Actually 2d1+2d3=200+2=202, 2d2+2d3 same, d1+d2+d3=201, 2d1+2d2=400. So min=201. That's d1+d2+d3. Good.
Case 5: d1=5, d2=5, d3=100 -> min: 110, 20, 210, 210 =20 (2d1+2d2). Good.
Now, could there be a scenario where 2d1+2d2 is less than both d1+d2+d3 and the others? Yes when d3 is large.
Now what about scenario where d3 is very large and d1 and d2 also large but one is slightly larger? 2d1+2d3 might be less than others? Possibly if d2 is huge.
Now, is there any scenario where 2d1+2d2 is not feasible because maybe the graph is not connected? But we argued it is connected because all vertices have positive degree. In (2,2,0), deg(A)=2>0, deg(B)=2>0, deg(H)=4>0. So connected. The Eulerian circuit exists. So it's feasible.
But note: In the walk corresponding to (2,2,0), we need to traverse two copies of H-A and two copies of H-B. A valid Eulerian circuit would be something like: start H, go to A via one H-A edge, come back to H via that same edge? But careful: In an Eulerian circuit on a multigraph with two parallel edges between H-A and two parallel edges between H-B, we need to design a circuit that uses each edge exactly once. There exists such a circuit. For example: H->A (edge1), A->H (edge2? Actually need to distinguish edges). But in an undirected multigraph with two parallel edges between H and A, you can traverse one from H to A, then later from A to H on the other. Similarly for H-B. A possible circuit: H -A (first HA), A-H (second HA), H-B (first HB), B-H (second HB). This yields exactly H->A->H->B->H. That is a valid closed walk. So yes.
For (2,0,2): H-A (edge1), A-B (edge1), B-A (edge2), A-H (edge2). That's H->A->B->A->H. So valid.
For (0,2,2): H->B->A->B->H etc.
Thus all are valid.
So answer is as above.
Now, I recall that in the Codeforces problem "Patrick and Shopping", the solution is indeed min(2*(d1+d2), d1+d2+d3, 2*(d1+d3), 2*(d2+d3)). Actually they might have written min(2d1+2d2, 2d1+2d3, 2d2+2d3, d1+d2+d3). So that's it.
But wait, some solutions also consider 2*(d1+d2) and 2*(d2+d3) and 2*(d1+d3). That's same.
Now check examples:
Input: 10 20 30 -> d1=10,d2=20,d3=30. Compute: d1+d2+d3=60, 2d1+2d2=60, 2d1+2d3=80, 2d2+2d3=100. min=60. Output 60. Good.
Second example: 1 1 5 -> d1+d2+d3=7, 2d1+2d2=4, 2d1+2d3=12, 2d2+2d3=12 -> min=4. Output 4. Good.
Thus solution is straightforward.
But is there any nuance? The problem statement says "Patrick always starts at his house. He should visit both shops moving only along the three existing roads and return back to his house. He doesn't mind visiting the same shop or passing the same road multiple times. The only goal is to minimize the total distance traveled." So yes.
Thus implement:
Read three ints d1,d2,d3.
Compute a = d1 + d2 + d3
b = 2d1 + 2d2
c = 2d1 + 2d3
d = 2d2 + 2d3
Answer = min(a,b,c,d)
Print answer.
But we can simplify: min(d1+d2+d3, 2d1+2d2, 2d1+2d3, 2d2+2d3)
Edge cases: all positive, no overflow? Up to 1e8, sums up to 21e8+21e8=4e8, within 32-bit signed int (max ~2e9). But in C++ int might be 32-bit and max 2,147,483,647, so 400,000,000 is fine. But if we use Python, it's fine.
Thus solution is trivial.
But we can also reason: The answer could be computed as min(d1+d2+d3, 2d1+2d2, 2d1+2d3, 2d2+2d3). That is final.
Now I'll produce final answer.
But we can also simplify further: min(d1+d2+d3, 2*(d1+d2), 2*(d1+d3), 2*(d2+d3)). Many solutions.
Thus final.
Now for the response: Provide solution in Python as per request? The user didn't specify language, but we can provide explanation and code. Typically we give solution in Python.
I'll write:
def min_distance(d1, d2, d3):
return min(d1+d2+d3, 2*(d1+d2), 2*(d1+d3), 2*(d2+d3))
Or compute four values.
Now produce final answer. The minimum distance is the smallest among four possible routes:
- Go to the first shop, then to the second, then directly home: (d_1 + d_2 + d_3).
- Go to each shop separately and return home each time: (2d_1 + 2d_2).
- Go to the first shop, then to the second, then back to the first, then home: (2d_1 + 2d_3).
- Go to the second shop, then to the first, then back to the second, then home: (2d_2 + 2d_3).
Thus the answer is
[
\min(d_1 + d_2 + d_3,; 2(d_1 + d_2),; 2(d_1 + d_3),; 2(d_2 + d_3)).
]
def solve():
d1, d2, d3 = map(int, input().split())
ans = min(d1 + d2 + d3,
2 * (d1 + d2),
2 * (d1 + d3),
2 * (d2 + d3))
print(ans)
if __name__ == "__main__":
solve()