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
| #include <cstdio> #include <cmath> #include <string> #include <map> #include <iostream> #include <algorithm> #define M 305 #define N 505 #define E 105 #define Max(a,b) if(a<(b))a=(b) using namespace std; struct kxj{ int a,b,c; }s[E]; map<string,int>mp; string color[M],A,B,C; int n,m,R,tot; double dp[M][N][N],cel; int Id(string str){ if(mp[str]==0){ mp[str]=++tot; color[tot]=str; } return mp[str]; } int main(){ bool flag=0; while(~scanf("%d",&R)){ if(flag)puts(""); else flag=1; mp.clear();tot=0; for(int i=1;i<=R;i++){ cin>>A>>B>>C; int x=Id(A),y=Id(B),z=Id(C); s[i]=(kxj){x,y,z}; } scanf("%d",&m); while(m--&&scanf("%d",&n)){ for(int i=1;i<=n;i++) for(int k=1;k<=tot;k++)dp[k][i][i]=-1e99; for(int i=1;i<=n;i++){ while(cin>>A){ if(A=="END")break; scanf("%lf",&cel); int x=mp[A]; dp[x][i][i]=log(cel); } } for(int i=1;i<n;i++){ for(int l=1,r=l+i;r<=n;l++,r++){ for(int k=1;k<=tot;k++)dp[k][l][r]=-1e99; for(int j=l;j<r;j++){ for(int k=1;k<=R;k++){ int a=s[k].a,b=s[k].b,c=s[k].c; Max(dp[c][l][r],dp[a][l][j]+dp[b][j+1][r]); Max(dp[c][l][r],dp[b][l][j]+dp[a][j+1][r]); } } } } double mx=-1e99; int c=-1; for(int i=1;i<=tot;i++){ if(dp[i][1][n]>mx)mx=dp[i][1][n],c=i; } if(c==-1)puts("GAMEOVER"); else cout<<color[c]<<endl; } } return 0; }
|