1
#include<stdio.h> const int Max_City=200; const int Max_Ticket=20; const int Max_TicketLength=10; const int Max_TripLength=10; struct info { int cost,n; int city[Max_TicketLength]; }; int cases,NT,NI,i,j,k,l,tmp,trip,change,sum,n; int city[Max_City]; int list[Max_TripLength]; info ticket[Max_Ticket]; int f[Max_TripLength][Max_City][4]; int route[Max_TripLength * Max_City]; int find(int x) { int i; for(i=0;i<sum;i++) if(city[i]==x) return i; city[sum]=x; sum++; return (sum-1); } int main() { cases=0; while(1) { scanf("%d",&NT); sum=0; if(NT==0) break; else cases++; for(i=0;i<NT;i++) { scanf("%d %d",&ticket[i].cost,&ticket[i].n); for(j=0;j<ticket[i].n;j++) { scanf("%d",&tmp); ticket[i].city[j]=find(tmp); } } scanf("%d",&NI); for(trip=0;trip<NI;trip++) { scanf("%d",&n); for(i=0;i<n;i++) { scanf("%d",&tmp); list[i]=find(tmp); } for(i=0;i<n;i++) for(j=0;j<sum;j++) f[i][j][0]=-1; for(i=0;i<4;i++) f[0][list[0]][i]=0; for(i=0;i<n;i++) { while(1) { change=0; for(j=0;j<sum;j++) if(f[i][j][0]>=0) for(k=0;k<NT;k++) if(ticket[k].city[0]==j) { tmp=i+1; for(l=1;l<ticket[k].n;l++) { if(tmp<n&&list[tmp]==ticket[k].city[l]) tmp++; if(f[tmp-1][ticket[k].city[l]][0]<0 || f[tmp-1][ticket[k].city[l]][0]>f[i][j][0]+ticket[k].cost) { f[tmp-1][ticket[k].city[l]][0]=f[i][j][0]+ticket[k].cost; f[tmp-1][ticket[k].city[l]][1]=k; f[tmp-1][ticket[k].city[l]][2]=i; f[tmp-1][ticket[k].city[l]][3]=j; change++; } } } if(change==0) break; } } printf("Case %d, Trip %d: Cost = %d\n",cases,trip+1,f[n-1][list[n-1]][0]); l=0; i=n-1;j=list[n-1]; while(i||j!=list[0]) { route[l]=f[i][j][1]; k=f[i][j][2]; j=f[i][j][3]; i=k; l++; } printf(" Tickets used:"); for(i=l-1;i>=0;i--) printf(" %d",route[i]+1); printf("\n"); } } return 0; }
作者:li771537550 发表于2013-5-20 0:39:53 原文链接
阅读:113 评论:0 查看评论