递推DP UVA 590 Always on the run
题意:题意难懂,就是一个小偷在m天内从城市1飞到城市n最小花费,输入的是每个城市飞到其他城市的航班。
分析:dp[i][j] 表示小偷第i天在城市j的最小花费。状态转移方程:dp[i][j] = min (dp[i-1][k] + cost[k][j][t%day]) t表示在t天时k飞往j的飞机的花费
收获:
代码:
/************************************************
* 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;
}
递推DP UVA 590 Always on the run的更多相关文章
- 递推DP UVA 607 Scheduling Lectures
题目传送门 题意:教授给学生上课,有n个主题,每个主题有ti时间,上课有两个限制:1. 每个主题只能在一节课内讲完,不能分开在多节课:2. 必须按主题顺序讲,不能打乱.一节课L时间,如果提前下课了,按 ...
- 递推DP UVA 1366 Martian Mining
题目传送门 /* 题意:抽象一点就是给两个矩阵,重叠的(就是两者选择其一),两种铺路:从右到左和从下到上,中途不能转弯, 到达边界后把沿途路上的权值相加求和使最大 DP:这是道递推题,首先我题目看了老 ...
- 递推DP UVA 1291 Dance Dance Revolution
题目传送门 题意:给一串跳舞的动作,至少一只脚落到指定的位置,不同的走法有不同的体力消耗,问最小体力消费多少分析:dp[i][j][k] 表示前i个动作,当前状态(j, k)的最小消费,状态转移方程: ...
- 递推DP UVA 1424 Salesmen
题目传送门 /* 题意:给定包含n个点的无向图和一个长度为L的序列,修改尽量少的点使得相邻的数字相同或连通 DP:状态转移方程:dp[i][j] = min (dp[i][j], dp[i-1][k] ...
- 递推DP UVA 473 Raucous Rockers
题目传送门 题意:n首个按照给定顺序存在m张光盘里,每首歌有播放时间ti,并且只能完整的存在一张光盘里,问最多能存几首歌 分析:类似01背包和完全背包,每首歌可存可不存,存到下一张光盘的情况是当前存不 ...
- 递推DP URAL 1167 Bicolored Horses
题目传送门 题意:k个马棚,n条马,黑马1, 白马0,每个马棚unhappy指数:黑马数*白马数,问最小的unhappy值是多少分析:dp[i][j] 表示第i个马棚放j只马的最小unhappy值,状 ...
- 递推DP URAL 1017 Staircases
题目传送门 /* 题意:给n块砖头,问能组成多少个楼梯,楼梯至少两层,且每层至少一块砖头,层与层之间数目不能相等! 递推DP:dp[i][j] 表示总共i块砖头,最后一列的砖头数是j块的方案数 状态转 ...
- 递推DP URAL 1260 Nudnik Photographer
题目传送门 /* 递推DP: dp[i] 表示放i的方案数,最后累加前n-2的数字的方案数 */ #include <cstdio> #include <algorithm> ...
- 递推DP URAL 1353 Milliard Vasya's Function
题目传送门 /* 题意:1~1e9的数字里,各个位数数字相加和为s的个数 递推DP:dp[i][j] 表示i位数字,当前数字和为j的个数 状态转移方程:dp[i][j] += dp[i-1][j-k] ...
随机推荐
- 【C++基础 02】深拷贝和浅拷贝
我的主题是.每天积累一点点. =========================================== 在类定义中,假设没有提供自己的拷贝构造函数,则C++提供一个默认拷贝构造函数. C ...
- 条款一:尽量使用const、inline而不是#define
#define ASPECT_RATIO 1.653 编译器会永远也看不到ASPECT_RATIO这个符号名,因为在源码进入编译器之前,它会被预处理程序去掉,于是ASPECT_RATIO不会加入到符号 ...
- centos 安装python2.7
安装pip sudo yum -y install epel-release sudo yum -y install python-pip 下载解压Python-2.7.3 #wget http:// ...
- [Angular] Write Compound Components with Angular’s ContentChild
Allow the user to control the view of the toggle component. Break the toggle component up into multi ...
- C++开发人脸性别识别教程(8)——搭建MFC框架之读取目录信息
在上一篇博客中我们已经绘制了MFC界面,在这篇博客中我们将加入响应代码,为MFC框架加入一个最主要的功能:打开一个目录. 一.加入相关头文件 这里头文件主要包括三类:opencv头文件.批量读取文件相 ...
- Windows环境下QWT安装及配置
** 1.QWT下载路径 **:https://sourceforge.net/projects/qwt/ 主要下载这三个文件:qwt-6.1.2.zip.qwt-6.1.2.pdf,qwt-6.1. ...
- 【计算机视觉】SIFT中LoG和DoG比較
在实际计算时,三种方法计算的金字塔组数noctaves,尺度空间坐标σ,以及每组金字塔内的层数S是一样的.同一时候,如果图像为640*480的标准图像. 金字塔层数: 当中o_min = 0,对于分辨 ...
- Android使用adb获得activity堆栈信息
很实用的命令: adb shell dumpsys activity 该命令能够看到当前执行的是哪个activity,执行的一些进程等 首先能够看到执行的进程: ACTIVITY MANAGER RU ...
- Windows Server 2012 R2 安装.NET Framework 3.5报错
简单记录一下,Windows Server 2012 R2 安装.NET Framework 3.5报错,下面是解决方法 载入ISO文件Windows Server 2012 R2,而且在安装的过程中 ...
- Codeforces Beta Round #22 (Div. 2 Only) E. Scheme dfs贪心
E. Scheme To learn as soon as possible the latest news about their favourite fundamentally new ope ...