递推DP UVA 590 Always on the run
生活随笔
收集整理的這篇文章主要介紹了
递推DP UVA 590 Always on the run
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
?
題目傳送門
題意:題意難懂,就是一個(gè)小偷在m天內(nèi)從城市1飛到城市n最小花費(fèi),輸入的是每個(gè)城市飛到其他城市的航班。
分析:dp[i][j] 表示小偷第i天在城市j的最小花費(fèi)。狀態(tài)轉(zhuǎn)移方程:dp[i][j] = min (dp[i-1][k] + cost[k][j][t%day]) t表示在t天時(shí)k飛往j的飛機(jī)的花費(fèi)
收獲:
?
代碼:
/************************************************ * Author :Running_Time * Created Time :2015-8-29 14:07:43 * File Name :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 | 1 typedef long 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];printf("Scenario #%d\n", ++cas);if(ans != INF){ printf("The best flight costs %d.\n\n", ans); }else{ puts("No flight possible.\n"); } }return 0; }
轉(zhuǎn)載于:https://www.cnblogs.com/Running-Time/p/4773806.html
總結(jié)
以上是生活随笔為你收集整理的递推DP UVA 590 Always on the run的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: UIView 学习知识点
- 下一篇: pat1049. Counting On