Recursive DP UVA 590 always on the run

Source: Internet
Author: User

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 &amp ;& 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.