NC24163. [USACO 2015 Jan B]Cow routing
描述
输入描述
The first line of input contains A, B, and N, separated by spaces.
The next 2N lines describe the available routes, in two lines per
route. The first line contains the cost of using the route (an integer
in the range 1..1000), and the number of cities along the route (an
integer in the range 1..500). The second line contains a list of the
cities in order along the route. Each city is identified by an
integer in the range 1..10,000.
输出描述
Output the minimum cost of a single route Bessie can use to travel
from city A to city B. If there is no such route, output -1.
示例1
输入:
1 2 3 3 3 3 2 1 4 4 2 1 4 3 8 5 4 1 7 8 2
输出:
8
说明:
Although there is a cheaper two-route solution (use route 2 to travelC++14(g++5.4) 解法, 执行用时: 35ms, 内存消耗: 1024K, 提交时间: 2020-09-12 10:59:15
#include<bits/stdc++.h> using namespace std; const int MAXN = 1e3 + 7; const int INF = 0x3f3f3f3f; vector<int> st[MAXN]; int c[MAXN]; void solve() { int a, b, n, ret = INF; cin >> a >> b >> n; for (int i = 1; i <= n; i++) { int co, k, x; cin >> c[i] >> k; for (int j = 1; j <= k; j++) { cin >> x; st[i].push_back(x); } int f1 = 0, f2 = 0; for (int j = 0; j < st[i].size(); j++) { if (st[i][j] == a) f1 = 1; if (f1 && st[i][j] == b) f2 = 1; } if (f1 && f2) ret = min(ret, c[i]); } cout << (ret == INF ? -1 : ret) << endl; } int main() { solve(); return 0; }
C++11(clang++ 3.9) 解法, 执行用时: 56ms, 内存消耗: 604K, 提交时间: 2019-08-14 13:55:36
#include<bits/stdc++.h> using namespace std; int main(){ int a,b,n,ans=2e9; cin>>a>>b>>n; for(int i=1;i<=n;i++){ int c,n2,x2=2e9,y2=-2e9; cin>>c>>n2; for(int j=1;j<=n2;j++){ int x; cin>>x; if(x==a)x2=j; if(x==b)y2=j; } if(y2-x2>0)ans=min(ans,c); } if(ans==2e9)cout<<-1<<"\n"; else cout<<ans<<"\n"; return 0; }