POJ3230——Travel
来源:互联网 发布:阿里云机房测速 编辑:程序博客网 时间:2024/04/30 23:40
Description
One traveler travels among cities. He has to pay for this while he can get some incomes.
Now there are n cities, and the traveler has m days for traveling. Everyday he may go to another city or stay there and pay some money. When he come to a city ,he can get some money. Even when he stays in the city, he can also get the next day's income. All the incomes may change everyday. The traveler always starts from city 1.
Now is your turn to find the best way for traveling to maximize the total income.
Input
There are multiple cases.
The first line of one case is two positive integers, n and m .n is the number of cities, and m is the number of traveling days. There follows n lines, one line n integers. The j integer in the i line is the expense of traveling from city i to city j. If i equals to j it means the expense of staying in the city.
After an empty line there are m lines, one line has n integers. The j integer in the i line means the income from city j in the i day.
The input is finished with two zeros.
n,m<100.
Output
Sample Input
3 33 1 22 3 11 3 22 4 34 3 23 4 20 0
Sample Output
8
Hint
-1+4-2+4-1+4=8;
Source
简单dp题,我们用dp[i][j] 表示到第i天,在城市j时的最大收益,所以方程就很容易写出来了
dp[i][j] = max(dp[i-1][k] - mat[k][j] + w[i][j])
#include <map> #include <set> #include <list> #include <stack> #include <queue> #include <vector> #include <cstdio> #include <cmath> #include <cstring> #include <iostream> #include <algorithm> using namespace std;int dp[105][105];int mat[105][105];int w[105][105];int main(){int n, m;while (~scanf("%d%d", &n, &m)){if (!n && !m){break;}memset ( dp, -0x3f3f3f3f, sizeof(dp) );for (int i = 1; i <= n; i++){for (int j = 1; j <= n; j++){scanf("%d", &mat[i][j]);}}for (int i = 1; i <= m; i++){for (int j = 1; j <= n; j++){scanf("%d", &w[i][j]);}}dp[0][1] = 0;for (int i = 1; i <= n; i++){dp[1][i] = dp[0][1] - mat[1][i];}for (int i = 1; i <= m; i++){for (int j = 1; j <= n; j++){for (int k = 1; k <= n; k++){dp[i][j] = max(dp[i][j], dp[i - 1][k] - mat[k][j] + w[i][j]);}}}int ans = 0;for (int i = 1; i <= n; i++){ans = max(ans, dp[m][i]);}printf("%d\n", ans);}return 0;}
- POJ3230——Travel
- POJ3230 Travel (DP)
- POJ3230 - Travel - 动态规划
- POj3230
- poj3230 Travel(图中的DP,比较简单而且有意思)
- hdu4418——Time travel
- hdu 4284——Travel
- hdu(5441)——Travel
- zoj 3854——Travel by Bike
- 动态规划 III——C - Travel
- Travel
- Travel
- Travel
- codeforces——466A——Cheap Travel
- 大学英语六级听力——019(automobile and travel)
- HDU 4571——Travel in time(dp)
- HDU 4885 —— TIANKENG’s travel(最短路)
- Islands Travel——SPFA求最短路
- 01背包问题
- 在macBook pro上安装myeclipse 2014 和破解过程
- 用java实现AES加解密算法
- 银行家算法——软考探究(四)
- MFC绘图板(二)
- POJ3230——Travel
- 对于没有显式初始化的变量的初始值可以做怎样的假定?
- Windows C++ 线程池
- 线段树
- Leetcode: Substring with Concatenation of All Words
- uml中的那些图
- HTTP协议详解
- /* ios */NSdictionary 的使用
- 万丈高楼平地起之线程篇