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 |
#include <iostream> #include <vector> #include <queue> using namespace std; int value[104]; vector<int> vb[104]; int visit[104]; queue<int> ue; int learned; int _next; int findAns(int a) { int _max=0; if(vb[a].size()==0) { _next=a; //learned+=value[a]; return 0; } else { for(int i=0;i<vb[a].size();i++) { if(value[vb[a][i]]>_max) { _max=value[vb[a][i]]; _next=vb[a][i]; } } learned+=_max; return findAns(_next); } return 0; } int main() { int t,n,m; int a,b,c=1; cin>>t; while(t--) { cin>>n>>m; for(int i=0;i<n;i++) { cin>>value[i]; vb[i].clear(); visit[i]=0; } for(int i=0;i<m;i++) { cin>>a>>b; vb[a].push_back(b); } learned=0; learned+=value[0]; findAns(0); cout<<"Case "<<c++<<": "; cout<<learned<<" "<<_next<<"\n"; } return 0; } |