1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
| #include <bits/stdc++.h> #define ll long long #define int long long #define double long double #define N 105 #define Orz ios::sync_with_stdio(0),cin.tie(0) #define INF 2e18 #define rep(i,l,r) for(int i=l;i<=r;i++) #define rrep(i,l,r) for(int i=l;i<r;i++) #define pii pair<int,int> #define pif pair<int,float> #define x first #define y second using namespace std; int n,k,visit[20];
struct Ticket{ int id,cost; vector<int> next_city; }; vector<Ticket> ticket[30];
struct node{ int cost,cur_pos,visit_pos; vector<int> used_ticket; };
struct cmp{ bool operator()(node a,node b){ return a.cost > b.cost; } }; priority_queue<node,vector<node>,cmp> pq;
signed main(){ Orz; cin>>n; rep(i,1,n){ int cost,num,s;cin>>cost>>num>>s; vector<int> temp; rep(j,1,num-1){ int k;cin>>k; temp.push_back(k); } ticket[s].push_back({i,cost,temp}); } cin>>k; rep(i,1,k)cin>>visit[i]; for(auto i : ticket[visit[1]]){ int p = 1; for(auto j : i.next_city){ if(p < k && j == visit[p+1])p++; pq.push({i.cost,j,p,{i.id}});
} } while(!pq.empty()){ node cur = pq.top(); pq.pop(); if(cur.visit_pos == k){ cout<<"Cost = "<<cur.cost<<", Tickets used: " <<cur.used_ticket[0]; for(int i=1;i<cur.used_ticket.size();i++) cout<<", "<<cur.used_ticket[i]; cout<<endl; break; } for(auto i : ticket[cur.cur_pos]){ vector<int> vec(cur.used_ticket); vec.push_back(i.id); int p = cur.visit_pos; for(auto j : i.next_city){ if(p < k && j == visit[p+1])p++; pq.push({cur.cost+i.cost,j,p,vec}); } } } }
|