/*
DP: f[i][j] = min( f[i-1][k]+p[k][j][i-1] )
*/
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <iostream>
using namespace std;
const int INF = 1<<30;
const int MAXN = 12, MAXK = 1002, MAXD = 32;
int n, k;
int p[MAXN][MAXN][MAXD];
int f[MAXK][MAXN];
int dp(int i, int j)
{
if(i == 1) {
if(j==0) return 0;
else return -2;
}
int& ans = f[i][j];
if(ans != -1) return ans;
int l, d, v;
int m = INF;
for(int k=0; k<n; k++) {
if(k==j) continue;
l = p[k][j][0];
d = i-1<=l ? i-1 : i-1-(i-2)/l*l;
if(p[k][j][d]) {
v = dp(i-1, k);
if(v != -2 && v+p[k][j][d] < m) {
m = v+p[k][j][d];
}
}
}
return ans = m==INF ? -2 : m;
}
int main(){
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
#endif
int T=0;
while(scanf("%d%d", &n, &k)==2 && n) {
memset(p, 0, sizeof(p));
int d;
for(int i=0; i<n; i++) {
for(int j=0; j<n; j++) {
if(i==j) continue;
scanf("%d", &d);
p[i][j][0] = d;
for(int k=1; k<=d; k++) {
scanf("%d", &p[i][j][k]);
}
}
}
memset(f, -1, sizeof(f));
printf("Scenario #%d\n", ++T);
int v = dp(k+1, n-1);
if(v == -2) {
printf("No flight possible.\n\n");
} else {
printf("The best flight costs %d.\n\n", v);
}
}
return 0;
}
UVa 590 Always on the run
最新推荐文章于 2020-08-03 15:46:12 发布