-
Notifications
You must be signed in to change notification settings - Fork 0
/
uva590.cpp
114 lines (82 loc) · 1.84 KB
/
uva590.cpp
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <cstdio>
#include <iostream>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <limits.h>
#include <list>
using namespace std;
vector<vector<vector<int> > > cost;
int n, k;
int memo[15][1010];
int escape(int curent_city, int day)
{
if(day == k)
{
if(curent_city == n-1)
return INT_MAX;
int d = cost[curent_city][n-1][0]; //cycle length
int index = (day-1) % d + 1;
if(cost[curent_city][n-1][index] == 0)
return INT_MAX;
else
return memo[curent_city][day] = cost[curent_city][n-1][index];
}
if(memo[curent_city][day] != -1)
return memo[curent_city][day];
int best = INT_MAX;
for(int next_city = 0; next_city < n; next_city++)
{
if(next_city != curent_city)
{
int d = cost[curent_city][next_city][0]; //cycle length
int index = (day-1) % d + 1;
int aux = escape(next_city, day + 1);
if(aux != INT_MAX)
{
if(cost[curent_city][next_city][index] != 0)
best = min(best, cost[curent_city][next_city][index] + aux);
}
}
}
return memo[curent_city][day] = best;
}
int main()
{
int tc = 0;
while(true)
{
cin >> n; cin >> k;
if(n == 0 && k == 0)
break;
tc++;
cost.clear();
cost.resize(n);
int d;
for(int city = 0; city < cost.size(); city++)
{
cost[city].resize(n);
for(int next = 0; next < cost.size(); next++)
{
if(next != city)
{
cin >> d; /* number of days cycle */
cost[city][next].push_back(d);
cost[city][next].resize(cost[city][next][0] + 1);
for(int i = 1; i <= cost[city][next][0]; i++)
cin >> cost[city][next][i];
}
}
}
memset(memo, -1, sizeof(memo));
int rez = escape(0, 1);
printf("Scenario #%d\n", tc);
if(rez != INT_MAX)
printf("The best flight costs %d.\n", rez);
else
printf("No flight possible.\n");
printf("\n");
}
return 0;
}