uva590 Always on the run(DP)
#include <iostream> #include <cstdio> #define ll long long using namespace std; int n,t; ll dp[1010][12],a[12][12][33]; int c[12][12]; int main(){ //freopen("590","r",stdin); int cas=1; while(cin>>n>>t){ if(n==0&&t==0) break; for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++){ if(i==j) continue; cin>>c[i][j]; for(int k=0;k<c[i][j];k++){ cin>>a[i][j][k]; } } } for(int i=0;i<1010;i++){ for(int j=0;j<12;j++){ dp[i][j]=(ll)1e16; } } dp[0][1]=0; for(int i=1;i<=t;i++){ for(int j=1;j<=n;j++){ for(int k=1;k<=n;k++){ //cout<<j<<" "<<k<<endl; if(j==k||a[j][k][(i-1)%c[j][k]]==0) continue; dp[i][k]=min(dp[i][k],dp[i-1][j]+a[j][k][(i-1)%c[j][k]]); } } } printf("Scenario #%d\n",cas++); if(dp[t][n]==(ll)1e16) printf("No flight possible.\n"); else printf("The best flight costs %lld.\n",dp[t][n]); puts(""); } return 0; }