Editing
2015 Jan Bronze Problem 1 Cow Routing
(section)
Jump to navigation
Jump to search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Code == === C++ === <pre> #include <iostream> #include <climits> // To use INT_MAX instead of INF using namespace std; int main() { freopen("cowroute.in", "r", stdin); freopen("cowroute.out", "w", stdout); int A, B, N; // Declare variables for starting city, target city and number of flight routes cin >> A >> B >> N; int result = INT_MAX; // Initialize the result with the maximum possible integer value for (int i = 0; i < N; i++) { // Loop through each flight route int cost, sz; // Declare variables for the cost of the flight route and the number of cities it passes through cin >> cost >> sz; bool found_A = false; // Variable to check if city A is found in the flight route for (int j = 0; j < sz; j++) { // Loop through each city in the flight route int city; // Declare a variable for the current city cin >> city; if (city == A) { // If the current city is city A found_A = true; // Set found_A to true } // If city A is found and the current city is city B // Update the result with the minimum cost if (found_A && city == B) { result = min(result, cost); } } } // If no suitable flight route is found, print -1 // Otherwise, print the minimum cost if (result == INT_MAX) { cout << "-1\n"; } else { cout << result << '\n'; } return 0; } </pre> === Java === <pre> import java.io.*; import java.util.*; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String[] input = br.readLine().split(" "); int A = Integer.parseInt(input[0]); // Starting city int B = Integer.parseInt(input[1]); // Target city int N = Integer.parseInt(input[2]); // Number of flight routes int result = Integer.MAX_VALUE; // Initialize the result with the maximum possible integer value for (int i = 0; i < N; i++) { input = br.readLine().split(" "); int cost = Integer.parseInt(input[0]); // Cost of the flight route int sz = Integer.parseInt(input[1]); // Number of cities it passes through boolean found_A = false; // Variable to check if city A is found in the flight route input = br.readLine().split(" "); for (int j = 0; j < sz; j++) { // Loop through each city in the flight route int city = Integer.parseInt(input[j]); // Current city if (city == A) { // If the current city is city A found_A = true; // Set found_A to true } // If city A is found and the current city is city B // Update the result with the minimum cost if (found_A && city == B) { result = Math.min(result, cost); } } } // If no suitable flight route is found, print -1 // Otherwise, print the minimum cost System.out.println(result == Integer.MAX_VALUE ? "-1" : result); } } </pre> === Python === <pre> import sys A, B, N = map(int, input().split()) result = float('inf') # Initialize the result with the maximum possible integer value for _ in range(N): cost, sz = map(int, input().split()) found_A = False # Variable to check if city A is found in the flight route cities = list(map(int, input().split())) for city in cities: # Loop through each city in the flight route if city == A: # If the current city is city A found_A = True # Set found_A to true # If city A is found and the current city is city B # Update the result with the minimum cost if found_A and city == B: result = min(result, cost) # If no suitable flight route is found, print -1 # Otherwise, print the minimum cost if result == float('inf'): print("-1") else: print(result) </pre> [[Category:Yearly_2014_2015]] [[Category:Bronze]] [[Category:Arrays]] [[Category:Simulation]] [[Category:Greedy]]
Summary:
Please note that all contributions to Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
My wiki:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Navigation menu
Personal tools
Not logged in
Talk
Contributions
Create account
Log in
Namespaces
Page
Discussion
English
Views
Read
Edit
View history
More
Search
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Tools
What links here
Related changes
Special pages
Page information