Topic Portal
Test instructions: Test instructions is difficult to understand, is a thief in M days from the city 1 flew to the city N minimum cost, entered is each city flew to other cities of the flight.
Analysis: Dp[i][j] represents the thief's first day in the city J's minimum cost. State transfer equation: dp[i][j] = min (Dp[i-1][k] + cost[k][j][t%day]) T represents the cost of an aircraft flying to J on T-day K
Harvest:
Code:
/************************************************* author:running_time* Created time:2015-8-29 14:07:43* File Na Me:UVA_590.cpp ************************************************/#include <cstdio> #include <algorithm> #include <iostream> #include <sstream> #include <cstring> #include <cmath> #include <string > #include <vector> #include <queue> #include <deque> #include <stack> #include <list># Include <map> #include <set> #include <bitset> #include <cstdlib> #include <ctime>using namespace std; #define Lson L, Mid, RT << 1#define Rson mid + 1, R, RT << 1 | 1typedef long ll;const int N = 1e5 + 10;const int INF = 0x3f3f3f3f;const int MOD = 1e9 + 7;int dp[1010][12];int d[12] [12];int Cost[12][12][32];int Main (void) {int n, m, cas = 0;while (scanf ("%d%d", &n, &m) = = 2) {if (n = = 0 & ;& m = = 0) break;for (int i=1; i<=n; ++i) {for (int j=1; j<=n; ++j) {if (I! = j) {scanf ("%d", &d[i][j]), for (int k=0; k<d[i][j]; ++k) {scanf ("%d", &cost[i][j][k]);}}} Memset (DP, INF, sizeof (DP)), for (int i=2; i<=n; ++i) {if (cost[1][i][0]) {dp[0][i] = cost[1][i][0];}} for (int i=1, i<m; ++i) {for (Int. k=1; k<=n; ++k) {for (int j=1; j<=n; ++j) {if (j! = k) {int c = cost[j][k][i%d[j][k ]];if (c) dp[i][k] = min (Dp[i][k], dp[i-1][j] + c);}}} int ans = dp[m-1][n];p rintf ("Scenario #%d\n", ++cas); if (ans! = INF) {printf ("The best flight costs%d.\n\n", ANS); }else{puts ("No flight possible.\n"); }} return 0;}
Recursive DP UVA 590 always on the run