#include<stdio.h> #include<string.h> #define maxn 105 int a[maxn],dp[maxn],map[maxn][maxn],pre[maxn]; int max(int x,int y) { return x>y?x:y; } void print(int p) { if(p==1)//一定从1点出发 { printf("1"); return ; } print(pre[p]); printf("->%d",p); } int main() { int i,j,t,n,m,ff=0; scanf("%d",&t); while(t--) { scanf("%d",&n); for(i=1;i<=n;i++) { scanf("%d",&a[i]); } a[n+1]=0; scanf("%d",&m); memset(dp,0,sizeof(dp)); memset(map,0,sizeof(map)); for(i=1;i<=m;i++) { int x,y; scanf("%d%d",&x,&y); map[x][y]=1; } for(i=1;i<=n+1;i++) { for(j=1;j<=n+1;j++) { if(map[i][j]&&dp[j]<dp[i]+a[j])//到j点的值要大 { dp[j]=dp[i]+a[j]; pre[j]=i; } } } printf("CASE %d#\n",++ff); printf("points : %d\n",dp[n+1]); printf("circuit : "); print(pre[n+1]); printf("->1\n"); if(t!=0) printf("\n"); } }
时间: 2024-11-08 14:39:03