虽然它出现在dp专场里···但是我第一反应是一道最短路题···不过幸好它出现在dp专场里···因为我不怎么会dijstra什么的···

题意:一条河上有N+1对码头,每个相邻码头之间需要一定时间到达,每对码头之间也需要一定时间到达,如下图:

给出起点码头和终点码头,问最短到达时间。

解法:假设起点码头一定在左边,终点码头一定在右边,那么为了时间最短一定是向右或向上(下)走,所以dp数组为每个从起点出发到每个码头时的最短耗时,dp方程很好确定,但是上下码头的顺序无法确定,而当路径是由对面码头过来的时候,对面码头的dp值一定不是从当前码头而来,dp方程dp[0][i] = min(dp[0][i - 1] + time[0][i - 1], dp[1][i - 1] + time[1][i - 1] + time[2][i]),dp[1][i]
= min(dp[1][i - 1] + time[1][i - 1], dp[0][i - 1] + time[0][i - 1] + time[2][i])。

但从起点(终点)到它对面的时间需要另外处理,例如如下情况:

显然从南方的码头出发向右绕一圈后所需时间较短,所以要分别对起点通过左面路径到达对面码头的最短时间和终点通过右面路径到达对面码头的最短时间进行处理,绕行走的一定是U形路,所以只要计算所有U形路的最小时间,不过我还是用的dp写的···不是很好理解···

对于终点的绕行时间在最后要特殊处理,因为绕行时间和中间路径的dp过程有矛盾的地方。

一开始TLE了···但是我听说了某喵的惨痛经历后把memset改掉之后1900msA掉了···郁闷

代码:

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<string.h>
#include<math.h>
#include<limits.h>
#include<time.h>
#include<stdlib.h>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
#define LL long long
using namespace std;
struct node
{
long long num;
int dir;
};
long long dp[2][1000005];
long long c[3][1000005];
int main()
{
int n;
while(~scanf("%d", &n) && n)
{
for(int i = 0; i < 2; i++)
for(int j = 0; j < 1000005; j++)
dp[i][j] = -1;
//memset(dp, -1, sizeof(dp));
node st, fi;
scanf("%d%lld%d%lld", &st.dir, &st.num, &fi.dir, &fi.num);
if(st.num > fi.num)
{
node tmp = st;
st = fi;
fi = tmp;
}
dp[st.dir][st.num] = 0;
dp[fi.dir][fi.num] = 0;
for(int i = 0; i < n; i++)
scanf("%lld", &c[0][i]);
for(int i = 0; i <= n; i++)
scanf("%lld", &c[2][i]);
for(int i = 0; i < n; i++)
scanf("%lld", &c[1][i]);
for(int i = st.num - 1; i >= 0; i--)
dp[st.dir][i] = c[st.dir][i] + dp[st.dir][i + 1];
dp[!st.dir][0] = c[2][0] + dp[st.dir][0];
for(int i = 1; i <= st.num; i++)
dp[!st.dir][i] = min(dp[st.dir][i] + c[2][i], dp[!st.dir][i - 1] + c[!st.dir][i - 1]);
for(int i = fi.num + 1; i <= n; i++)
dp[fi.dir][i] = c[fi.dir][i - 1] + dp[fi.dir][i - 1];
dp[!fi.dir][n] = c[2][n] + dp[fi.dir][n];
for(int i = n - 1; i >= fi.num; i--)
dp[!fi.dir][i] = min(dp[fi.dir][i] + c[2][i], dp[!fi.dir][i + 1] + c[!fi.dir][i]);
for(int i = st.num + 1; i < fi.num; i++)
{
dp[0][i] = min(dp[0][i - 1] + c[0][i - 1], dp[1][i - 1] + c[1][i - 1] + c[2][i]);
dp[1][i] = min(dp[1][i - 1] + c[1][i - 1], dp[0][i - 1] + c[0][i - 1] + c[2][i]);
}
if(fi.num > 0)
{
dp[!fi.dir][fi.num] += dp[!fi.dir][fi.num - 1] + c[!fi.dir][fi.num - 1];
dp[fi.dir][fi.num] = min(dp[!fi.dir][fi.num], dp[fi.dir][fi.num - 1] + c[fi.dir][fi.num - 1]);
}
else
dp[fi.dir][fi.num] = dp[!fi.dir][fi.num];
cout << dp[fi.dir][fi.num] << endl;
}
return 0;
}

不想写注释了···就算写了我的代码也不会变的好读的QAQ

_(:з」∠)_

POJ 3377 Ferry Lanes的更多相关文章

  1. POJ 2609 Ferry Loading(双塔DP)

    Ferry Loading Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1807   Accepted: 509   Sp ...

  2. POJ 2609 Ferry Loading

    双塔DP+输出路径. 由于内存限制,DP只能开滚动数组来记录. 我的写法比较渣,但是POJ能AC,但是ZOJ依旧MLE,更加奇怪的是Uva上无论怎么改都是WA,其他人POJ过的交到Uva也是WA. # ...

  3. poj 2336 Ferry Loading II ( 【贪心】 )

    Ferry Loading II Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3704   Accepted: 1884 ...

  4. 【POJ3377】Ferry Lanes 最短路

    我仅仅是贴一下手写堆优化的dij模板.尽管.它.TLE了--**** #include <cstdio> #include <cstring> #include <ios ...

  5. POJ 3895 Cycles of Lanes (dfs)

    Description Each of the M lanes of the Park of Polytechnic University of Bucharest connects two of t ...

  6. poj 动态规划题目列表及总结

    此文转载别人,希望自己能够做完这些题目! 1.POJ动态规划题目列表 容易:1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 11 ...

  7. poj动态规划列表

    [1]POJ 动态规划题目列表 容易: 1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 1189, 1208, 1276, 13 ...

  8. POJ 动态规划题目列表

    ]POJ 动态规划题目列表 容易: 1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 1189, 1208, 1276, 1322 ...

  9. poj 动态规划的主题列表和总结

    此文转载别人,希望自己可以做完这些题目. 1.POJ动态规划题目列表 easy:1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, ...

随机推荐

  1. There is a legend about a bird

          There is a legend about a bird which sings just once in its life, more sweetly than any other ...

  2. 【BZOJ3262】 陌上花开

    Description 有n朵花,每朵花有三个属性:花形(s).颜色(c).气味(m),又三个整数表示.现要对每朵花评级,一朵花的级别是它拥有的美丽能超过的花的数量.定义一朵花A比另一朵花B要美丽,当 ...

  3. 对.net orm工具Dapper在多数据库方面的优化

    Dapper是近2年异军突起的新ORM工具,它有ado.net般的高性能又有反射映射实体的灵活性,非常适合喜欢原生sql的程序员使用,而且它源码很小,十分轻便.我写本博客的目的不是为了介绍Dapper ...

  4. XCode6.1中的ios7.1适配

    在xcode6.1中新创建的项目,运行在我的ios7.1的ipod touch上时(与5s的一样的尺寸, Retina屏幕), 上下出现了黑边,由于没有下载7.1的模拟器,不知道模拟器上有无问题, 查 ...

  5. 【数学】[BZOJ 3884] 上帝与集合的正确用法

    Description 根据一些书上的记载,上帝的一次失败的创世经历是这样的: 第一天, 上帝创造了一个世界的基本元素,称做“元”. 第二天, 上帝创造了一个新的元素,称作“α”.“α”被定义为“元” ...

  6. Windows 2008 R2系统开机时如何不让Windows进行磁盘检测?

    开始→运行,在运行对话框中键入“chkntfs /t:0”,即可将磁盘扫描等待时间设置为0, 如果要在计算机启动时忽略扫描某个分区,比如C盘,可以输入“chkntfs /x c:”命令:如果要恢复对C ...

  7. [转载]获取当前日期和农历的js代码

    原文地址: http://www.cnblogs.com/Gnepner/archive/2011/09/07/2169822.html 获取当前日期时间: function GetCurrentDa ...

  8. Good Bye 2015B

    Problem B:http://codeforces.com/contest/611/problem/B B. New Year and Old Property 题意:问输入的年份a到b中转化为二 ...

  9. 12 求1+2+...+n

    参考 http://www.cppblog.com/zengwei0771/archive/2012/04/28/173014.html 和 http://blog.csdn.net/shiren_b ...

  10. ajax的GET和POST请求

    GET和POST请求 GET请求时最常见的请求类型,用于向服务器查询信息,必要时可以将查询字符串参数放在URL尾部发送给服务器,如果参数有特殊字符必须正确编码.我们上面使用的例子都是使用GET请求,非 ...